serenade
serenade

Reputation: 359

How to represent items in a list as binary?

I have a data with 5 columns (ID, Q_1, Q_2, Q_3, Q_4). ID column has student numbers (122 -127). Q_1 column has student answers (Q-1_A-4, Q-1_A-12...) Q_2 column has student answers (Q-2_A-2, Q-2_A-3...) Q_3 and Q4 column has student answers (T,F)

I want to create a matrix in R. For instance; for each ID, if Q-1_A-4 is given, the matrix should include 1, if is not given, the matrix should include 0. The input sample and desired output sample is in the below.

If somebody could help me for coding this case in R, I will be happy. Thanks.

Input:

ID   Q_1        Q_2            Q_3      Q_4

122  Q-1_A-4                   F         F          
123  Q-1_A-4    Q-2_A-2        F         T  
124  Q-1_A-35   Q-2_A-2        T         F  
125  Q-1_A-14   Q-2_A-4        F         T  
126  Q-1_A-14   Q-2_A-2        F         F  
127  Q-1_A-4    Q-2_A-3        F         F  

Desired output:

ID   Q-1_A-4    Q-1_A-14   Q-1_A-35   Q-2_A-2    Q-2_A-4  .....

122  1          0              0         0         0
123  1          0              0         1         0 
124  0          0              1         1         0 
125  0          1              0         0         1  
126  0          1              0         1         0
127  1          0              0         0         0 

Upvotes: 1

Views: 58

Answers (1)

akrun
akrun

Reputation: 887501

We can use mtabulate from library(qdapTools)

library(qdapTools)
d1 <- mtabulate(as.data.frame(t(df1[-1])))
`row.names<-`(cbind(df1[1],d1[grep("^Q", names(d1))]), NULL)
#   ID Q-1_A-4 Q-2_A-2 Q-1_A-35 Q-1_A-14 Q-2_A-4 Q-2_A-3
#1 122       1       0        0        0       0       0
#2 123       1       1        0        0       0       0
#3 124       0       1        1        0       0       0
#4 125       0       0        0        1       1       0
#5 126       0       1        0        1       0       0
#6 127       1       0        0        0       0       1

data

df1 <- structure(list(ID = 122:127, Q_1 = c("Q-1_A-4", 
 "Q-1_A-4", "Q-1_A-35", 
"Q-1_A-14", "Q-1_A-14", "Q-1_A-4"), Q_2 = c("", 
"Q-2_A-2", "Q-2_A-2", 
"Q-2_A-4", "Q-2_A-2", "Q-2_A-3"), Q_3 = c(FALSE, FALSE, 
 TRUE, 
FALSE, FALSE, FALSE), Q_4 = c(FALSE, TRUE, FALSE, TRUE, FALSE, 
FALSE)), .Names = c("ID", "Q_1", "Q_2", "Q_3", "Q_4"), 
class = "data.frame", row.names = c(NA, -6L))

Upvotes: 1

Related Questions