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

后端 未结 4 1177
半阙折子戏
半阙折子戏 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:54

    If I understand correctly this is one possibility:

    A = c(1.1, 3.2, 5, 8.5, 4.6, 2.2)
    B = c(1, 2, 3,4,10)
    
    B1 <- head(B, -1)
    B2 <- tail(B, -1)
    
    outs <- list()
    
    for(i in seq_along(B1)) {
        outs[[i]] <- A[B1[i] < A & A < B2[i]]
    }
    
    names(outs) <- paste(B1, " & ", B2)
    
    ## > outs
    ## $`1  &  2`
    ## [1] 1.1
    ## 
    ## $`2  &  3`
    ## [1] 2.2
    ## 
    ## $`3  &  4`
    ## [1] 3.2
    ## 
    ## $`4  &  10`
    ## [1] 5.0 8.5 4.6
    

提交回复
热议问题