1

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])

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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"

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.