如何根据因子变量的数量级别创建索引变量?明确地说:
x=c(rep(letters[1:5], 3))
x=sort(x)
index=c(rep(1:3, 5))
data.frame(cbind(x,index))
x index
1 a 1
2 a 2
3 a 3
4 b 1
5 b 2
6 b 3
7 c 1
8 c 2
9 c 3
10 d 1
11 d 2
12 d 3
13 e 1
14 e 2
15 e 3
我想在上面为大数据创建索引变量。
答案 0 :(得分:1)
这对我有用
library(dplyr)
x=c(rep(letters[1:5], 3))
x=sort(x)
index=c(rep(1:3, 5))
df <- data.frame(cbind(x,index))
df <- df %>% group_by(x) %>% mutate(index2= 1:n())