Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(microbenchmark)
- df1 <- data.frame(name = rep(LETTERS, 250), number = rep(c(1:26), 250), stringsAsFactors = FALSE)
- df1$name[df1$name == "D" | df1$name == "F" | df1$name == "L"] <- "foo*"
- df2 <- data.frame(name = rep(LETTERS, 250), number = rep(c(1:26), 250), stringsAsFactors = FALSE)
- df2$name[df2$name == "A" | df2$name == "R" | df2$name == "T"] <- "*foo*"
- df3 <- data.frame(name = rep(LETTERS, 250), number = rep(c(1:26), 250), stringsAsFactors = FALSE)
- df3$name[df3$name == "C" | df3$name == "Q" | df3$name == "W"] <- "f*oo"
- df <- list(df1, df2, df3)
- dt <- lapply(df, as.data.table)
- mapply.remove.asterisk = function(x){
- df2 <- data.frame(mapply(gsub, "\*", "", x, perl = TRUE))
- colnames(df2) <- colnames(x)
- }
- dt.remove.asterisk = function (x) {
- x[, lapply(.SD, function(x) gsub("\*", "", x, perl = TRUE))]
- }
- mapgsubtest = function(x) {
- df.test <- lapply(x, mapply.remove.asterisk)
- }
- dtgsubtest = function(x) {
- dt.test <- lapply(x, dt.remove.asterisk)
- }
- microbenchmark(mapgsubtest(df), dtgsubtest(dt), neval = 100)
- Unit: nanoseconds
- expr min lq mean median uq max neval
- mapgsubtest(df) 6698066 6886010 7081510.34 7052019 7212621 7955832 100
- dtgsubtest(dt) 6791813 6997184 7209173.80 7199550 7352039 8272226 100
- neval 0 0 3.21 0 0 301 100
Add Comment
Please, Sign In to add comment