Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: split dataframe by multiple answers

Tags:

r

So I have a dataframe with results of the survey. The thing is, I have several questions with multiple answers in cell. For example:

q1|q2
answer1;answer2;ansert4 |1986

so I need to extract 'tidy' dataset for specific analysis tasks, meaning: split those cells down in the same column and copy other specified columns so it will be like that:

q1|q2
answer1|1986
answer2|1986
answer4|1986

How can i do this in R? I am pretty sure it is a simple task, but I don't have any clue...

like image 824
Philipp_Kats Avatar asked Nov 29 '25 12:11

Philipp_Kats


1 Answers

Another similar solution would be

library(splitstackshape)
cSplit(df, "q1", sep = ";", direction = "long")
#         q1   q2
# 1: answer1 1986
# 2: answer2 1986
# 3: ansert4 1986
like image 172
David Arenburg Avatar answered Dec 02 '25 03:12

David Arenburg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!