Manipulating seperated species quantity data into a species abundance matrix

浪子不回头ぞ 提交于 2019-12-12 02:07:25

问题


I was hoping somebody could help with some data manipulation in R, i'm struggling to get this to work as the data is currently in a slightly odd format.

I need a species abundance table in order to run some of the functions in vegan.

However when I collected the data I inputted it in a way which is not very compatable as I had to keep species collected from the same site seperated by date and other factors which was necessary for another program.

So my data currently looks like 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 what i'm trying to manipulate it to is an abundance matrix, so 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 was to loop through for each site, then a second loop for each taxon within a site, i would then take the sum of the quantity for each taxa and cbind it into a data table, the problem occurs as I end up with lots of tables with different numbers of columns for each site, which then cant be bound together into one.

Any help or suggestions would be very much appreciated.

Many thanks.


回答1:


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


来源:https://stackoverflow.com/questions/25366929/manipulating-seperated-species-quantity-data-into-a-species-abundance-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!