dplyr:在group_by中生成行号/行位置

时间:2016-04-27 15:14:57

标签: r data.table dplyr

我有一个数据集,我想按组生成行位置。例如

library(data.table)

data<-data.table(Position=c(1,2,3,4,5,6,7,8,9,10),
Category=c("M","M","M","M","F","F","F","M","M","F"))

我按类别进行分组,并希望按组创建作为行位置的列。像下面的东西或data.table

dataByGroup %>% group_by(Category) %>% mutate(positionInCategory = 1:nrow(Category))

无法弄清楚如何实现这一目标?

期望的输出:

| Position|Category | positionInCategory|
|--------:|:--------|------------------:|
|        1|M        |                  1|
|        2|M        |                  2|
|        3|M        |                  3|
|        4|M        |                  4|
|        5|F        |                  1|
|        6|F        |                  2|
|        7|F        |                  3|
|        8|M        |                  5|
|        9|M        |                  6|
|       10|F        |                  4|

2 个答案:

答案 0 :(得分:21)

尝试以下方法:

library(data.table)
library(dplyr)

data<-data.table(Position=c(1,2,3,4,5,6,7,8,9,10),
                 Category=c("M","M","M","M","F","F","F","M","M","F"))

cleanData <- data %>%
  group_by(Category) %>%
  mutate(positionInCategory = 1:n())

答案 1 :(得分:6)

尝试

data[, new := rowid(Category)]
# or, if you're using 1.9.6 or older
data[, new := 1:.N, by=Category]

    Position Category new
 1:        1        M   1
 2:        2        M   2
 3:        3        M   3
 4:        4        M   4
 5:        5        F   1
 6:        6        F   2
 7:        7        F   3
 8:        8        M   5
 9:        9        M   6
10:       10        F   4

要使用rowid,您目前需要unstable/devel version of the package