A test for the mutations targeted by the drug is already available, too, for $300 to $600.

Just 4 percent of cancer patients have the type of genetic aberration susceptible to pembrolizumab. But that adds up to a lot of patients: as many as 60,000 each year in the United States alone, the study’s investigators estimated.

Clinicians have long been accustomed to classifying cancers by their location in the body — patients are diagnosed with lung cancer, for example, or brain cancer.

Yet researchers have been saying for years that what matters was the genetic mutation causing the tumors. At first, they were certain they would be able to cure cancers with drugs that zeroed in on the mutations, wherever the tumors were lodged.

But cancers were more complicated than that, said Dr. Drew M. Pardoll, director of the Johns Hopkins Bloomberg-Kimmel Institute and an author of the new paper.

A mutation that appeared in half of all melanomas, for example, turned out to be rare in other cancers. And even when scientists pinpointed that mutation in 10 percent of colon cancers, the drug that worked for melanoma patients did not work for other cancer patients.

“It was a great dream,” Dr. Pardoll sighed.

The new study was based on a different idea. The immune system can recognize cancer cells as foreign and destroy them. But tumors deflect the attack by shielding proteins on their surface, making them invisible to the immune system.

Pembrolizumab is a new type of immunotherapy drug known as a PD-1 blocker, which unmasks the cancer cells so that the immune system can find and destroy them.