Check which elements of a vector is between the elements of another one in R

后端 未结 4 1170
半阙折子戏
半阙折子戏 2020-12-12 01:30

I am new user in R. How can I check which elements of the vector A is between the elements of vector B for example:

    A = c(1.1,          


        
4条回答
  •  半阙折子戏
    2020-12-12 01:43

    Try this:

          data.frame(A,cut(A,B))
    

    For each observation in A, it tells you which pair of B observations it's between.

    Like so:

    > data.frame(A,cut(A,B))
        A cut.A..B.
    1 1.1     (1,2]
    2 3.2      
    3 5.0      
    4 8.5      
    5 4.6      
    6 2.2     (2,3]
    

    NA means it isn't between two B observations.

    Also try:

    data.frame(A,cut(A,c(-Inf,B,Inf)))
    

提交回复
热议问题