Fastest way to cross-tabulate two massive logical vectors in R

后端 未结 5 863
别那么骄傲
别那么骄傲 2021-02-02 16:08

For two logical vectors, x and y, of length > 1E8, what is the fastest way to calculate the 2x2 cross tabulations?

I suspect the answer is to w

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 16:14

    If you're doing a lot of operations on huge logical vectors, take a look at the bit package. It saves a ton of memory by storing the booleans as true 1-bit booleans.

    This doesn't help with table; it actually makes it worse because there are more unique values in the bit vector due to how it's constructed. But it really helps with logical comparisons.

    # N <- 3e7
    require(bit)
    xb <- as.bit(x)
    yb <- as.bit(y)
    benchmark(replications = 1, order = "elapsed", 
        bit = {res <- func_logical(xb,yb)},
        logical = {res <- func_logical(x,y)}
    )
    #      test replications elapsed relative user.self sys.self user.child sys.child
    # 1     bit            1   0.129  1.00000     0.132    0.000          0         0
    # 2 logical            1   3.677 28.50388     2.684    0.928          0         0
    

提交回复
热议问题