r - How to display plant species biomass in a site by species matrix? -
i earlier asked "how display 2 columns binary (presence/absence) matrix?". question received 2 excellent answers. take step further , add third column original site species columns reflects biomass of each species in each plot.
column 1 (plot) specifies code ~ 200 plots, column 2 (species) specifies code ~ 1200 species , column 3 (biomass) specifies dryweight. each plot has > 1 species , each species can occur in > 1 plot. total number of rows ~ 2700.
> head(dissim) plot species biomass 1 a1f56r jactom 20.2 2 a1f56r zinunk 10.3 3 a1f56r mikcor 0.4 4 a1f56r rubcle 1.3 5 a1f56r sphoos 12.4 6 a1f56r nepbis1 8.2 tail(dissim) plot species biomass 2707 og100m562r selcup 4.7 2708 og100m562r pip139 30.5 2709 og100m562r stasum 0.1 2710 og100m562r artani 3.4 2711 og100m562r annunk 20.7 2712 og100m562r rubunk 22.6
i create plot species matrix displays biomass of each species in each plot (rather binary presence/absence matrix), of form:
jactom rubcle chrodo uncgla a1f56r 1.3 0 10.3 0 a1f17r 0 22.3 0 4 a1m5r 3.2 0 3.7 9.7 a1m5r 1 0 0 20.1 a1m17r 5.4 6.9 0 1
any advice on how add additional level of complexity appreciated.
the xtabs , tapply functions return table matrix:
# using mrflick's example > xtabs(~a+b,dd) b f g h j 0 1 0 2 3 b 0 0 2 1 0 c 0 3 0 0 1 d 2 2 2 1 1 e 1 1 2 4 1 # --- tapply solution bit less elegant > dd$one=1 > with(dd, tapply(one, list(a,b), sum)) f g h j na 1 na 2 3 b na na 2 1 na c na 3 na na 1 d 2 2 2 1 1 e 1 1 2 4 1 # if want make na's become zeros then: > tbl <- with(dd, tapply(one, list(a,b), sum)) > tbl[is.na(tbl)] <- 0 > tbl f g h j 0 1 0 2 3 b 0 0 2 1 0 c 0 3 0 0 1 d 2 2 2 1 1 e 1 1 2 4 1
Comments
Post a Comment