我有一个数据集,看起来像这样:
df <- data.frame(plot = c("A","A","A","A","A","B","B","B","B","B","C","C","C","C","C"),
species = c("Fagus","Fagus","Quercus","Picea", "Abies","Fagus","Fagus","Quercus","Picea", "Abies","Fagus","Fagus","Quercus","Picea", "Abies"),
value = sample(100, size = 15, replace = TRUE))
head(df)
plot species value
1 A Fagus 53
2 A Fagus 48
3 A Quercus 5
4 A Picea 25
5 A Abies 12
6 B Fagus 12
现在,我想创建一个新的 Dataframe ,其中包含share.conifers
和share.broadleaves
的每个plot
值,方法是将values
与适用于species
的条件相加。我考虑过使用case_when
,但不确定如何编写语法:
df1 <- df %>% share.broadleaves = case_when(plot = plot & species = "Fagus" or species = "Quercus" ~ FUN="sum")
df1 <- df %>% share.conifers = case_when(plot = plot & species = "Abies" or species = "Picea" ~ FUN="sum")
我知道这不对,但我想要这样的。
2条答案
按热度按时间gcuhipw91#
使用
dplyr
/tidyr
:首先构造组,进行计算,然后展开成列。
输出量:
我不确定你是想求和还是想得到份额,但代码可以很容易地适应你的任何目标。
li9yvcax2#
一种方法是通过
plot
和species
进行总结:如果你真的想得到每一地块的特定物种的份额,你也可以这样做:
其给出: