如何根据多个条件对行求和 - R? [英] How to sum rows based on multiple conditions - R?
本文介绍了如何根据多个条件对行求和 - R?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,其中包含地块 ID (plotID)、树种代码 (species) 和覆盖值 (cover).您可以看到其中一个地块中有多个树种记录.如果每个地块中有重复的物种"行,我如何对封面"字段求和?
I have a dataframe that contains a plot ID (plotID), tree species code (species), and a cover value (cover). You can see there are multiple records of tree species within one of the plots. How can I sum the "cover" field if there are duplicate "species" rows within each plot?
例如,这里是一些示例数据:
# Sample Data
plotID = c( "SUF200001035014", "SUF200001035014", "SUF200001035014", "SUF200001035014", "SUF200001035014", "SUF200046012040",
"SUF200046012040", "SUF200046012040", "SUF200046012040", "SUF200046012040", "SUF200046012040", "SUF200046012040")
species = c("ABBA", "BEPA", "PIBA2", "PIMA", "PIRE", "PIBA2", "PIBA2", "PIMA", "PIMA", "PIRE", "POTR5", "POTR5")
cover = c(26.893939, 5.681818, 9.469697, 16.287879, 1.893939, 16.287879, 4.166667, 10.984848, 16.666667, 11.363636, 18.181818,
13.257576)
df_original = data.frame(plotID, species, cover)
这是预期的输出:
# Intended Output
plotID2 = c( "SUF200001035014", "SUF200001035014", "SUF200001035014", "SUF200001035014", "SUF200001035014", "SUF200046012040",
"SUF200046012040", "SUF200046012040", "SUF200046012040")
species2 = c("ABBA", "BEPA", "PIBA2", "PIMA", "PIRE", "PIBA2", "PIMA", "PIRE", "POTR5")
cover2 = c(26.893939, 5.681818, 9.469697, 16.287879, 1.893939, 20.454546, 18.651515, 11.363636, 31.439394)
df_intended_output = data.frame(plotID2, species2, cover2)
推荐答案
轻松使用聚合
aggregate(cover~species+plotID, data=df_original, FUN=sum)
使用 data.table
as.data.table(df_original)[, sum(cover), by = .(plotID, species)]
这篇关于如何根据多个条件对行求和 - R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文