0

Here is an example dataframe:

df <- data.frame(col1 = c('x: 1', 'y: 2'))

I would like to split 'col1' by the colon and store each token in a separate column. The following is how the output should look like:

col1 , col2

x , 1

y , 2

the line below is doing the split but how to store each token in it's own column:

df %>% mutate(x = str_split(col1,': ',2))
ronencozen
  • 1,991
  • 1
  • 15
  • 26
  • It is not clear how to store each token in it's own column. – ronencozen Aug 28 '17 at 18:34
  • What's not clear? Almost all the answers there show how to do that. – Rich Scriven Aug 28 '17 at 18:38
  • @ronencozen Try e.g. `splitstackshape::cSplit(df, 1, ":")`, which might need `install.packages("splitstackshape")` before. – lukeA Aug 28 '17 at 18:41
  • Thanks, it works, but i am looking for an elegant solution, could be that i will end up using tidyr::separate, and simply ignore the warning, in the case of more than two tokens. – ronencozen Aug 28 '17 at 19:06
  • In base R - `strcapture("([a-z]+): (\\d+)", as.character(df$col1), data.frame(col1 = character(), col2 = integer()))` – Rich Scriven Aug 28 '17 at 19:08
  • Interesting approach, i guess it could turn into a more generic one by adapting the patterns, thank you! – ronencozen Aug 28 '17 at 19:19

0 Answers0