Home > OS >  Split column after first two characters
Split column after first two characters

Time:05-17

I want to split a column into two columns while the one column would keep the first two characters within the origin column and the new column would contain all other characters.

Something similar is also fine, as long as I can split the cell content into two columns after the first two characters.

There is no specific character to split up, just after the first two characters. Hence, it would be a char, not a string, or num. I can't find something that fits.

CodePudding user response:

# sample data
d = data.frame(x = c("a", "ab", "abc", "abcd"))

tidyr::separate(d, x, into = c("x1", "x2"), sep = 2)
#   x1 x2
# 1  a   
# 2 ab   
# 3 ab  c
# 4 ab cd

You could also use substring:

library(dplyr) # easily omitted if you prefer a base-only answer
d |> mutate(
  x1 = substring(x, start = 1, stop = 2), 
  x2 = substring(x, start = 3)
)
     x x1 x2
1    a  a   
2   ab ab   
3  abc ab  c
4 abcd ab cd

CodePudding user response:

Using tstrsplit() from data.table:

library(data.table)

d = data.frame(x = c("a", "ab", "abc", "abcd"))

d[c('x1', 'x2')] <- tstrsplit(d$x, '(?<=.{2})', perl = TRUE)

#      x x1   x2
# 1    a  a <NA>
# 2   ab ab <NA>
# 3  abc ab    c
# 4 abcd ab   cd
  •  Tags:  
  • r
  • Related