Skip to content
Prev 385760 / 398503 Next

Split

Oh, if efficiency is a consideration, then my code is about 15 times as
fast as Rui's:
##Rui's
+     F2$Y1 <- +grepl("_", F2$text)
+     tmp <- strsplit(as.character(F2$text), "_")
+     tmp <- lapply(tmp, function(x) if(length(x) == 1) c(x, ".") else x)
+     tmp <- do.call(rbind, tmp)
+     colnames(tmp) <- c("X1", "X2")
+     F2 <- cbind(F2[-3], tmp)    # remove the original column
+ })
   user  system elapsed
 20.072   0.625  20.786

## my version
+     wh <- grep("_",F2$text, fixed = TRUE, invert = TRUE)
+     F2[wh,"text"] <- paste(F2[wh,"text"],".",sep = "_")
+     z <- unlist(strsplit(F1$text,"_"))
+     F2 <- cbind(F2, matrix(z, ncol = 2, byrow = TRUE))
+     F2
+ })
   user  system elapsed
  1.256   0.019   1.281

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Sep 22, 2020 at 5:04 PM Val <valkremk at gmail.com> wrote: