Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(plyr)
- library(dplyr)
- library(magrittr)
- dt = fread('No, A, B, C, D, E, F, G
- 1, 1, 3, 0, 0, 4, 2, 1
- 2, 1, 0, 0, 0, 2, 0, 3
- 3, 0, 1, 0, 3, 0, 0, 0
- 4, 1, 3, 2, 1, 0, 0, 0
- 5, 0, 6, 0, 0, 0, 0, 0
- 6, 0, 0, 0, 0, 2, 2, 1
- ')
- dt %>% mutate(id=1:nrow(.)) %>% split(.$id) %>%
- llply(function(v){
- v = unlist(v)[1:(length(v)-1)]
- data.table(matrix(c(v[v!=0], rep(0, sum(v==0))), 1))
- }) %>% rbindlist %>% setnames(names(dt))
Advertisement
Add Comment
Please, Sign In to add comment