这是我的第一个问题,所以这里......
我的数据集是
person = c("a","a","a","a","b","b","b","b","c","c","d","d","d","d","d","e","e","e","f")
training = c("q1","q2","q7","q4","q1","q2","q3","q4","q3","q4","q3","q4","q5","q6","q99","q18","q1","q9","q99")
data = data.frame(person,training)
我想做一个countifs样式函数来计算一个人根据他们在列表中的位置记录的次数 - 通常我会在excel中使用绝对引用来执行此操作但是我有93k行数据所以它保持崩溃。
在Excel中我会:
如何在R中复制data$id
?
答案 0 :(得分:0)
以下是dplyr的解决方案:
library(dplyr)
data <- group_by(data,person) %>% mutate(id=row_number())
data
person training id
(fctr) (fctr) (int)
1 a q1 1
2 a q2 2
3 a q7 3
4 a q4 4
5 b q1 1
6 b q2 2
7 b q3 3
8 b q4 4
9 c q3 1
10 c q4 2
11 d q3 1
12 d q4 2
13 d q5 3
14 d q6 4
15 d q99 5
16 e q18 1
17 e q1 2
18 e q9 3
19 f q99 1
答案 1 :(得分:0)
这是一个可能的解决方案:
data$id <- sapply(1:nrow(data),function(r) sum(data$person[1:r]==data$person[r]))