I have demographic survey data in which responses for variable X
were recorded into 3 separate columns. col1
contains only 1, col2
only 2 and col3
contains everything else. There is no overlap of responses so when the respondent answered "1" cells for col2
and col3
in that same row are empty. I'd like to combine all 3 columns (there are 3000+ responses so doing this manually is out of the question). I should note that there are 100+ columns in this data frame and I will need to use this on other variables as well.
I attempted gather()
and apply()
commands without much luck.
(see this question for reference: r collapsing data from multiple columns into one)
The apply command combined EVERY column, so I'm guessing I just need to fix my parameters? Wasn't sure how to do this (a bit of a newbie here).
df$New <- apply(df, 1, function(x) paste(x, collapse = ","))
Thanks for any help/advice!