How do you create vectors with specific intervals in R?

前端 未结 3 712
南旧
南旧 2020-12-02 20:12

I have a question about creating vectors. If I do a <- 1:10, \"a\" has the values 1,2,3,4,5,6,7,8,9,10.

My question is how do you create a vector wit

3条回答
  •  無奈伤痛
    2020-12-02 20:45

    Usually, we want to divide our vector into a number of intervals. In this case, you can use a function where (a) is a vector and (b) is the number of intervals. (Let's suppose you want 4 intervals)

    a <- 1:10
    b <- 4
    
    FunctionIntervalM <- function(a,b) {
      seq(from=min(a), to = max(a), by = (max(a)-min(a))/b)
    }
    
    FunctionIntervalM(a,b)
    # 1.00  3.25  5.50  7.75 10.00
    

    Therefore you have 4 intervals:

    1.00 - 3.25 
    3.25 - 5.50
    5.50 - 7.75
    7.75 - 10.00
    

    You can also use a cut function

      cut(a, 4)
    
    # (0.991,3.25] (0.991,3.25] (0.991,3.25] (3.25,5.5]   (3.25,5.5]   (5.5,7.75]  
    # (5.5,7.75]   (7.75,10]    (7.75,10]    (7.75,10]   
    #Levels: (0.991,3.25] (3.25,5.5] (5.5,7.75] (7.75,10]
    

提交回复
热议问题