0
votes

I have a dataframe with multiple columns. The first column contains a unique identifier for a particular respondent; columns X1:n represent survey items where respondents are asked factual questions. A blank value signifies questions they answered correctly. Cells with character letters signify the incorrect option they had given (i.e., A through D). A simple example of the dataframe would be:

d <- data.frame('Name'=c("Res1","Res2","Res3"),'1'=c("A","","A"),'2'=c("","B",""), '3'=c("","C","E"), '4' = c("D","D",""))

For each row in the dataframe, I want to paste the questions they answered incorrectly with the corresponding question and selected answer into a single string variable. I can use this variable to provide feedback to participants regarding which questions they missed and their incorrect response.

I had originally tried the following using PASTE:

test4<- within(d, id<-paste(names(d[,2:5]), d[,2:5] ,sep = ".",collapse=","))

However, it didn't work.

What I ultimately want attached to the end of each row is a character string like this for row one (and for each row after}:

"1.A 4.D"

2

2 Answers

2
votes

A dplyr/tidyr approach that gets the strings back as a column in d:

library(dplyr)
library(tidyr)
d <- data.frame('Name'=c("Res1","Res2","Res3"),
                '1'=c("A","","A"),
                '2'=c("","B",""), 
                '3'=c("","C","E"), 
                '4' = c("D","D",""))


gather(d, question, response, -Name) %>%
  filter(response != "") %>%
  mutate(incorrect_string = sprintf("%s.%s",
                                    question, response)) %>%
  group_by(Name) %>%
  summarise(incorrect_string = paste0(incorrect_string, collapse = ", ")) %>%
  left_join(d, ., by = c("Name" = "Name"))
1
votes

Here is a solution, maybe not elegant... With your d:

answers= apply(d,1,FUN=function(row){paste(paste(as.character(1:(ncol(d)-1)),row[-1],sep=".")[row[-1]!=""],collapse=' ')})
out=data.frame(corrects=answers)
rownames(out) = d[,"Name"]
out


    corrects
Res1     1.A 4.D
Res2 2.B 3.C 4.D
Res3     1.A 3.E