0

Having a data structure into the comma separated format:

dframe = data.frame(id=c(1,2,43,53), title=c("text1,color","color,text2","text2","text3,text2"))

To convert it as a Boolean vector with exist or not in every row like this expected output:

dframe = data.frame(id=c(1,2,43,53), text1=c(1,0,0,0), color=c(1,1,0,0), text2=c(0,1,1,1), text3=c(0,0,0,1))
Elr Mant
  • 507
  • 1
  • 4
  • 14
  • @Sotos it is not the same with your answer in the duplicate example as the structure of the dataframe is different – Elr Mant Mar 12 '19 at 22:40

1 Answers1

1

We can use separate_rows and spread from tidyverse:

library(tidyverse)
dframe %>%
  separate_rows(title, sep = ",") %>%
  mutate(id2 = 1) %>%
  spread(title, id2, fill = 0)

Output:

# A tibble: 4 x 5
# Groups:   id [4]
     id color text1 text2 text3
  <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     1     1     0     0
2     2     1     0     1     0
3    43     0     0     1     0
4    53     0     0     1     1
acylam
  • 18,231
  • 5
  • 36
  • 45