Мы можем использовать extract
из tidyr
library(tidyverse)
DF %>%
extract(Text, into = c('Col1', 'Col2'), '^([^>]+) >.* > ([^>]+)$',
remove = FALSE)
# Text Col1 Col2
#1 Some text > in > a string Some text a string
#2 Another > text in > another > set of string Another set of string
Или с base R
, split
в >
, а затем получить первый и последний элемент
DF[c('Col1', 'Col2')] <- t(sapply(strsplit(DF$Text, " > "),
function(x) c(x[1], x[length(x)])))
Обновление
В обновленном наборе данных 'DF3' NAs
являются символьными строками.Мы можем преобразовать его в реальные NA
is.na(DF3$Text) <- DF3$Text == "NA"
DF3[c('Col1', 'Col2')] <- t(sapply(strsplit(DF3$Text, " > "),
function(x) c(x[1], x[length(x)])))
DF3
# Text Col1 Col2
#1 Some text > in > a string Some text a string
#2 Another > text in > another > set of string Another set of string
#3 This > is one This is one
#4 <NA> <NA> <NA>
или аналогично шаблону @ Onyambu
DF3 %>%
extract(Text, into = c("Col1", "Col2"),
"^([^>]*)>(?:.*>)?([^>]*)$", remove = FALSE)
# Text Col1 Col2
#1 Some text > in > a string Some text a string
#2 Another > text in > another > set of string Another set of string
#3 This > is one This is one
#4 <NA> <NA> <NA>
data
DF <- structure(list(Text = c("Some text > in > a string",
"Another > text in > another > set of string"
)), .Names = "Text", row.names = c(NA, -2L), class = "data.frame")
DF3 <- structure(list(Text = c("Some text > in > a string",
"Another > text in > another > set of string", "This > is one", "NA")),
.Names = "Text", row.names = c(NA, -4L), class = "data.frame")