Advertisement
celestialgod

ntile calculation (skip NA operation)

Jan 5th, 2016
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.58 KB | None | 0 0
  1. library(data.table)
  2. library(plyr)
  3. library(dplyr)
  4. library(magrittr)
  5.  
  6. set.seed(100)
  7. numRows = 20
  8. numCols = 10
  9. DT = raply(numCols, sample(c(NA, 1:10), numRows, TRUE, prob = c(.1, rep(0.09, 10)))) %>%
  10.   t %>% data.table %>% tbl_dt(FALSE)
  11. # Source: local data table [20 x 10]
  12. #
  13. #       V1    V2    V3    V4    V5    V6    V7    V8    V9   V10
  14. #    (int) (int) (int) (int) (int) (int) (int) (int) (int) (int)
  15. # 1      4     6     4     6     5     4     5     8     9     4
  16. # 2      7     8    10     8     3     5     5    NA     3     8
  17. # 3      3     6     9     1     1    NA     6     2     6     1
  18. # 4     NA     9    10     8     1     4     3    10     2     8
  19. # 5      6     5     3     5    NA     3     4     2    NA     2
  20. # 6      6     2     6     4     3     8    NA     8     2     4
  21. # 7      9     9     9     5     9     1     1     1    NA     2
  22. # 8      5    10    10     5     7     8     1    NA     8     2
  23. # 9      6     6     7     7     4    NA     3    NA     9     4
  24. # 10     2     7     4     8     9     6     2     7    10    10
  25. # 11     3     6     4     5    10    10     7     6     5     1
  26. # 12    10     1     7     4     7     9    10     1     5     5
  27. # 13     4     4     7     3     4    NA     9     2     3     7
  28. # 14     5     1     7     1     5     7     6     2    10     1
  29. # 15     9     8     3     8    10     1     3     3     9     8
  30. # 16     8    10     7     3     5    NA    NA     9     4     4
  31. # 17     7     2     2    10     6     1     6    10     1     2
  32. # 18     4     3     7     9     2     8    NA     9     8     3
  33. # 19     4     1     3    10    NA     7     6    NA     5     2
  34. # 20     8     2     7    NA     9    10     3     8     3     9
  35. ntile_f = function(x){
  36.   x[!is.na(x)] %<>% ntile(10)
  37.   return(x)
  38. }
  39. names(DT)[laply(DT, function(x) any(is.na(x)))] %>% {
  40.   colNames = paste0(., '.ntile')
  41.   colwise(ntile_f, .)(DT) %>% setnames(colNames)
  42.   } %>% bind_cols(DT, .)
  43. # Source: local data frame [20 x 17]
  44. #
  45. #       V1    V2    V3    V4    V5    V6    V7    V8    V9   V10 V1.ntile V4.ntile V5.ntile V6.ntile V7.ntile V8.ntile
  46. #    (int) (int) (int) (int) (int) (int) (int) (int) (int) (int)    (dbl)    (dbl)    (dbl)    (dbl)    (dbl)    (dbl)
  47. # 1      4     6     4     6     5     4     5     8     9     4        2        6        4        3        5        6
  48. # 2      7     8    10     8     3     5     5    NA     3     8        7        7        2        4        6       NA
  49. # 3      3     6     9     1     1    NA     6     2     6     1        1        1        1       NA        6        2
  50. # 4     NA     9    10     8     1     4     3    10     2     8       NA        7        1        4        2        9
  51. # 5      6     5     3     5    NA     3     4     2    NA     2        5        4       NA        2        5        2
  52. # 6      6     2     6     4     3     8    NA     8     2     4        6        3        3        7       NA        7
  53. # 7      9     9     9     5     9     1     1     1    NA     2        9        4        8        1        1        1
  54. # 8      5    10    10     5     7     8     1    NA     8     2        4        5        7        7        1       NA
  55. # 9      6     6     7     7     4    NA     3    NA     9     4        6        6        3       NA        3       NA
  56. # 10     2     7     4     8     9     6     2     7    10    10        1        8        8        5        2        6
  57. # 11     3     6     4     5    10    10     7     6     5     1        2        5        9        9        9        5
  58. # 12    10     1     7     4     7     9    10     1     5     5       10        3        7        9       10        1
  59. # 13     4     4     7     3     4    NA     9     2     3     7        3        2        4       NA        9        3
  60. # 14     5     1     7     1     5     7     6     2    10     1        5        1        5        6        7        4
  61. # 15     9     8     3     8    10     1     3     3     9     8        9        8       10        1        3        4
  62. # 16     8    10     7     3     5    NA    NA     9     4     4        8        2        6       NA       NA        8
  63. # 17     7     2     2    10     6     1     6    10     1     2        7        9        6        2        8       10
  64. # 18     4     3     7     9     2     8    NA     9     8     3        3        9        2        8       NA        9
  65. # 19     4     1     3    10    NA     7     6    NA     5     2        4       10       NA        6        8       NA
  66. # 20     8     2     7    NA     9    10     3     8     3     9        8       NA        9       10        4        7
  67. # Variables not shown: V9.ntile (dbl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement