![](/img/trans.png)
[英]Sum a column based on the value of a cell in another column of the same row in R
[英]How to sum a column in R filtered by the value of another column? Or to a specific row?
另一种方法是使用library(dplyr)
函数。 例如,您可以通过迭代对场 ID 求和,如下所示:
yourdata %>%
group_by(Iteration) %>%
summarise(Farm_ID_sum = sum(Farm ID))
一个例子:
example <- data.frame(
"Iteration" = sample(1:5, 20, replace = TRUE),
"FarmID" = sample(1:500, 20, replace = TRUE)
)
example %>%
group_by(Iteration) %>%
summarise(Farm_ID_sum = sum(FarmID))
# Gives the output:
# A tibble: 5 x 2
Iteration Farm_ID_sum
<int> <int>
1 1 790
2 2 694
3 3 391
4 4 1588
5 5 1828
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.