How to display plant species biomass in a site by species matrix?

巧了我就是萌 提交于 2019-12-01 14:19:48

The xtabs and tapply functions return a table which is a matrix:

# Using MrFlick's example
> xtabs(~a+b,dd)
   b
a   f g h i j
  a 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

# --- the tapply solution is a bit less elegant
> dd$one=1
> with(dd, tapply(one, list(a,b), sum))
   f  g  h  i  j
a 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 you want to make the NA's become zeros then:

> tbl <- with(dd, tapply(one, list(a,b), sum))
> tbl[is.na(tbl)] <- 0
> tbl
  f g h i j
a 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

With sample data

set.seed(15)
dd<-data.frame(
    a=sample(letters[1:5], 30, replace=T),
    b=sample(letters[6:10], 30, replace=T)
)

if you know each occurrence only appears once you can do

with(dd, table(a,b))

#    b
# a   f g h i j
#   a 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

if they are potentially duplicated, and you only want to track presence/absence, you can do

with(unique(dd), table(a,b))
# or 
with(dd, (table(a,b)>0)+0)

#    b
# a   f g h i j
#   a 0 1 0 1 1
#   b 0 0 1 1 0
#   c 0 1 0 0 1
#   d 1 1 1 1 1
#   e 1 1 1 1 1
Tim

You asked also about a solution when there are three variables. Below I provide two solutions that you asked for.

First, let's set up the data the data:

set.seed(15)
dd<-data.frame(
  a=sample(letters[1:5], 30, replace=T),
  b=sample(letters[6:10], 30, replace=T),
  c=sample(letters[1:3], 30, replace=T)
)

If you have three discrete variables and want only to count the occurrences, here you have a version of solution by @MrFlick:

by(dd, dd$c, function(x) with(x, table(a, b)))

And if you want average values of the third variable you can use this solution:

reshape::cast(dd, a ~ b, value = 'c', fun = mean)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!