0

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 :)

osbwh
  • 49
  • 8

1 Answers1

1

We can do

df1 <- cbind(df, +(sapply(1:3, grepl, x = df$response)))
colnames(df1)[-1] <- paste0("response", colnames(df1)[-1])
df1
#   response response1 response2 response3
#1        1         1         0         0
#2        2         0         1         0
#3        3         0         0         1
#4      123         1         1         1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Would you please let me know if there's a way to include it in the same df? Thanks a lot! – osbwh Sep 04 '17 at 18:18
  • @Omar Then just update the `df` object i.e. `df <- cbind(df, +(sapply(1:3, grepl, x = df$response)))` – akrun Sep 04 '17 at 18:19