dplyr: Generate row number/row position in group_b

2020-02-26 05:06发布

I have a dataset and I want to generate the row position by group. For example

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"))

I group by the Category and want to create column that is the row position by group. Something like below or with data.table

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

Unable to work out how to achieve this?

Desired output:

| 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条回答
Summer. ? 凉城
2楼-- · 2020-02-26 05:28

Try

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

To use rowid, you'll currently need the unstable/devel version of the package.

查看更多
相关推荐>>
3楼-- · 2020-02-26 05:41

Try the following:

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())
查看更多
登录 后发表回答