Home > Mobile >  Split each string into multiple strings in a vector
Split each string into multiple strings in a vector

Time:05-16

fruits <- c("apple", "orange", "pear")
df <- data.frame(string = c("appleorange",
                        "orangepear",
                        "applepear"))

Desired outcome:

string
appleorange apple orange
orangepear orange pear
applepear apple pear

CodePudding user response:

Here is one approach using regex along with sub:

regex <- paste0("(?:", paste(fruits, collapse="|"), ")")
df$col1 <- sub(paste0(regex, "$"), "", df$string)
df$col2 <- sub(paste0("^", regex), "", df$string)
df

       string   col1   col2
1 appleorange  apple orange
2  orangepear orange   pear
3   applepear  apple   pear

Data:

fruits <- c("apple", "orange", "pear")
df <- data.frame(string = c("appleorange", "orangepear", "applepear"))

CodePudding user response:

Here is a solution using stringr package:

library(dplyr)
library(stringr)

df %>%
  mutate(col1 = str_extract(string, paste(fruits, collapse = '|')),
         col2 = str_replace(string, col1, ''))
       string   col1   col2
1 appleorange  apple orange
2  orangepear orange   pear
3   applepear  apple   pear

CodePudding user response:

Using separate

library(dplyr)
library(stringr)
library(tidyr)
separate(df, string, into = c("col1", "col2"), 
   sep = glue::glue("(?<=[a-z])(?={str_c(fruits, collapse='|')})"), remove = FALSE)
       string   col1   col2
1 appleorange  apple orange
2  orangepear orange   pear
3   applepear  apple   pear
  • Related