Advertisement
celestialgod

shift rows

Jul 12th, 2015
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.80 KB | None | 0 0
  1. library(data.table)
  2. library(plyr)
  3. library(dplyr)
  4. library(magrittr)
  5.  
  6. (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,'')))
  7. old_names = names(dt)
  8. dt %<>% setnames(paste0("V", 1:ncol(dt)))
  9. txt = dt %>% mutate_(Vcobime = paste0("paste(", paste0("V", 1:ncol(dt),
  10.   collapse = ","), ", sep = \",\")")) %>% .$Vcobime
  11. loc = which(substr(txt, sapply(txt, nchar), sapply(txt, nchar)) == ",")
  12. txt[loc] = gsub("(([^,]+,)*)(,*)", "\\3\\1", txt[loc]) %>%
  13.   substr(1, sapply(.,nchar)-1) %>% paste0(",", .)
  14. dt = fread(paste(txt, collapse = "\n")) %>% setnames(old_names)
  15.  
  16. ## test
  17. set.seed(100)
  18. n_cols = 100
  19. n_rows = 2e5
  20. rightShift = sample(0:39, n_rows, TRUE)
  21. m = matrix(rbinom(n_cols*n_rows, 10, 0.5), n_rows)
  22. dt = t(sapply(1:n_rows, function(i){
  23.   c(as.character(m[i,1:(n_cols-rightShift[i])]), rep('', rightShift[i]))
  24. })) %>% data.table            
  25.  
  26. st = proc.time()
  27. txt = dt %>% mutate_(Vcobime = paste0("paste(", paste0("V", 1:ncol(dt),
  28.   collapse = ","), ", sep = \",\")")) %>% .$Vcobime
  29. loc = which(substr(txt, sapply(txt, nchar), sapply(txt, nchar)) == ",")
  30. txt[loc] = gsub("(([^,]+,)*)(,*)", "\\3\\1", txt[loc]) %>%
  31.   substr(1, sapply(.,nchar)-1) %>% paste0(",", .)
  32. res = fread(paste(txt, collapse = "\n"))
  33. proc.time() - st
  34. #   user  system elapsed
  35. #   7.97    0.16    8.14
  36.  
  37. st = proc.time()
  38. dtT = data.table(t(dt))
  39. len = nrow(dtT)
  40. fun <-function(x){
  41.   idx <- len-length(which(x!=''))
  42.   if (idx !=0) {
  43.     res <- rbind(array(rep('',idx),c(idx,1)), as.matrix(x[which(x!='')],ncol=1))
  44.   } else {
  45.     res <- as.matrix(x[which(x!='')],ncol=1)
  46.   }
  47.   res
  48. }
  49. res2 = dtT[,lapply(.SD,fun)]
  50. res2 = data.table(t(res2))      
  51. proc.time() - st
  52. #   user  system elapsed
  53. # 218.23    0.64  220.40
  54.  
  55. all.equal(res, colwise(as.integer)(res2) %>% data.table) # TRUE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement