Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(plyr)
- library(dplyr)
- library(magrittr)
- (dt <- data.table(x=1:6,b=c(2,'',4,5,6,7),c(1,'','',4,5,6),c(1,'','',4,5,''),c('','','',4,5,'')))
- old_names = names(dt)
- dt %<>% setnames(paste0("V", 1:ncol(dt)))
- txt = dt %>% mutate_(Vcobime = paste0("paste(", paste0("V", 1:ncol(dt),
- collapse = ","), ", sep = \",\")")) %>% .$Vcobime
- loc = which(substr(txt, sapply(txt, nchar), sapply(txt, nchar)) == ",")
- txt[loc] = gsub("(([^,]+,)*)(,*)", "\\3\\1", txt[loc]) %>%
- substr(1, sapply(.,nchar)-1) %>% paste0(",", .)
- dt = fread(paste(txt, collapse = "\n")) %>% setnames(old_names)
- ## test
- set.seed(100)
- n_cols = 100
- n_rows = 2e5
- rightShift = sample(0:39, n_rows, TRUE)
- m = matrix(rbinom(n_cols*n_rows, 10, 0.5), n_rows)
- dt = t(sapply(1:n_rows, function(i){
- c(as.character(m[i,1:(n_cols-rightShift[i])]), rep('', rightShift[i]))
- })) %>% data.table
- st = proc.time()
- txt = dt %>% mutate_(Vcobime = paste0("paste(", paste0("V", 1:ncol(dt),
- collapse = ","), ", sep = \",\")")) %>% .$Vcobime
- loc = which(substr(txt, sapply(txt, nchar), sapply(txt, nchar)) == ",")
- txt[loc] = gsub("(([^,]+,)*)(,*)", "\\3\\1", txt[loc]) %>%
- substr(1, sapply(.,nchar)-1) %>% paste0(",", .)
- res = fread(paste(txt, collapse = "\n"))
- proc.time() - st
- # user system elapsed
- # 7.97 0.16 8.14
- st = proc.time()
- dtT = data.table(t(dt))
- len = nrow(dtT)
- fun <-function(x){
- idx <- len-length(which(x!=''))
- if (idx !=0) {
- res <- rbind(array(rep('',idx),c(idx,1)), as.matrix(x[which(x!='')],ncol=1))
- } else {
- res <- as.matrix(x[which(x!='')],ncol=1)
- }
- res
- }
- res2 = dtT[,lapply(.SD,fun)]
- res2 = data.table(t(res2))
- proc.time() - st
- # user system elapsed
- # 218.23 0.64 220.40
- all.equal(res, colwise(as.integer)(res2) %>% data.table) # TRUE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement