In a survey, there was a question that asked "what aspect of the course helped you learn concepts the most? Select all that apply"
Here is what the list of responses looked like:
Student_ID = c(1,2,3)
Responses = c("lectures,tutorials","tutorials,assignments,lectures", "assignments,presentations,tutorials")
Grades = c(1.1,1.2,1.3)
Data = data.frame(Student_ID,Responses,Grades);Data
Student_ID | Responses | Grades
1 | lectures,tutorials | 1.1
2 | tutorials,assignments,lectures | 1.2
3 | assignments,presentations,tutorials | 1.3
Now I want to create a data frame that looks something like this
Student_ID | Lectures | Tutorials | Assignments | Presentation | Grades
1 | 1 | 1 | 0 | 0 | 1.3
2 | 1 | 1 | 1 | 0 | 1.4
3 | 0 | 1 | 1 | 1 | 1.3
I managed to separate the comma separated responses into columns, using the splitstackshape package. So currently my data looks like this:
Student ID | Response 1 | Response 2 | Response 3 | Response 4 | Grades
1 | lectures | tutorials | NA | NA | 1.1
2 | tutorials | assignments | lectures | NA | 1.2
3 | assignments| presentation| tutorials | NA | 1.3
But as I stated earlier, I would like my table to look like the way I presented above, in dummy codes. I am stuck on how to proceed. Perhaps an idea is to go through each observation in the columns and append 1 or 0 to a new data frame with lectures,tutorials,assignments,presentation as the headers?
First the Response
column is converted from factor to character class. Each element of that column is then split on comma. I don't know what all the possible responses are, so I used all that are present. Next the split Response
column is tabulated, specifying the possible levels. The resulting list is converted into a matrix before being mixed into the old data.frame.
Data$Responses <- as.character(Data$Responses)
resp.split <- strsplit(Data$Responses, ",")
lev <- unique(unlist(resp.split))
resp.dummy <- lapply(resp.split, function(x) table(factor(x, levels=lev)))
Data2 <- with(Data, data.frame(Student_ID, do.call(rbind, resp.dummy), Grades))
Data2
# Student_ID lectures tutorials assignments presentations Grades
# 1 1 1 1 0 0 1.1
# 2 2 1 1 1 0 1.2
# 3 3 0 1 1 1 1.3