r - Weighting maximum abundance by the number of samples -
i have dataset contains data on abundance of organism , sediment mud content % in found.
i have subsequently partitioned mud content data 10 bins (i.e. 0 - 10%, 10.1 - 20% etc) , placed abundance data each bin accordingly.
the primary aim plot maximum abundance in each mud bin on mud gradient (i.e. 0 - 100 %) these maximums weighted number of samples in each bin.
so, question how weight maximum abundance in given mud bin number of samples in each bin?
here simple subset of data:
mud % bins: | 0 - 9 | 9.1 - 18 | 18.1 - 27 | abundance: 10,10,2,2,2,1,1 15,15,15,2 20,20,20,1,1,1,1,1
you can use ddply
plyr package that. in following code,wtdabundance weighted abundance= (max of bin*number of observation of bin)/total observation
sample data,
mydata<-structure(list(id = 1:19, bin = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 3l, 3l, 3l, 3l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l), .label = c("0-9", "18.1-27", "9.1-18"), class = "factor"), abundance = c(10l, 10l, 2l, 2l, 2l, 1l, 1l, 15l, 15l, 15l, 2l, 20l, 20l, 20l, 1l, 1l, 1l, 1l, 1l)), .names = c("id", "bin", "abundance"), class = "data.frame", row.names = c(na, -19l)) > mydata id bin abundance 1 1 0-9 10 2 2 0-9 10 3 3 0-9 2 4 4 0-9 2 5 5 0-9 2 6 6 0-9 1 7 7 0-9 1 8 8 9.1-18 15 9 9 9.1-18 15 10 10 9.1-18 15 11 11 9.1-18 2 12 12 18.1-27 20 13 13 18.1-27 20 14 14 18.1-27 20 15 15 18.1-27 1 16 16 18.1-27 1 17 17 18.1-27 1 18 18 18.1-27 1 19 19 18.1-27 1 ddply(dat,.(bin), summarize, max.abundance=max(abundance), freq=length(bin),mwtdabundance=((max.abundance*freq/nrow(dat)))) bin max.abundance freq mwtdabundance 1 0-9 10 7 3.684211 2 18.1-27 20 8 8.421053 3 9.1-18 15 4 3.157895
Comments
Post a Comment