我有一个data.frame
DAT
包含以下格式的字符串的 8 列(这些是测验的八个问题的多项选择答案):
Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8
1 ,,1 ,,1 ,1, 1,, ,,1 ,,1 ,1, 1,,
2 ,,1 ,,1 ,1, 1,, ,,1 ,,1 ,1, 1,,
3 ,,1 ,,1 ,1, 1,, ,,1 ,,1 ,1, 1,,
4 ,,1 ,,1 ,1, 1,, ,,1 ,,1 ,1, 1,,
5 ,,1 ,,1 ,1, 1,, ,,1 ,,1 ,1, 1,,
6 ,,1 ,,1 ,1, 1,, ,,1 ,,1 ,1, 1,,
我想将其转换为以下内容:
q11 q12 q13 q21 q22 q23 q31 q32 q33 q41 q42 q43 q51 q52 q53 q61 q62 q63 q71 q72 q73 q81 q82 q83
1 0 0 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0 0
2 0 0 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0 0
3 0 0 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0 0
4 0 0 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0 0
5 0 0 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0 0
6 0 0 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0 0
所以我写了以下代码:
NAMES.Q = paste(rep("Q",8), c(1:8), sep="")
DAT[ which(DAT[NAMES.Q]=="NULL"),]<- NA # to set to NA skipped questions
NAMES.q = paste(rep("q",8), c(1:8), sep="")
以下代码是将字符串转换为0
数值1
。
q1 <- read.csv(text = as.character(DAT[,"Q1"]), strip.white = TRUE)
q2 <- read.csv(text = as.character(DAT[,"Q2"]), strip.white = TRUE)
q3 <- read.csv(text = as.character(DAT[,"Q3"]), strip.white = TRUE)
q4 <- read.csv(text = as.character(DAT[,"Q4"]), strip.white = TRUE)
q5 <- read.csv(text = as.character(DAT[,"Q5"]), strip.white = TRUE)
q6 <- read.csv(text = as.character(DAT[,"Q6"]), strip.white = TRUE)
q7 <- read.csv(text = as.character(DAT[,"Q7"]), strip.white = TRUE)
q8 <- read.csv(text = as.character(DAT[,"Q8"]), strip.white = TRUE)
names(q1) = paste("q1", 1:3, sep = "")
names(q2) = paste("q2", 1:3, sep = "")
names(q3) = paste("q3", 1:3, sep = "")
names(q4) = paste("q4", 1:3, sep = "")
names(q5) = paste("q5", 1:3, sep = "")
names(q6) = paste("q6", 1:3, sep = "")
names(q7) = paste("q7", 1:3, sep = "")
names(q8) = paste("q8", 1:3, sep = "")
q1[is.na(q1)] <- 0
q2[is.na(q2)] <- 0
q3[is.na(q3)] <- 0
q4[is.na(q4)] <- 0
q5[is.na(q5)] <- 0
q6[is.na(q6)] <- 0
q7[is.na(q7)] <- 0
q8[is.na(q8)] <- 0
qs<-cbind(q1, q2, q3, q4, q5, q6, q7, q8)
该代码有效,但我发现它很难阅读和维护。
你会建议一个循环或另一种方式在我的主要data.frame
(DAT)中写入这些信息而不创建一个新的data.frame
?