Demo of the bit package

knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
.ff.version <- try(packageVersion("ff"), silent = TRUE) <- !inherits(.ff.version, "try-error") && .ff.version >= "4.0.0" && require(ff)

bit type

Create a huge boolean vector (no NAs allowed)

n <- 1e8
b1 <- bit(n)

It costs only one bit per element


A couple of standard methods work

b1[10:30] <- TRUE

Create a another boolean vector with TRUE in some different positions

b2 <- bit(n)
b2[20:40] <- TRUE

fast boolean operations

b1 & b2

fast boolean operations

summary(b1 & b2)

bitwhich type

Since we have a very skewed distribution we may coerce to an even sparser representation

w1 <- as.bitwhich(b1) 
w2 <- as.bitwhich(b2)

and everything

w1 & w2

works as expected

summary(w1 & w2)

even mixing

summary(b1 & w2)

processing chunks

Many bit functions support a range restriction,

summary(b1, range=c(1,1000))

which is useful

as.which(b1, range=c(1, 1000))

for filtered chunked looping

lapply(chunk(from=1, to=n, length=10), function(i)as.which(b1, range=i))

over large ff vectors

x <- ff(vmode="single", length=n)
x[1:1000] <- runif(1000)
lapply(chunk(x, length.out = 10), function(i)sum(x[as.hi(b1, range=i)]))

and wrap-up

rm(x, b1, b2, w1, w2, n)

for more info check the usage vignette

Try the bit package in your browser

Any scripts or data that you put into this service are public.

bit documentation built on Aug. 4, 2020, 9:06 a.m.