r - 如何总结具有不同值的某些行,但保持某些列上的所有行相同,然后将其折叠成一行?
问题描述
例如我有数据框:
firstname lastname season attempts yards weight
bob smith 2018 7 38 200
bob smith 2018 11 56 200
bob smith 2018 17 88 200
bob smith 2018 8 24 200
我想把它浓缩成一行:
firstname lastname season attempts yards weight
bob smith 2018 43 206 200
解决方案
我们可以使用aggregate
from base R
。使用公式方法,将列指定sum
为matrix
lhs 上的a~
并.
表示用作分组的所有其他列。指定聚合函数 -sum
aggregate(cbind(attempts, yards) ~ ., df1, sum)
-输出
firstname lastname season weight attempts yards
1 bob smith 2018 200 43 206
或在 中,对除“attempts”、“yards”和所有其他 ( ) 之外的列进行tidyverse
分组,然后获取across
summarise
across
everything()
sum
library(dplyr)
df1 %>%
group_by(across(-c(attempts, yards))) %>%
summarise(across(everything(), sum), .groups = 'drop') %>%
select(names(df1))
-输出
# A tibble: 1 x 6
firstname lastname season attempts yards weight
<chr> <chr> <int> <int> <int> <int>
1 bob smith 2018 43 206 200
数据
df1 <- structure(list(firstname = c("bob", "bob", "bob", "bob"),
lastname = c("smith",
"smith", "smith", "smith"), season = c(2018L, 2018L, 2018L, 2018L
), attempts = c(7L, 11L, 17L, 8L), yards = c(38L, 56L, 88L, 24L
), weight = c(200L, 200L, 200L, 200L)), class = "data.frame", row.names = c(NA,
-4L))