0

Having a dataframe like this:

data.frame(id = c(1,2), text = c("Google,Amazon", "Amazon,Yahoo"), stringsAsFactors = FALSE)
#   id          text
# 1  1 Google,Amazon
# 2  2  Amazon,Yahoo

How is it possible to create from text column using as separator the comma. Example of expected output:

data.frame(id = c(1,2), Google = c(1,0), Amazon = c(1,1), Yahoo = c(0,1))
#      id Google Amazon Yahoo
# 1     1      1      1     0
# 2     2      0      1     1
Ric S
  • 9,073
  • 3
  • 25
  • 51
Nathalie
  • 1,228
  • 7
  • 20

1 Answers1

2

Using libraries dplyr and tidyr

library(dplyr)
library(tidyr)

df %>% 
  mutate(
    text = strsplit(text, ","),
    value = 1
    ) %>% 
  unnest(text) %>% 
  pivot_wider(
    id_cols = id,
    names_from = text,
    values_from = value,
    values_fill = list(value = 0)
  )

Output

# A tibble: 2 x 4
#      id Google Amazon Yahoo
#   <dbl>  <dbl>  <dbl> <dbl>
# 1     1      1      1     0
# 2     2      0      1     1
Ric S
  • 9,073
  • 3
  • 25
  • 51