【发布时间】:2017-03-06 14:23:51
【问题描述】:
我有一个数据框 (df):
a <- c("up","up","up","up","down","down","down","down")
b <- c("l","r","l","r","l","l","r","r")
df <- data.frame(a,b)
我想添加第三列 (c),其中包含条目顺序,按列 a 和 b 分组,如下所示:
a b c
1 up l 1
2 up r 1
3 up l 2
4 up r 2
5 down l 1
6 down l 2
7 down r 1
8 down r 2
我尝试过使用 dplyr 的解决方案,但没有奏效:
order <- df %>%
group_by(a) %>%
group_by(b) %>%
mutate(c = row_number()) # This counts the order based on `b`, ignoring `a`
order <- df %>%
group_by(a) %>%
group_by(b) %>%
mutate(c = seq_len(n())) # This counts the order based on `b`, ignoring `a`
如果可能,我更愿意继续使用 dplyr 和管道,但欢迎提出其他建议
【问题讨论】:
-
group_by(a,b) 而不是 2 个 group_by 语句有什么问题?
-
请注意,默认情况下
group_by会覆盖以前的组。使用单个呼叫,或使用add = TRUE。
标签: r dplyr data-manipulation