Categorize numeric variable with mutate

后端 未结 2 1441
猫巷女王i
猫巷女王i 2020-11-29 03:47

I would like to a categorize numeric variable in my data.frame object with the use of dplyr (and have no idea how to do it).

Without

2条回答
  •  误落风尘
    2020-11-29 04:48

    set.seed(123)
    df <- data.frame(a = rnorm(10), b = rnorm(10))
    
    df %>% mutate(a = cut(a, breaks = quantile(a, probs = seq(0, 1, 0.2))))
    

    giving:

                     a          b
    1  (-0.586,-0.316]  1.2240818
    2   (-0.316,0.094]  0.3598138
    3      (0.68,1.72]  0.4007715
    4   (-0.316,0.094]  0.1106827
    5     (0.094,0.68] -0.5558411
    6      (0.68,1.72]  1.7869131
    7     (0.094,0.68]  0.4978505
    8              -1.9666172
    9   (-1.27,-0.586]  0.7013559
    10 (-0.586,-0.316] -0.4727914
    

提交回复
热议问题