Skip to content

Instantly share code, notes, and snippets.

@slowkow
Last active March 29, 2017 03:18
Show Gist options
  • Select an option

  • Save slowkow/8516bffb7a45e1c8e2a1 to your computer and use it in GitHub Desktop.

Select an option

Save slowkow/8516bffb7a45e1c8e2a1 to your computer and use it in GitHub Desktop.
Reverse mapping in R.

Let's start with a mapping of Ensembl gene identifiers to Entrez gene identifiers:

xs <- structure(
  c("7105", "64102", "9093", "9093"),
  .Names = c("ENSG00000000003", "ENSG00000000005", "ENSG00000103423", "ENSG00000276726")
)
xs
ENSG00000000003 ENSG00000000005 ENSG00000103423 ENSG00000276726 
         "7105"         "64102"          "9093"          "9093" 

Here's a common way to reverse the mapping. Notice that we have two items with the same name:

rxs <- names(xs)
names(rxs) <- xs
rxs
             7105             64102              9093              9093 
"ENSG00000000003" "ENSG00000000005" "ENSG00000103423" "ENSG00000276726" 
class(rxs)
[1] "character"

If we use the reverseSplit() function from BioBase, we get a slightly different result:

reverseSplit(xs)
$`64102`
[1] "ENSG00000000005"

$`7105`
[1] "ENSG00000000003"

$`9093`
[1] "ENSG00000103423" "ENSG00000276726"
class(reverseSplit(xs))
[1] "list"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment