Ciao
这是我的复制示例。
have <- data.frame("STUDENT"=c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5),
"CLASS"=c(10,10,10,30,30,30,50,50,50,70,70,70,90,90,90),
"SCORE"=c(1:15))
want <- data.frame("STUDENT"=c(101,101,101,102,102,102,103,103,103,104,104,104,105,105,105),
"CLASS"=c(1,1,1,3,3,3,5,5,5,7,7,7,9,9,9),
"SCORE"=c(1:15))
所以我想基本上为学生和班级生成不同的数字顺序。换句话说,我想重新编码STUDENT和CLASS,以便它们按特定的顺序排列。
我想做的是
have %>% group_by(id) %>%
mutate(1=10,2=30)
但这很乏味,而且也不起作用。谢谢你!
答案 0 :(得分:1)
我们可以尝试
have %>%
mutate(STUDENT = factor(STUDENT, labels = 101:105),
CLASS = substr(CLASS, 1, 1))