Answers.Body: <p>I developed the function and it worked perfectly,</p>
  <pre class="lang-r prettyprint-override"><code>Converst_taxID_to_taxName &lt;- function(tax_id) {
    tax_info &lt;- entrez_summary(db=&quot;taxonomy&quot;, id=tax_id)
    return(tax_info$scientificname)
  }
  
  # usage example
  myDataFrame &lt;- myDataFrame %&gt;%
  mutate(Organism = sapply(`subject tax id`, Converst_taxID_to_taxName))
  
  # in case one tax ID has a problem or cannot be proceeded, it puts &quot;NA&quot; and automatically goes the next ID
  Converst_taxID_to_taxName2 &lt;- function(tax_id) {
    tryCatch({
      tax_info &lt;- entrez_summary(db=&quot;taxonomy&quot;, id=tax_id)
      return(tax_info$scientificname)
    }, error = function(e) {
      return(NA)
    })
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-04T19:45:07.590Z
Answers.Text: Adding that the function entrez_summary is from the R library rentrez.
Answers.UserDisplayName: aminards
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-16T16:42:10.660Z
Answers.LastActivityDate: 2023-11-16T20:26:23.270Z
Answers.LastEditDate: 2023-11-16T20:26:23.270Z
Answers.UserDisplayName: Farzad Beikpour
Body: <p>I have a data frame on R, called qIns8, with 14 columns and 204 rows. the column called &quot;subject tax id&quot; has NCBI taxIDs for the corresponding row, I need a script to add another column to the data frame called Organism for which the script has to get the organism name (NCBI taxname) from the column subject tax id.
  I already tried this script using Taxize package, but it did not work:</p>
  <pre class="lang-r prettyprint-override"><code>getTAXname &lt;- function(tax_id) {
    name &lt;- tryCatch({
      tax_name &lt;- sci_name(tax_id)
      return(tax_name)
    }, error = function(e) {
      return(NA)
    })
    return(name)
  }
  
  # Apply the function to each row of the dataframe
  qIns8<span class="math-container">$Organism &lt;- sapply(qIns8$</span>subject_tax_id, getTAXname)
  
  # Check the updated dataframe
  head(qIns8)
  </code></pre>
  <p>I also tried this from Taxonomizr package and it did not work either:</p>
  <pre class="lang-r prettyprint-override"><code>getTaxonomy(qIns8$`subject tax id`, sqlFile = qIns8$`Organism`)
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-11-16T09:28:55.117Z
Comments.Text: Please make sure to format code as code. See https://bioinformatics.stackexchange.com/help/formatting.
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-11-16T09:30:30.687Z
Comments.Text: Please [edit] your question and explain what "didn't work" means. How did it fail? Was there an error? What error? Was there no error but you didn't get the expected result? What did you get? What were you expecting? Also, try to give us an example we can use. Simplify your script to the minimum code that reproduces the issue, provide us with a simple data frame that we can also load and try our solutions out with.
Comments.UserDisplayName: terdon
Tags: <r><taxonomy><ncbi>
Title: getting NCBI TAXname by R
ThreadText: # getting NCBI TAXname by R
  
  (Asked by: username_0 on 2023-11-16)
  
  I have a data frame on R, called qIns8, with 14 columns and 204 rows. the column called "subject tax id" has NCBI taxIDs for the corresponding row, I need a script to add another column to the data frame called Organism for which the script has to get the organism name (NCBI taxname) from the column subject tax id.
  I already tried this script using Taxize package, but it did not work:
  
  ```r
  getTAXname <- function(tax_id) {
    name <- tryCatch({
      tax_name <- sci_name(tax_id)
      return(tax_name)
    }, error = function(e) {
      return(NA)
    })
    return(name)
  }
  
  # Apply the function to each row of the dataframe
  qIns8$Organism <- sapply(qIns8$subject_tax_id, getTAXname)
  
  # Check the updated dataframe
  head(qIns8)
  
  ```
  
  I also tried this from Taxonomizr package and it did not work either:
  
  ```r
  getTaxonomy(qIns8$`subject tax id`, sqlFile = qIns8$`Organism`)
  
  ```
  
  **Tags:** r, taxonomy, ncbi
  
  > **Comments:**
  > **username_1**: Please make sure to format code as code. See https://bioinformatics.stackexchange.com/help/formatting.  
  > **username_1**: Please [edit] your question and explain what "didn't work" means. How did it fail? Was there an error? What error? Was there no error but you didn't get the expected result? What did you get? What were you expecting? Also, try to give us an example we can use. Simplify your script to the minimum code that reproduces the issue, provide us with a simple data frame that we can also load and try our solutions out with.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2023-11-16. Score: 2
  
  I developed the function and it worked perfectly,
  
  ```r
  Converst_taxID_to_taxName <- function(tax_id) {
    tax_info <- entrez_summary(db="taxonomy", id=tax_id)
    return(tax_info$scientificname)
  }
  
  # usage example
  myDataFrame <- myDataFrame %>%
  mutate(Organism = sapply(`subject tax id`, Converst_taxID_to_taxName))
  
  # in case one tax ID has a problem or cannot be proceeded, it puts "NA" and automatically goes the next ID
  Converst_taxID_to_taxName2 <- function(tax_id) {
    tryCatch({
      tax_info <- entrez_summary(db="taxonomy", id=tax_id)
      return(tax_info$scientificname)
    }, error = function(e) {
      return(NA)
    })
  }
  
  ```
  
  > **Comments:**
  > **username_2**: Adding that the function entrez_summary is from the R library rentrez.  
  
  ---
  
  