Specify factor levels for intervals

前端 未结 1 388
半阙折子戏
半阙折子戏 2021-01-28 12:06

I have a continuous variable that I would like to turn into a factor.

The data is:

DCA<-c(0.14,0.14,0.16,0.16,0.27,0.27,0.07,0.07,0.41,0.41,0.00,0.00         


        
1条回答
  •  执念已碎
    2021-01-28 12:48

    Look into cut:

    cut(DCA, c(-Inf, seq(0, .5, .1), Inf))
    #  [1] (0.1,0.2]  (0.1,0.2]  (0.1,0.2]  (0.1,0.2]  (0.2,0.3]  (0.2,0.3]  (0,0.1]   
    #  [8] (0,0.1]    (0.4,0.5]  (0.4,0.5]  (-Inf,0]   (-Inf,0]   (0.3,0.4]  (0.3,0.4] 
    # [15] (0.1,0.2]  (0.1,0.2]  (0.5, Inf] (0.5, Inf] (0.2,0.3]  (0.2,0.3]  (0,0.1]   
    # [22] (0,0.1]    (0.4,0.5]  (0.4,0.5]  (0.2,0.3]  (0.2,0.3]  (0,0.1]    (0,0.1]   
    # [29] (-Inf,0]   (-Inf,0]   (0.5, Inf] (0.5, Inf] (0,0.1]    (0,0.1]    (0.1,0.2] 
    # [36] (0.1,0.2]  (0.2,0.3]  (0.2,0.3]  (0.2,0.3]  (0.2,0.3]  (0.5, Inf] (0.5, Inf]
    # [43] (0.2,0.3]  (0.2,0.3]  (0,0.1]    (0,0.1]    (0,0.1]    (0,0.1]    (0.3,0.4] 
    # [50] (0.3,0.4]  (0.3,0.4]  (0.3,0.4]  (0.3,0.4]  (0.3,0.4]  (0.3,0.4]  (0.3,0.4] 
    # [57] (0.2,0.3]  (0.2,0.3]  (-Inf,0]   (-Inf,0]   (0.3,0.4]  (0.3,0.4]  (0.1,0.2] 
    # [64] (0.1,0.2]  (0.3,0.4]  (0.3,0.4]  (0.3,0.4]  (0.3,0.4]  (0,0.1]    (0,0.1]   
    # [71] (0.3,0.4]  (0.3,0.4] 
    # 7 Levels: (-Inf,0] (0,0.1] (0.1,0.2] (0.2,0.3] (0.3,0.4] ... (0.5, Inf]
    

    You may want to customize the second argument ("breaks") to represent the breaks you are actually looking for as well as look into some of the other arguments that can be passed to the cut function.

    0 讨论(0)
提交回复
热议问题