Here you can use strsplit:
do.call(rbind,
lapply(
strsplit(x," "),
function(y)
cbind(paste(head(y,length(y)-1),collapse=" "),tail(y,1))
)
)
Or an alternative implementation using sapply
t(
sapply(
strsplit(x," "),
function(y) cbind(paste(head(y,length(y)-1),collapse=" "),tail(y,1))
)
)
Result:
[,1] [,2]
[1,] "This is a" "test"
[2,] "Testing 1,2,3" "Hello"
[3,] "Foo" "Bar"
[4,] "Random 214274(%*(^(*" "Sample"
[5,] "Some" "Hyphenated-Thing"
source
share