Home > Back-end >  Splitting strings into multiple columns using str_split
Splitting strings into multiple columns using str_split

Time:08-05

i'm trying to use str_split to split this:

col
a_b_c
c_d_e

into this:

col_1 col_2 col_3
a b c
c d e

here's the code but i'm getting an error on the replacement rows not matching


  df %>%
  dplyr::mutate(
    col_1 = stringr::str_split(
      string = col, 
      pattern = "_", 
      simplify = T)[,1],
    col_2 = stringr::str_split(
      string = col, 
      pattern = "_", 
      simplify = T)[,2],
    col_3 = stringr::str_split(
      string = col, 
      pattern = "_", 
      simplify = T)[,3])

CodePudding user response:

We could use separate for this:

library(dplyr)
library(tidyr)

df %>% 
  separate(col, c("col_1", "col_2", "col_3"))

  col_1 col_2 col_3
1     a     b     c
2     c     d     e

CodePudding user response:

Using strsplit

col <- c('a_b_c', 'c_d_e')
matrix(unlist(strsplit(col, '_')), nrow=2, ncol = 3, byrow = TRUE)
     [,1] [,2] [,3]
[1,] "a"  "b"  "c" 
[2,] "c"  "d"  "e"

CodePudding user response:

Yet another solution, based on tidyr::extract:

library(tidyverse)

extract(df, col, str_c("col_", 1:3), "(.*)_(.*)_(.*)")

#>   col_1 col_2 col_3
#> 1     a     b     c
#> 2     c     d     e

CodePudding user response:

We can use

library(stringr)

data.frame(do.call(rbind , str_split(df$col , "_")))
  • output
  X1 X2 X3
1  a  b  c
2  c  d  e
  • Related