我有一个数据集,其中包含一组人的观察分数,如下所示:
person_id <- c(1:50)
person_score <- rep(1:10,5)
people <- data.frame(person_id, person_score)
我需要创建一组新变量,这些变量是观察分数的重新编码值。我有一组变量,它们是将观察到的分数转换为新变量的“键”,如下所示:
observed <- c(1,2,3,4,5,6,7,8,9,10)
score1 <- c(10,14,17,18,20,21,22,26,28,31)
score2 <- c(6,9,11,14,17,18,20,24,25,26)
score3 <- c(11,13,15,17,19,21,23,25,27,29)
score4 <- c(43,44,45,46,47,48,49,50,51,52)
scores <- data.frame(observed,score1,score2, score3, score4)
...其中第一个值对应于观察到的分数 = 1,第二个值对应于观察到的分数 = 2,依此类推。
我需要创建四个新变量,分别对应 score1、score2、score3 和 score 4。我可以考虑手动重新编码,如下所示,但它非常缓慢且繁琐:
people$value1[person_score == 1] <- 10
people$value1[person_score == 2] <- 14
...等等 score1
people$value2[person_score == 1] <- 6
people$value2[person_score == 2] <- 9
...等等 score2
people$value3[person_score == 1] <- 11
people$value3[person_score == 2] <- 13
...等等 score3
people$value4[person_score == 1] <- 43
people$value4[person_score == 2] <- 44
...等等 score4