Let's say I want to split a string column to separate columns. For that I use mutate and str_match (or str_replace) from stringr package, but the result is not as wanted.
Set up data frame and split the columns:
df <-
data.frame(strings = c('a_b_c', 'ab_cd_ef', 'abc_def_ghi')) %>%
mutate(string = stringr::str_match(strings, '([a-z]+)_([a-z]+)_([a-z]+)'))
df
strings string.1 string.2 string.3 string.4
1 a_b_c a_b_c a b c
2 ab_cd_ef ab_cd_ef ab cd ef
3 abc_def_ghi abc_def_ghi abc def ghi
When looking the the column names I only see two columns. This also makes it complicated to refer the columns. I assume it has something to do with the matrix format of the output from the str_match function.
df %>% ncol
[1] 2
df %>% colnames
[1] "strings" "string"
Is there an easy way how to have this new columns behaving like normal data.frame columns? If possible, with a renaming step. This is something I want to have:
df %>% ncol
[1] 5
df %>% colnames
[1] "strings" "string_1" "string_2" "string_3" "string_4"
df
strings string_1 string_2 string_3 string_4
1 a_b_c a_b_c a b c
2 ab_cd_ef ab_cd_ef ab cd ef
3 abc_def_ghi abc_def_ghi abc def ghi