Search code examples
rdataframephyloseq

Max Column for each Row


I'm an R novice, so I can't make a sample data frame for you, for which I apologize. However, I'm doing bacterial community analysis and I have a table that has species for each column and each sample for each row. Each column is an identifier for each species. Within the data frame is the species abundance for each sample. My goal is to identify the most abundant species (column) for each sample (row). I think making a data frame that has samples (rows) with the most abundant species column identifier would be the most useful! Iterations I've tried (using phyloseq package, but can be used without this package).

beagle <- names(sort(taxa_sums(top.pdy.phyl),T, function(x) which(max==x)))
beagle2 <- names(taxa_sums(top.pdy.phyl),T, function(x) colnames(which.max(top.pdy.phyl))))

Any help would be appreciated! Thank you!


Solution

  • How about:

    names(top.pdy.phyl)[ apply(top.pdy.phyl, 1, which.max) ]
    

    and

    apply(top.pdy.phyl, 1 , max)