Philipp_Kats
Philipp_Kats

Reputation: 4212

R: split dataframe by multiple answers

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...

Upvotes: 2

Views: 148

Answers (2)

David Arenburg
David Arenburg

Reputation: 92282

Another similar solution would be

library(splitstackshape)
cSplit(df, "q1", sep = ";", direction = "long")
#         q1   q2
# 1: answer1 1986
# 2: answer2 1986
# 3: ansert4 1986

Upvotes: 6

Colonel Beauvel
Colonel Beauvel

Reputation: 31161

You can do this with data.table package and strsplit function:

library(data.table)
setDT(df)[,setNames(strsplit(q1, ';'),'q1'),by=q2]
#     q2       q1
#1: 1986  answer1
#2: 1986  answer2
#3: 1986 ansert4 

Data:

df = structure(list(q1 = "answer1;answer2;ansert4 ", q2 = 1986L), .Names = c("q1", 
"q2"), class = "data.frame", row.names = c(NA, -1L))

Upvotes: 3

Related Questions