Guest User

Untitled

a guest
Feb 22nd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. set.seed(1)
  2. n=20
  3. df <- data.frame(s1 = paste(sample(0:3, n, replace = TRUE),sample(0:3, n, replace = TRUE),sep="/"),
  4. s2 = paste(sample(0:3, n, replace = TRUE),sample(0:3, n, replace = TRUE),sep="/"),
  5. s3 = paste(sample(0:3, n, replace = TRUE),sample(0:3, n, replace = TRUE),sep="/"),
  6. stringsAsFactors = FALSE)
  7.  
  8. library(parallel)
  9. split.mat = do.call(rbind,mclapply(1:nrow(df), function(x) {
  10. mat = sapply(df[x,1:ncol(df)], function(y) strsplit(y, split = "\/")[[1]])
  11. return(c(mat[1,],mat[2,]))
  12. }, mc.core = 10))
  13.  
  14. library(data.table)
  15. fwrite(df, sep = "/", quote = FALSE,
  16. col.names = FALSE, file = "df.txt")
  17.  
  18. NN <- 2L*ncol(df)
  19.  
  20. DT1 <- fread("df.txt", sep = "/", select = seq(from = 1L, to = NN, by = 2L))
  21. DT2 <- fread("df.txt", sep = "/", select = seq(from = 2L, to = NN, by = 2L))
  22.  
  23. options(stringsAsFactors=F)
  24. library(rbenchmark)
  25. library(stringi)
  26. library(tidyr)
  27.  
  28. set.seed(1)
  29. ncols <- 1
  30. nrows <- 10*1000
  31. strdat <- paste(sample(0:3, nrows*ncols, replace=T),
  32. sample(0:3, nrows*ncols, replace=T), sep="/")
  33.  
  34. benchmark(strsplitMtd=lapply(strdat, function(x) strsplit(x,"/")[[1]]),
  35. striMtd=stri_list2matrix(stri_split_fixed(strdat, "/"), byrow=T),
  36. tidyrMtd=separate(data.frame(S=strdat), S, c("S1","S2"), "/"))
Add Comment
Please, Sign In to add comment