[英]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.