Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(plyr)
- library(dplyr)
- library(magrittr)
- set.seed(100)
- numRows = 20
- numCols = 10
- DT = raply(numCols, sample(c(NA, 1:10), numRows, TRUE, prob = c(.1, rep(0.09, 10)))) %>%
- t %>% data.table %>% tbl_dt(FALSE)
- # Source: local data table [20 x 10]
- #
- # V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
- # (int) (int) (int) (int) (int) (int) (int) (int) (int) (int)
- # 1 4 6 4 6 5 4 5 8 9 4
- # 2 7 8 10 8 3 5 5 NA 3 8
- # 3 3 6 9 1 1 NA 6 2 6 1
- # 4 NA 9 10 8 1 4 3 10 2 8
- # 5 6 5 3 5 NA 3 4 2 NA 2
- # 6 6 2 6 4 3 8 NA 8 2 4
- # 7 9 9 9 5 9 1 1 1 NA 2
- # 8 5 10 10 5 7 8 1 NA 8 2
- # 9 6 6 7 7 4 NA 3 NA 9 4
- # 10 2 7 4 8 9 6 2 7 10 10
- # 11 3 6 4 5 10 10 7 6 5 1
- # 12 10 1 7 4 7 9 10 1 5 5
- # 13 4 4 7 3 4 NA 9 2 3 7
- # 14 5 1 7 1 5 7 6 2 10 1
- # 15 9 8 3 8 10 1 3 3 9 8
- # 16 8 10 7 3 5 NA NA 9 4 4
- # 17 7 2 2 10 6 1 6 10 1 2
- # 18 4 3 7 9 2 8 NA 9 8 3
- # 19 4 1 3 10 NA 7 6 NA 5 2
- # 20 8 2 7 NA 9 10 3 8 3 9
- ntile_f = function(x){
- x[!is.na(x)] %<>% ntile(10)
- return(x)
- }
- names(DT)[laply(DT, function(x) any(is.na(x)))] %>% {
- colNames = paste0(., '.ntile')
- colwise(ntile_f, .)(DT) %>% setnames(colNames)
- } %>% bind_cols(DT, .)
- # Source: local data frame [20 x 17]
- #
- # V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V1.ntile V4.ntile V5.ntile V6.ntile V7.ntile V8.ntile
- # (int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl)
- # 1 4 6 4 6 5 4 5 8 9 4 2 6 4 3 5 6
- # 2 7 8 10 8 3 5 5 NA 3 8 7 7 2 4 6 NA
- # 3 3 6 9 1 1 NA 6 2 6 1 1 1 1 NA 6 2
- # 4 NA 9 10 8 1 4 3 10 2 8 NA 7 1 4 2 9
- # 5 6 5 3 5 NA 3 4 2 NA 2 5 4 NA 2 5 2
- # 6 6 2 6 4 3 8 NA 8 2 4 6 3 3 7 NA 7
- # 7 9 9 9 5 9 1 1 1 NA 2 9 4 8 1 1 1
- # 8 5 10 10 5 7 8 1 NA 8 2 4 5 7 7 1 NA
- # 9 6 6 7 7 4 NA 3 NA 9 4 6 6 3 NA 3 NA
- # 10 2 7 4 8 9 6 2 7 10 10 1 8 8 5 2 6
- # 11 3 6 4 5 10 10 7 6 5 1 2 5 9 9 9 5
- # 12 10 1 7 4 7 9 10 1 5 5 10 3 7 9 10 1
- # 13 4 4 7 3 4 NA 9 2 3 7 3 2 4 NA 9 3
- # 14 5 1 7 1 5 7 6 2 10 1 5 1 5 6 7 4
- # 15 9 8 3 8 10 1 3 3 9 8 9 8 10 1 3 4
- # 16 8 10 7 3 5 NA NA 9 4 4 8 2 6 NA NA 8
- # 17 7 2 2 10 6 1 6 10 1 2 7 9 6 2 8 10
- # 18 4 3 7 9 2 8 NA 9 8 3 3 9 2 8 NA 9
- # 19 4 1 3 10 NA 7 6 NA 5 2 4 10 NA 6 8 NA
- # 20 8 2 7 NA 9 10 3 8 3 9 8 NA 9 10 4 7
- # Variables not shown: V9.ntile (dbl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement