Answers.Body: <p>So after digging I found the solution. What worked for me was adding the <code>file = </code> in the <code>write.nexus</code> command. The final line should be
  <code>write.nexus(tree, file =  &quot;Cod.trees&quot;) </code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-17T17:16:09.437Z
Answers.Text: Ahhh! Right of course! Many thanks for posting back. Could you very kindly mark you post as "accepted". Admittedly, you don't any bonus rep, but it helps site statistics and it is definitely the solution. I completely missed the `file =`.
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-16T15:50:14.943Z
Answers.LastActivityDate: 2023-10-16T15:50:14.943Z
Answers.UserDisplayName: Nickmofoe
Body: <p>I have a phylogenetic tree with a lot of different populations and I wanted to removes most of them and apply new names to the labels. I wrote this code:</p>
  <pre class="lang-r prettyprint-override"><code>library(ape)
  library(phylotools)
  tree&lt;- read.tree(&quot;Consensus_COMBINED_Cod3genesRatesC_trees.trees&quot;)
  
  tree&lt;- drop.tip(tree, tip = c(&quot;CeuMist2&quot;,&quot;CeuMist1&quot;,&quot;Ceu_PAN_2&quot;,&quot;Ceu_PAN_1&quot;,&quot;Ceu_PAN_3&quot;,&quot;Ceu_PAN_4&quot;))
  tree&lt;- drop.tip(tree, tip= c(&quot;CcNav1&quot;,&quot;CcNav2&quot;,&quot;Cc_Mal_1&quot;,&quot;Cc_KalR2_2&quot;,&quot;CcRod1&quot;,&quot;CcRod2&quot;,&quot;CcRod3&quot;,&quot;CcRod4&quot;,&quot;CcHr1&quot;))
  tree&lt;- drop.tip(tree, tip= c(&quot;CeST6_4&quot;,&quot;CeST6_5&quot;,&quot;CeAA7_1&quot;,&quot;CeST6_3&quot;,&quot;CeST6_2&quot;))
  tree&lt;- drop.tip(tree, tip= c(&quot;Cg_MelR3_1&quot;,&quot;Cg_MelR6_2&quot;,&quot;Cg_MelR7_2&quot;,&quot;Cg_MelR6_1&quot;,&quot;Cg_MelR7_3&quot;,&quot;Cg_MelR5_1&quot;,&quot;Cg_MelR4_2&quot;,&quot;Cg_MelR1_1&quot;,&quot;Cg_MelR2_1&quot;,&quot;Cg_MelR4_1&quot;,&quot;Cg_MelR7_4&quot;,&quot;Cg_MelR5_2&quot;))
  tree&lt;- drop.tip(tree, tip= c(&quot;ChTM5_1&quot;,&quot;ChTM4_3&quot;,&quot;ChTM4_1&quot;,&quot;Ch_MEN_5&quot;,&quot;Ch_MEN_13&quot;,&quot;Ch_MEN_8&quot;,&quot;Ch_LEV_3&quot;,&quot;Ch_LEV_5&quot;,&quot;Ch_LEV_4&quot;,&quot;Ch_MEN_6&quot;,&quot;Ch_MEN_10&quot;,&quot;Ch_LEV_2&quot;,&quot;Ch_MEN_4&quot;,&quot;Ch_MEN_14&quot;,&quot;Ch_LEV_1&quot;,&quot;Ch_MEN_11&quot;,&quot;Ch_MEN_3&quot;,&quot;Ch_MEN_9&quot;))
  tree&lt;- drop.tip(tree, tip= c(&quot;Ci_MSP_2&quot;,&quot;Ci_MSP_1&quot;,&quot;Ci_DIA_1&quot;,&quot;Ci_DIA_2&quot;,&quot;CiPLAN1&quot;,&quot;CiPLAN3&quot;,&quot;CiPLAN2&quot;,&quot;Ci_MEN_1&quot;,&quot;Ci_MEN_2&quot;,&quot;Ci_MEN_12&quot;))
  tree&lt;- drop.tip(tree, tip= c(&quot;CpGravR3&quot;,&quot;CpGravR4&quot;,&quot;CpGio_2&quot;,&quot;CpGravR5_2&quot;,&quot;CpGravR2&quot;,&quot;CpGravR7&quot;,&quot;CpGio_1&quot;,&quot;CpGio_3&quot;,&quot;CpGravR1&quot;,&quot;CpGravR6_2&quot;,&quot;CpGravR5_1&quot;,&quot;CpThe_1&quot;,&quot;CpSka_1&quot;))
  tree&lt;- drop.tip(tree, tip= c(&quot;Cn_IG_2&quot;,&quot;Cn_IG_3&quot;,&quot;Cn_IG_1&quot;,&quot;LevantinaFC4779&quot;,&quot;Assyriella_naegelei&quot;))
  
  newlabels&lt;- c(&quot;C. eucineta&quot;, &quot;C. codringtonii&quot;, &quot;C. elisabethae&quot;,&quot;C. gittenbergeri&quot;, &quot;C. helenae&quot;, &quot;C. intusplicata&quot;, &quot;C. parnassia&quot;)
  tree$tip.label&lt;- newlabels
  
  write.nexus(tree, &quot;CodTree.trees&quot;)
  </code></pre>
  <p>When I use the <code>write.nexus</code> command I get the following error:</p>
  <pre class="lang-r prettyprint-override"><code>Error in y$tip.label : $ operator is invalid for atomic vectors
  </code></pre>
  <p>How can I circumvent this error and export the tree in nexus format?</p>
  <p>I will also provide the tree here: <a href="https://drive.google.com/file/d/1lYmzW9htwaclC_yYs3fQom-Z3zbjk8wH/view?usp=drive_link" rel="nofollow noreferrer">https://drive.google.com/file/d/1lYmzW9htwaclC_yYs3fQom-Z3zbjk8wH/view?usp=drive_link</a></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-10-16T13:52:41.070Z
Comments.Text: Hi @Nickmofoe I'll look at this in detail, but will not be until the end of the week (really busy week).
Comments.UserDisplayName: M__
Tags: <r><phylogenetics><file-formats>
Title: How can I export a pruned phylogenetic tree in nexus format in R?
ThreadText: # How can I export a pruned phylogenetic tree in nexus format in R?
  
  (Asked by: username_0 on 2023-10-16)
  
  I have a phylogenetic tree with a lot of different populations and I wanted to removes most of them and apply new names to the labels. I wrote this code:
  
  ```r
  library(ape)
  library(phylotools)
  tree<- read.tree("Consensus_COMBINED_Cod3genesRatesC_trees.trees")
  
  tree<- drop.tip(tree, tip = c("CeuMist2","CeuMist1","Ceu_PAN_2","Ceu_PAN_1","Ceu_PAN_3","Ceu_PAN_4"))
  tree<- drop.tip(tree, tip= c("CcNav1","CcNav2","Cc_Mal_1","Cc_KalR2_2","CcRod1","CcRod2","CcRod3","CcRod4","CcHr1"))
  tree<- drop.tip(tree, tip= c("CeST6_4","CeST6_5","CeAA7_1","CeST6_3","CeST6_2"))
  tree<- drop.tip(tree, tip= c("Cg_MelR3_1","Cg_MelR6_2","Cg_MelR7_2","Cg_MelR6_1","Cg_MelR7_3","Cg_MelR5_1","Cg_MelR4_2","Cg_MelR1_1","Cg_MelR2_1","Cg_MelR4_1","Cg_MelR7_4","Cg_MelR5_2"))
  tree<- drop.tip(tree, tip= c("ChTM5_1","ChTM4_3","ChTM4_1","Ch_MEN_5","Ch_MEN_13","Ch_MEN_8","Ch_LEV_3","Ch_LEV_5","Ch_LEV_4","Ch_MEN_6","Ch_MEN_10","Ch_LEV_2","Ch_MEN_4","Ch_MEN_14","Ch_LEV_1","Ch_MEN_11","Ch_MEN_3","Ch_MEN_9"))
  tree<- drop.tip(tree, tip= c("Ci_MSP_2","Ci_MSP_1","Ci_DIA_1","Ci_DIA_2","CiPLAN1","CiPLAN3","CiPLAN2","Ci_MEN_1","Ci_MEN_2","Ci_MEN_12"))
  tree<- drop.tip(tree, tip= c("CpGravR3","CpGravR4","CpGio_2","CpGravR5_2","CpGravR2","CpGravR7","CpGio_1","CpGio_3","CpGravR1","CpGravR6_2","CpGravR5_1","CpThe_1","CpSka_1"))
  tree<- drop.tip(tree, tip= c("Cn_IG_2","Cn_IG_3","Cn_IG_1","LevantinaFC4779","Assyriella_naegelei"))
  
  newlabels<- c("C. eucineta", "C. codringtonii", "C. elisabethae","C. gittenbergeri", "C. helenae", "C. intusplicata", "C. parnassia")
  tree$tip.label<- newlabels
  
  write.nexus(tree, "CodTree.trees")
  
  ```
  
  When I use the `write.nexus` command I get the following error:
  
  ```r
  Error in y$tip.label : $ operator is invalid for atomic vectors
  
  ```
  
  How can I circumvent this error and export the tree in nexus format?
  
  I will also provide the tree here: <https://drive.google.com/file/d/1lYmzW9htwaclC_yYs3fQom-Z3zbjk8wH/view?usp=drive_link>
  
  **Tags:** r, phylogenetics, file-formats
  
  > **Comments:**
  > **username_1**: Hi @username_0 I'll look at this in detail, but will not be until the end of the week (really busy week).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2023-10-16. Score: 3 | Accepted answer
  
  So after digging I found the solution. What worked for me was adding the `file =`  in the `write.nexus` command. The final line should be
  `write.nexus(tree, file = "Cod.trees")`
  
  > **Comments:**
  > **username_1**: Ahhh! Right of course! Many thanks for posting back. Could you very kindly mark you post as "accepted". Admittedly, you don't any bonus rep, but it helps site statistics and it is definitely the solution. I completely missed the `file =`.  
  
  ---
  
  