美文网首页R语言R for data science
data.table中 between 与 inrange的区别

data.table中 between 与 inrange的区别

作者: 热衷组培的二货潜 | 来源:发表于2019-04-27 18:11 被阅读3次

    问题来源于一片博客data.table 与 dplyr 的区别

    查阅到 between vs inrange in data.table

    • 官方说明书
    • between is equivalent to x >= lower & x <= upper when incbounds=TRUE, or x > lower & y < upper when FALSE, It is set to TRUE by default for infix notations. With a caveat that NA in lower or upper are taken as a missing bound and return TRUE not NA.
    • inrange checks whether each value in x is in between any of the intervals provided in lower,upper.
    between(x, lower, upper, incbounds=TRUE)
    x %between% y
    inrange(x, lower, upper, incbounds=TRUE)
    x %inrange% y
    
    > library(data.table)
    > X = data.table(a=1:5, b=6:10, c=c(5:1))
    > X
       a  b c
    1: 1  6 5
    2: 2  7 4
    3: 3  8 3
    4: 4  9 2
    5: 5 10 1
    
    > X[a %between% list(c, b)]
       a  b c
    1: 3  8 3
    2: 4  9 2
    3: 5 10 1
    
    > X[a %between% list(b, c)]
    Empty data.table (0 rows) of 3 cols: a,b,c
    
    > X[a %inrange% list(c, b)]
       a  b c
    1: 1  6 5
    2: 2  7 4
    3: 3  8 3
    4: 4  9 2
    5: 5 10 1
    
    > X[a %inrange% list(b, c)]
    Empty data.table (0 rows) of 3 cols: a,b,c
    
    • 从上面可以看出X[a %between% list(c, b)]%between%表示的是Cn ≤ An ≤ Bn的行
    • X[a %inrange% list(c, b)] 表示是:假设在列c中的最小值为Cmin, 列b中的最大值为Bmax,那么结果就是符合Cmin ≤ a ≤ Bmax的行,其实不难理解,我们可以看到当list(c, b)变为list(b, c)时候是没有结果输出的。

    相关文章

      网友评论

        本文标题:data.table中 between 与 inrange的区别

        本文链接:https://www.haomeiwen.com/subject/qrnknqtx.html