celestialgod

move to left

Sep 12th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.46 KB | None | 0 0
  1. library(data.table)
  2. library(plyr)
  3. library(dplyr)
  4. library(magrittr)
  5.  
  6. dt = fread('No, A, B, C, D, E, F, G
  7. 1, 1, 3, 0, 0, 4, 2, 1
  8. 2, 1, 0, 0, 0, 2, 0, 3
  9. 3, 0, 1, 0, 3, 0, 0, 0
  10. 4, 1, 3, 2, 1, 0, 0, 0
  11. 5, 0, 6, 0, 0, 0, 0, 0
  12. 6, 0, 0, 0, 0, 2, 2, 1
  13. ')
  14. dt %>% mutate(id=1:nrow(.)) %>% split(.$id) %>%
  15.   llply(function(v){
  16.     v = unlist(v)[1:(length(v)-1)]
  17.     data.table(matrix(c(v[v!=0], rep(0, sum(v==0))), 1))
  18.   }) %>% rbindlist %>% setnames(names(dt))
Advertisement
Add Comment
Please, Sign In to add comment