Home > Mobile >  How to split one column whith multiples delimiters in multiple columns in R?
How to split one column whith multiples delimiters in multiple columns in R?

Time:05-05

I have values ​​with the following structure: string OR string_string.interger

EX:

df<-data.frame(Objs=c("Windows","Door_XYZ.1", "Door_XYY.1", "Chair_XYYU.2" ))
Objs
Windows
Door_XYZ.1
Door_XYY.1
Chair_XYYU.2

Using the command split(), separate() or something similar I need to generate a dataframe similar to this one:

Obs: The split must be performed for the characters "_" and "."

Objs IND TAG Control
Windows NA NA NA
Door_XYZ.1 Door XYZ 1
Door_XYY.1 Door XYY 1
Chair_XYYU.2 Chair XYYU 2

The closest solution was suggested by @Tommy, in similar context.

df %>%  data.frame(.,do.call(rbind,str_split(.$Objs,"_")))

CodePudding user response:

The default value of the sep argument in separate() will nearly get the result you need. A conditional mutate was also needed to remove the Windows entry from the IND column.

library(tidyverse)
df <- data.frame(Objs=c("Windows","Door_XYZ.1", "Door_XYY.1", "Chair_XYYU.2" ))

df %>% 
  separate(Objs, into = c("IND", "TAG", "Control"), remove = FALSE, fill = "right") %>% 
  mutate(IND = if_else(Objs == IND, NA_character_, IND))
#>           Objs   IND  TAG Control
#> 1      Windows  <NA> <NA>    <NA>
#> 2   Door_XYZ.1  Door  XYZ       1
#> 3   Door_XYY.1  Door  XYY       1
#> 4 Chair_XYYU.2 Chair XYYU       2

Created on 2022-05-05 by the reprex package (v1.0.0)

  • Related