r - Manipulating seperated species quantity data into a species abundance matrix -
i hoping data manipulation in r, i'm struggling work data in odd format.
i need species abundance table in order run of functions in vegan.
however when collected data inputted in way not compatable had keep species collected same site seperated date , other factors necessary program.
so data looks this:
df <- data.frame (site=c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3), species=c("a","a","a","b","b","c","a","b","b","b","c","d","a","b","c","f","f","f"), quantity=c(3,1,1,2,3,3,5,12,1,2,4,1,5,6,3,1,1,1))
and i'm trying manipulate abundance matrix, like:
cola= c(1,2,3) colb=c(5,5,5) colc=c(5,15,6) cold=c(3,4,3) cole=c(0,1,0) colf=c(0,0,1) colg=c(0,0,2) df= data.frame (site=cola, speciesa=colb, speciesb=colc,speciesc=cold,speciesd=cole,speciese=colf,speciesf=colg)
my first plan loop through each site, second loop each taxon within site, take sum of quantity each taxa , cbind data table, problem occurs end lots of tables different numbers of columns each site, cant bound one.
any or suggestions appreciated.
many thanks.
library(reshape2) res <- dcast(df, site~species, value.var="quantity", sum) colnames(res) <- c("site", paste0("species", letters[c(1:4,6)])) res # site speciesa speciesb speciesc speciesd speciesf # 1 1 5 5 3 0 0 # 2 2 5 15 4 1 0 # 3 3 5 6 3 0 3
Comments
Post a Comment