我是R的新手并且尝试一次重新编码多个变量。所有变量都有类似的限制: 我很高兴任何有用的文章。
# actually real life data
Bed <- rep(c("1", "2"),10)
reported <- rep(c("yes", "no"), 10)
x <- c(100:110) #in real life a few missings and more variable
var1 <- x
var2 <- x
data <- data.frame(Bed, reported, var1, var2)
#etc.
#restrictions
reported <- rep(c("yes","no"), each=4)
Bed <- rep(c("1", "2", "3", "4"), each=2)
mtrx <- matrix( , nrow(data),160)
data_new <- data.frame(mtrx)
#this is the part that does not work!
for (i in 1:20){
for (j in 1:8)
data_new[,i][(data$reported==reported[2*j]) & (data$Bed==Bed[j])] <- data[,(i+6)] [(data$reported==reported[2*j]) & (data$Bed==Bed[j])]
}
- &GT;数据包括20个变量,这些变量应在条件
下重新编码为data_new