r - 使用 tidyr 在多个条件下折叠多行
r - Collapse multiple rows in one following multiple conditions with tidyr
我有这样的数据库结构
A B C
n 1 M
n 2 U
n 1 U
f 3 M
f 4 M
f 1 U
使用包 tidyr,我想得到这个结果:
A B C
n 1 M
n 3 U
f 7 M
f 1 U
所以我想对以相同的 A 值为特征的 b 值求和,得到这个子集,折叠 B 值相对于相同的 C 值。
我该怎么办?
library(dplyr)
df %>%
group_by(A,C) %>%
summarize(B=sum(B)) %>%
data.frame()
我有这样的数据库结构
A B C
n 1 M
n 2 U
n 1 U
f 3 M
f 4 M
f 1 U
使用包 tidyr,我想得到这个结果:
A B C
n 1 M
n 3 U
f 7 M
f 1 U
所以我想对以相同的 A 值为特征的 b 值求和,得到这个子集,折叠 B 值相对于相同的 C 值。
我该怎么办?
library(dplyr)
df %>%
group_by(A,C) %>%
summarize(B=sum(B)) %>%
data.frame()