1
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

3 Answers 3

3

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

Comments

3

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

Comments

1

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

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.