R - 添加在组内按顺序计数但重复重复的列

R - add column that counts sequentially within groups but repeats for duplicates

我正在寻找添加列 "desired_result" 的解决方案,最好使用 dplyr and/or ave()。在此处查看数据框,其中组为 "section",我希望我的 "desired_results" 列按顺序计数的唯一实例位于 "exhibit":

structure(list(section = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), exhibit = structure(c(1L, 
2L, 3L, 3L, 1L, 2L, 2L, 3L), .Label = c("a", "b", "c"), class = "factor"), 
desired_result = c(1L, 2L, 3L, 3L, 1L, 2L, 2L, 3L)), .Names = c("section", 
"exhibit", "desired_result"), class = "data.frame", row.names = c(NA, 
-8L))

dense_rank

library(dplyr)
df %>% 
  group_by(section) %>% 
  mutate(desire=dense_rank(exhibit))
#  section exhibit desired_result desire
#1       1       a              1      1
#2       1       b              2      2
#3       1       c              3      3
#4       1       c              3      3
#5       2       a              1      1
#6       2       b              2      2
#7       2       b              2      2
#8       2       c              3      3

我最近将函数 rleid() 推送到 data.table(目前在开发版本 1.9.5 上可用),它就是这样做的。有兴趣的可以按照this.

安装
require(data.table) # 1.9.5, for `rleid()`
require(dplyr)
DF %>% 
  group_by(section) %>% 
  mutate(desired_results=rleid(exhibit))

#   section exhibit desired_result desired_results
# 1       1       a              1               1
# 2       1       b              2               2
# 3       1       c              3               3
# 4       1       c              3               3
# 5       2       a              1               1
# 6       2       b              2               2
# 7       2       b              2               2
# 8       2       c              3               3

如果需要精确枚举并且您希望得到一致的结果(以便同一展品在不同部分的编号始终相同),您可以尝试:

library(dplyr)
df <- data.frame(section = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L),
                 exhibit = c('a', 'b', 'c', 'c', 'a', 'b', 'b', 'c'))
if (is.null(saveLevels <- levels(df$exhibit)))
    saveLevels <- sort(unique(df$exhibit)) ## or levels(factor(df$exhibit))
df %>%
    group_by(section) %>%
    mutate(answer = as.integer(factor(exhibit, levels = saveLevels)))
## Source: local data frame [8 x 3]
## Groups: section
##   section exhibit answer
## 1       1       a      1
## 2       1       b      2
## 3       1       c      3
## 4       1       c      3
## 5       2       a      1
## 6       2       b      2
## 7       2       b      2
## 8       2       c      3

If/when 一个新的 exhibit 出现在随后的 section 中,他们应该得到新的枚举结果。 (注意最后的 exhibit 是不同的。)

df2 <- data.frame(section = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L),
                  exhibit = c('a', 'b', 'c', 'c', 'a', 'b', 'b', 'd'))
if (is.null(saveLevels2 <- levels(df2$exhibit)))
    saveLevels2 <- sort(unique(df2$exhibit))
df2 %>%
    group_by(section) %>%
    mutate(answer = as.integer(factor(exhibit, levels = saveLevels2)))
## Source: local data frame [8 x 3]
## Groups: section
##   section exhibit answer
## 1       1       a      1
## 2       1       b      2
## 3       1       c      3
## 4       1       c      3
## 5       2       a      1
## 6       2       b      2
## 7       2       b      2
## 8       2       d      4