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
- ')
- 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 %>% {gsub("0", "", .)}
- tmp = gsub(",$", "", gsub("([^,]+)(,+)", "\\1,", txt))
- maxNumComma = nchar(txt) - nchar(gsub(",", "", txt))
- numCommaEach = nchar(tmp) - nchar(gsub(",", "", tmp))
- txt = sapply(maxNumComma - numCommaEach, function(x){
- if (x > 0)
- return(paste0(rep(",", x), 0, collapse=""))
- else
- return("")
- }) %>% paste0(tmp, .)
- dt = fread(paste(txt, collapse = "\n")) %>% setnames(old_names)
Advertisement
Add Comment
Please, Sign In to add comment