I have a data.frame
DAT
in which there 8 columns containing the strings in the following format (these are the multiple choice answers to eight questions of a quiz):
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,,
I would like to convert it to the following:
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
So I write the following code:
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="")
The following code is to convert the strings into 0
and 1
numeric values.
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)
The code works, but I find it very difficult to read and maintain.
Would you suggest a loop or another way of writing this information in my main data.frame
(DAT) without creating a new data.frame
?
First, read the data with read.table
. The default field separator in read.table
is 'white space', ie the separator between the concatenated "Q" columns.
Then you may use a function in package splitstackshape
, concat.split.multiple
, to split the concatenated columns. By not specifying split.cols
, the columns that need to be split, all columns are split. The default separator character ( seps
) used in each column is ,
.The default shape ( direction
) of the resulting data frame is "wide". Thus, in this case you only need to supply the name of the data frame.
df <- read.table(text=" 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,,", header=TRUE)
library(splitstackshape)
# split columns
df2 <- concat.split.multiple(df)
# or explicitly writing out the arguments
df2 <- concat.split.multiple(data = df, split.cols = names(df), seps = ",")
# replace NA with 0
df2[is.na(df2)] <- 0
df2
# Q1_1 Q1_2 Q1_3 Q2_1 Q2_2 Q2_3 Q3_1 Q3_2 Q3_3 Q4_1 Q4_2 Q4_3 Q5_1 Q5_2 Q5_3 Q6_1 Q6_2 Q6_3 Q7_1 Q7_2 Q7_3 Q8_1 Q8_2 Q8_3
# 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
Use strsplit
instead of read.csv
. Add some lapply
loops and you are all set.
DF <- read.table(text=" 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,,", header=TRUE)
DF2 <- do.call(cbind.data.frame, lapply(DF, function(x) {
res <- strsplit(x, ",")
res <- lapply(res, as.numeric)
res <- do.call(rbind, res)
res[is.na(res)] <- 0
res
}))
# Q1.1 Q1.2 Q1.3 Q2.1 Q2.2 Q2.3 Q3.1 Q3.2 Q4.1 Q4.2 Q5.1 Q5.2 Q5.3 Q6.1 Q6.2 Q6.3 Q7.1 Q7.2 Q8.1 Q8.2
#1 0 0 1 0 0 1 0 1 1 0 0 0 1 0 0 1 0 1 1 0
#2 0 0 1 0 0 1 0 1 1 0 0 0 1 0 0 1 0 1 1 0
#3 0 0 1 0 0 1 0 1 1 0 0 0 1 0 0 1 0 1 1 0
#4 0 0 1 0 0 1 0 1 1 0 0 0 1 0 0 1 0 1 1 0
#5 0 0 1 0 0 1 0 1 1 0 0 0 1 0 0 1 0 1 1 0
#6 0 0 1 0 0 1 0 1 1 0 0 0 1 0 0 1 0 1 1 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.