Reputation: 39
I have conducted a survey with limesurvey and have exported the results as csv.-file, which I import into R.
One of the questions is a multiple choice question, in which the participants could name the subjects they study. The output from limesurvey looks somewhat like this (but with more subjects and more participants):
Participant | Maths | Physics | English | Biology
1 | Y | | Y |
2 | | Y | Y |
3 | Y | Y | | Y
I'd like to get a result that looks like this
Participant | Subject 1 | Subject 2| Subject 3 |
1 | Maths | English | |
2 | Physics | English | |
3 | Maths | Physics | Biology |
I'd be grateful for any pointers.
Upvotes: 0
Views: 326
Reputation: 346
Here's my attempt to generate the expected dataframe as requested:
library(tidyverse)
library(gtools)
rand_list = c('Y', NA)
df = data.frame(participant = seq(1,10, by = 1), # r starts counting from 0
Maths = sample(rand_list, 10, replace = TRUE),
Physics = sample(rand_list, 10, replace = TRUE),
English = sample(rand_list, 10, replace = TRUE),
Biology = sample(rand_list, 10, replace = TRUE))
df_to_new_format = function(data){
vector_subject = colnames(data)
vector_new_col = c()
for (i in 1:length(vector_subject)){
if (i == 1){
new_col = 'participant'
vector_new_col <- c(vector_new_col, new_col)
rm(new_col)
} else{
new_col = paste('Subject', as.character(i - 1))
vector_new_col <- c(vector_new_col, new_col)
rm(new_col)
}
}
for (j in 1:length(vector_subject)){
if (j == 1){
next
} else{
data[[j]] <- recode(data[[j]], 'Y' = vector_subject[j])
}
}
colnames(data) <- vector_new_col
return(data)
}
df = df_to_new_format(data = df)
df_new_format = c()
for (m in 1:nrow(df)){
temp = mixedsort(as.matrix(df[m,]))
print(temp)
df_new_format = rbind(df_new_format, temp)
}
df_new_format = as.data.frame(df_new_format, row.names = FALSE)
colnames(df_new_format) = colnames(df)
Upvotes: 2
Reputation: 8836
I'm a little out of practice with this kind of data wrangling, but here's a few suggestions.
First let's assume your data is of this form:
dtf <- structure(list(Participant = c("1", "2", "3", "4"),
Physics = c("Y", "Y", "N", "N"), Chemistry = c("Y", "N", "N",
"N"), Math = c("N", "Y", "Y", "Y"), Biology = c("N", "Y", "N",
"Y")), class = "data.frame", row.names = c(NA, -4L))
Then we can rearrange things like this
wh <- which(dtf == "Y", arr.ind=TRUE)
tapply(wh[,2], wh[,1], function(x) colnames(dtf)[x])
# $`1`
# [1] "Physics" "Chemistry"
# $`2`
# [1] "Physics" "Math" "Biology"
# $`3`
# [1] "Math"
# $`4`
# [1] "Math" "Biology"
Or
dtf2 <- dtf[1]
dtf2$Subject <- apply(dtf, 1, function(r) {c(names(dtf)[r == "Y"])})
dtf2
# Participant Subject
# 1 1 Physics, Chemistry
# 2 2 Physics, Math, Biology
# 3 3 Math
# 4 4 Math, Biology
Or using melt()
and dcast()
from reshape2
library(reshape2)
dtf.m <- melt(dtf, 1)
dcast(dtf.m[dtf.m$value == "Y", 1:2], Participant ~ variable)
# Participant Physics Chemistry Math Biology
# 1 1 Physics Chemistry <NA> <NA>
# 2 2 Physics <NA> Math Biology
# 3 3 <NA> <NA> Math <NA>
# 4 4 <NA> <NA> Math Biology
Upvotes: 1