joey711 / phyloseq

phyloseq is a set of classes, wrappers, and tools (in R) to make it easier to import, store, and analyze phylogenetic sequencing data; and to reproducibly share that data and analysis with others. See the phyloseq front page:
http://joey711.github.io/phyloseq/
569 stars 187 forks source link

subset_taxa in loop #1659

Open jeremysutherland opened 1 year ago

jeremysutherland commented 1 year ago

Does anyone know why this doesn't work?

PHYLA <- unique(ps.rare@tax_table$phylum)
for (n in PHYLA){
  ps <- subset_taxa(ps.rare, phylum == n) ### This doesn't work. Error in eval(e, x, parent.frame()) : object 'n' not found
  print(ps) 
  }

This script is just for example purposes. The issue seems to be passing the "n" variable to the '=='. I've tried str(n), as.character(n), print(n). Nothing seems to do the trick.

jeremysutherland commented 1 year ago

This is the solution:

PHYLA <- unique(ps.rare@tax_table$phylum)
for (i in PHYLA){
ASV <- as.data.frame(ps.rare@otu_table)
TAXA <- as.data.frame(ps.rare@tax_table)
TAXA <- subset(TAXA, phylum == i)
print(dim(ASV))
ASV <- subset(ASV, rownames(ASV) %in% rownames(TAXA))
print(dim(ASV))
}