Try this approach with separate()
from tidyr
(tidyverse
):
library(tidyverse)
#Separate
df2 <- df %>% separate(col = V1,into = c('a','b','c'),sep = ';')
Output:
a b c
1 Gastroenterología clinica Endoscopia digestiva Motilidad y Neurogastro
2 Gastroenterología clinica Endoscopia digestiva Motilidad y Neurogastro
3 Gastroenterología clinica Endoscopia digestiva Motilidad y Neurogastro
4 Gastroenterología clinica Endoscopia digestiva <NA>
5 Motilidad y Neurogastro <NA> <NA>
6 Gastroenterología clinica Motilidad y Neurogastro <NA>
Some data used:
#Data
df <- structure(list(V1 = c("Gastroenterología clinica;Endoscopia digestiva;Motilidad y Neurogastro",
"Gastroenterología clinica;Endoscopia digestiva;Motilidad y Neurogastro",
"Gastroenterología clinica;Endoscopia digestiva;Motilidad y Neurogastro",
"Gastroenterología clinica;Endoscopia digestiva", "Motilidad y Neurogastro",
"Gastroenterología clinica;Motilidad y Neurogastro")), class = "data.frame", row.names = c(NA,
-6L))
Update: In order to have one variable per value here the code:
#Code
df %>% separate_rows(V1,sep=';') %>%
mutate(V=paste0('V',1:n())) %>%
pivot_wider(names_from = V,values_from=V1)
Output:
# A tibble: 1 x 14
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Gastro~ Endos~ Motili~ Gastro~ Endo~ Motil~ Gastr~ Endo~ Motil~ Gastr~ Endo~ Motil~ Gastr~ Motil~
Update 2: In order to have a variable for each class, try this:
#Code 2
df %>% mutate(id=1:n()) %>% separate_rows(V1,sep=';') %>%
#group_by(V1) %>%
mutate(var=1) %>%
pivot_wider(names_from = V1,values_from=var) %>%
replace(is.na(.),0) %>% select(-id)
Output:
# A tibble: 6 x 3
`Gastroenterología clinica` `Endoscopia digestiva` `Motilidad y Neurogastro`
<dbl> <dbl> <dbl>
1 1 1 1
2 1 1 1
3 1 1 1
4 1 1 0
5 0 0 1
6 1 0 1
And if you want the totals, try this:
#Code 3
df %>% mutate(id=1:n()) %>% separate_rows(V1,sep=';') %>%
#group_by(V1) %>%
mutate(var=1) %>%
pivot_wider(names_from = V1,values_from=var) %>% select(-id) %>%
summarise_all(.funs = sum,na.rm=T)
Output:
# A tibble: 1 x 3
`Gastroenterología clinica` `Endoscopia digestiva` `Motilidad y Neurogastro`
<dbl> <dbl> <dbl>
1 5 4 5