Skip to content
Prev 385755 / 398503 Next

Split

Hello,

A base R solution with strsplit, like in your code.

F1$Y1 <- +grepl("_", F1$text)

tmp <- strsplit(as.character(F1$text), "_")
tmp <- lapply(tmp, function(x) if(length(x) == 1) c(x, ".") else x)
tmp <- do.call(rbind, tmp)
colnames(tmp) <- c("X1", "X2")
F1 <- cbind(F1[-3], tmp)    # remove the original column
rm(tmp)

F1
#  ID1 ID2 Y1   X1 X2
#1  A1  B1  0 NONE  .
#2  A1  B1  1   cf 12
#3  A1  B1  0 NONE  .
#4  A2  B2  1   X2 25
#5  A2  B3  1   fd 15


Note that cbind dispatches on F1, an object of class "data.frame".
Therefore it's the method cbind.data.frame that is called and the result 
is also a df, though tmp is a "matrix".


Hope this helps,

Rui Barradas


?s 20:07 de 22/09/20, Rui Barradas escreveu: