I've survey results (categorical) stored in csv file with multiple responses within the same cell. I'd like to split it into separate column (dummy variables)
The data looks like
response <-c(1,2,3,123)
df <-data.frame(response)
I tried the code below
for(t in unique(df$response))
{df[paste("response",t,sep="")] <- ifelse(df$response==t,1,0)}
the result is here, but it created a new column for 123
head(df)
response response1 response2 response3 response123
1 1 1 0 0 0
2 2 0 1 0 0
3 3 0 0 1 0
4 123 0 0 0 1
I'd like the data to look as below
response response1 response2 response3
1 1 1 0 0
2 2 0 1 0
3 3 0 0 1
4 123 1 1 1
Appreciate your help and advice :)