Answers.Body: <p>Those aren't external_gene_name's, they're ensembl_gene_id_versions:</p>
  <pre><code>theBM &lt;- getBM(attributes='ensembl_gene_id','hgnc_symbol', 
                 filters = 'ensembl_gene_id_version', 
                 values = charg2, 
                 mart = ensembl)
  </code></pre>
  <p>Note that you'll get more hits if you strip the gene ID versions off:</p>
  <pre><code>charg2 = sapply(strsplit(charg, '.', fixed=T), function(x) x[1])
  theBM = getBM(attributes='ensembl_gene_id','hgnc_symbol', 
                filters = 'ensembl_gene_id', 
                values = charg2, 
                mart = ensembl)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-20T17:08:51.700Z
Answers.Text: Okay, thank you! So I tried this -- it's returning `theBM` (finally!) with the gene names, but when I merge it, the merge still doesn't work :(. Thank you immensely though :)))).
Answers.UserDisplayName: Equinox
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-20T19:59:24.557Z
Answers.Text: Look at the first few rows of each and it should be apparent why there’s a mismatch.
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-20T20:35:30.383Z
Answers.Text: Hm, maybe merge isn't the right tool. I'm trying to have the HGNC symbols replace my column of Ensembl Gene IDs -- the getBM worked beautifully, but what next?
Answers.UserDisplayName: Equinox
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-20T20:40:09.647Z
Answers.Text: left_join from dplyr
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-20T20:46:27.637Z
Answers.Text: Okay, please help, how would I implement that?
Answers.UserDisplayName: Equinox
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-20T22:27:11.267Z
Answers.Text: Sorry for the double comment -- this has been gnawing away at me. I was looking through the biomaRt documentation and I found this line: `# rows are sorted by filters, and in different order than the values argument`, which makes sense -- my `getBM` is working. My question is, how do I get the order of getBM to match my results?
Answers.UserDisplayName: Equinox
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-22T16:44:55.610Z
Answers.Text: Use factors to apply the sorting order from one dataset to another.
Answers.UserDisplayName: Ram RS
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-20T13:22:26.437Z
Answers.LastActivityDate: 2020-06-20T13:22:26.437Z
Answers.UserDisplayName: Devon Ryan
Body: <p><em>This question has also been asked on <a href="https://www.biostars.org/p/444838/" rel="nofollow noreferrer">Biostars</a> and <a href="https://stackoverflow.com/questions/62480055/trying-to-convert-ensembl-id-to-gene-name-in-r-biomart">StackOverflow</a></em></p>
  <p>I've been trying to code (in R) a way to convert gene accession numbers to gene names (from RNAseq data). I've looked at all the related questions and tried to modify my code such, but for some reason it's still not working. Here is my code, where <code>charg</code> is a character vector of the gene accession ID's of the data set <code>resdata</code>:</p>
  <pre><code>charg &lt;- resdata$genes
  head(charg)
  
  library(biomaRt)
  
  ensembl = useMart(&quot;ensembl&quot;,dataset=&quot;hsapiens_gene_ensembl&quot;)
  
  theBM &lt;- getBM(attributes='ensembl_gene_id','hgnc_symbol', 
        filters = 'external_gene_name', 
        values = charg, 
        mart = ensembl)
  
  resdata &lt;- merge.data.frame(resdata, theBM, by.x=&quot;genes&quot;,by.y=&quot;ensembl_gene_id&quot;)
  </code></pre>
  <p>Here's some output (where I'm struggling):</p>
  <pre><code>&gt; head(charg)
  [1] &quot;ENSG00000261150.2&quot;  &quot;ENSG00000164877.18&quot; &quot;ENSG00000120334.15&quot;
  [4] &quot;ENSG00000100906.10&quot; &quot;ENSG00000182759.3&quot;  &quot;ENSG00000124145.6&quot; 
  
  &gt; dim(theBM)
  [1] 0 1
  
  &gt; head(theBM)
  [1] ensembl_gene_id
  &lt;0 rows&gt; (or 0-length row.names)
  
  &gt; dim(resdata)
  [1] 20381    11
  &gt; resdata &lt;- merge.data.frame(resdata, theBM, by.x=&quot;genes&quot;,by.y=&quot;ensembl_gene_id&quot;)
  &gt; dim(resdata) #after merge
  [1]  0 11 #isn't correct -- just row names! where'd my genes go?
  </code></pre>
  <p>Thank you.</p>
  
Tags: <r><rna-seq><gene><biomart>
Title: Problem with merge data while trying to convert gene names in R
ThreadText: # Problem with merge data while trying to convert gene names in R
  
  (Asked by: username_0 on 2020-06-20)
  
  *This question has also been asked on [Biostars](https://www.biostars.org/p/444838/) and [StackOverflow](https://stackoverflow.com/questions/62480055/trying-to-convert-ensembl-id-to-gene-name-in-r-biomart)*
  
  I've been trying to code (in R) a way to convert gene accession numbers to gene names (from RNAseq data). I've looked at all the related questions and tried to modify my code such, but for some reason it's still not working. Here is my code, where `charg` is a character vector of the gene accession ID's of the data set `resdata`:
  
  ```
  charg <- resdata$genes
  head(charg)
  
  library(biomaRt)
  
  ensembl = useMart("ensembl",dataset="hsapiens_gene_ensembl")
  
  theBM <- getBM(attributes='ensembl_gene_id','hgnc_symbol', 
        filters = 'external_gene_name', 
        values = charg, 
        mart = ensembl)
  
  resdata <- merge.data.frame(resdata, theBM, by.x="genes",by.y="ensembl_gene_id")
  
  ```
  
  Here's some output (where I'm struggling):
  
  ```
  > head(charg)
  [1] "ENSG00000261150.2"  "ENSG00000164877.18" "ENSG00000120334.15"
  [4] "ENSG00000100906.10" "ENSG00000182759.3"  "ENSG00000124145.6" 
  
  > dim(theBM)
  [1] 0 1
  
  > head(theBM)
  [1] ensembl_gene_id
  <0 rows> (or 0-length row.names)
  
  > dim(resdata)
  [1] 20381    11
  > resdata <- merge.data.frame(resdata, theBM, by.x="genes",by.y="ensembl_gene_id")
  > dim(resdata) #after merge
  [1]  0 11 #isn't correct -- just row names! where'd my genes go?
  
  ```
  
  Thank you.
  
  **Tags:** r, rna-seq, gene, biomart
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-06-20. Score: 1
  
  Those aren't external\_gene\_name's, they're ensembl\_gene\_id\_versions:
  
  ```
  theBM <- getBM(attributes='ensembl_gene_id','hgnc_symbol', 
                 filters = 'ensembl_gene_id_version', 
                 values = charg2, 
                 mart = ensembl)
  
  ```
  
  Note that you'll get more hits if you strip the gene ID versions off:
  
  ```
  charg2 = sapply(strsplit(charg, '.', fixed=T), function(x) x[1])
  theBM = getBM(attributes='ensembl_gene_id','hgnc_symbol', 
                filters = 'ensembl_gene_id', 
                values = charg2, 
                mart = ensembl)
  
  ```
  
  > **Comments:**
  > **username_0**: Okay, thank you! So I tried this -- it's returning `theBM` (finally!) with the gene names, but when I merge it, the merge still doesn't work :(. Thank you immensely though :)))).  
  > **username_1**: Look at the first few rows of each and it should be apparent why there’s a mismatch.  
  > **username_0**: Hm, maybe merge isn't the right tool. I'm trying to have the HGNC symbols replace my column of Ensembl Gene IDs -- the getBM worked beautifully, but what next?  
  > **username_1**: left_join from dplyr  
  > **username_0**: Okay, please help, how would I implement that?  
  > **username_0**: Sorry for the double comment -- this has been gnawing away at me. I was looking through the biomaRt documentation and I found this line: `# rows are sorted by filters, and in different order than the values argument`, which makes sense -- my `getBM` is working. My question is, how do I get the order of getBM to match my results?  
  > **username_2**: Use factors to apply the sorting order from one dataset to another.  
  
  ---
  
  