Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set.seed(1)
- n=20
- df <- data.frame(s1 = paste(sample(0:3, n, replace = TRUE),sample(0:3, n, replace = TRUE),sep="/"),
- s2 = paste(sample(0:3, n, replace = TRUE),sample(0:3, n, replace = TRUE),sep="/"),
- s3 = paste(sample(0:3, n, replace = TRUE),sample(0:3, n, replace = TRUE),sep="/"),
- stringsAsFactors = FALSE)
- library(parallel)
- split.mat = do.call(rbind,mclapply(1:nrow(df), function(x) {
- mat = sapply(df[x,1:ncol(df)], function(y) strsplit(y, split = "\/")[[1]])
- return(c(mat[1,],mat[2,]))
- }, mc.core = 10))
- library(data.table)
- fwrite(df, sep = "/", quote = FALSE,
- col.names = FALSE, file = "df.txt")
- NN <- 2L*ncol(df)
- DT1 <- fread("df.txt", sep = "/", select = seq(from = 1L, to = NN, by = 2L))
- DT2 <- fread("df.txt", sep = "/", select = seq(from = 2L, to = NN, by = 2L))
- options(stringsAsFactors=F)
- library(rbenchmark)
- library(stringi)
- library(tidyr)
- set.seed(1)
- ncols <- 1
- nrows <- 10*1000
- strdat <- paste(sample(0:3, nrows*ncols, replace=T),
- sample(0:3, nrows*ncols, replace=T), sep="/")
- benchmark(strsplitMtd=lapply(strdat, function(x) strsplit(x,"/")[[1]]),
- striMtd=stri_list2matrix(stri_split_fixed(strdat, "/"), byrow=T),
- tidyrMtd=separate(data.frame(S=strdat), S, c("S1","S2"), "/"))
Add Comment
Please, Sign In to add comment