celestialgod

category split

Apr 17th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.78 KB | None | 0 0
  1. library(magrittr)
  2. library(plyr)
  3. library(dplyr)
  4.  
  5. set.seed(100)
  6. numCate <- 5000
  7. N <- 1e6
  8. cates <- sample(paste0("A", 1:numCate), N, TRUE)
  9. time <- sample(1:300, N, TRUE)
  10. dat <- data.frame(cates, time, stringsAsFactors = FALSE) %>% tbl_df
  11. st <- proc.time()
  12. dat_out <- ddply(dat, .(cates), function(sub_df){
  13.   sub_df %>% arrange(time) %>% filter(diff(c(0, time)) > 1)
  14. }) %>% tbl_df
  15. proc.time() - st
  16. # user  system elapsed
  17. # 6.13    0.00    6.21
  18.  
  19. library(foreach)
  20. library(doSNOW)
  21. cl <- makeCluster(8, type = "SOCK")
  22. registerDoSNOW(cl)
  23.  
  24. st <- proc.time()
  25. dat_out <- ddply(dat, .(cates), function(sub_df){
  26.   sub_df %>% arrange(time) %>% filter(diff(c(0, time)) > 1)
  27. }, .parallel=TRUE) %>% tbl_df
  28. proc.time() - st
  29. # user  system elapsed
  30. # 2.85    0.58    4.32
  31. stopCluster(cl)
Advertisement
Add Comment
Please, Sign In to add comment