Fast data manipulation functions implemented in C for large datasets. Provides vectorized alternatives to base R functions with significant performance improvements.
# From CRAN
install.packages("kit")
# Development version
install.packages("kit", repos = "https://fastverse.r-universe.dev")kit uses OpenMP in iif(), nswitch(), vswitch(), psort(), and
charToFact(). The other functions use one thread. OpenMP is optional.
Without OpenMP, kit still works, but these five functions use one thread.
-
Linux: Install the OpenMP runtime for your compiler. With GCC, this is usually
libgomp(libgomp1on Debian and Ubuntu). With Clang, installlibomp(libomp-devon Debian and Ubuntu). -
macOS: Apple Clang does not include OpenMP. Install it with Homebrew:
brew install libomp. On Apple Silicon, Homebrew installs to a path that clang does not search by default. Add that path to your R Makevars before you installkitfrom source (configuredecides withCFLAGS, so the include path must be there):# ~/.R/Makevars CFLAGS += -I$(shell brew --prefix libomp)/include -L$(shell brew --prefix libomp)/lib LDFLAGS += -L$(shell brew --prefix libomp)/lib
-
Windows: OpenMP is included in Rtools. Install the Rtools version that matches your R version. Then install
kitfrom source.
Set the number of threads once for each session:
options(kit.nThread = 4L)The default is 1. If you set a value that is larger than the number of
available processors, kit uses the number of available processors. kit
reads the option each time you call a function, so you can change it between
calls.
To check whether OpenMP is enabled, run:
.Call(kit:::CompEnabledR)If this call returns FALSE, the five functions above use one thread, even
when kit.nThread is larger than 1.
kit is tested on Linux, macOS, and Windows. OpenMP is optional. See
OpenMP and threads for the installation requirements.
shareData() behaves differently on POSIX systems and Windows:
- Linux, macOS, and other POSIX systems:
getData()reads the object one time. A second call with the same name fails. If the sharing session stops beforeclearData(), the object can stay in shared memory: on Linux in/dev/shmuntil you remove it or restart the machine, on macOS until the machine restarts. - Windows:
getData()can read the object more than one time while the sharing session is alive. If the sharing session stops, Windows removes the mapping.
Vector-valued functions that combine several vectors or data frame columns element by element:
psum,pprod,pmean: Row-wise sum, product, and meanfpmin,fpmax,prange: Row-wise minimum, maximum, and rangepall,pany: Row-wise all/any operationspcount,pcountNA: Count occurrences of values or NAspfirst,plast: First/last non-missing values
The word "parallel" in their names means they work across several vectors at once, not that they use multiple threads. These functions are single-threaded C code.
library(kit)
x <- c(1, 3, NA, 5)
y <- c(2, NA, 4, 1)
psum(x, y, na.rm = TRUE) # [1] 3 3 4 6
pmean(x, y, na.rm = TRUE) # [1] 1.5 3.0 4.0 3.0
fpmin(x, y, na.rm = TRUE) # [1] 1 3 4 1
fpmax(x, y, na.rm = TRUE) # [1] 2 3 4 5
prange(x, y, na.rm = TRUE) # [1] 1 0 0 4Fast vectorized conditional logic:
iif: Fast replacement forifelse()with attribute preservationnif: Nested if-else (SQL CASE WHEN equivalent)vswitch,nswitch: Vectorized switch statements
iif(x > 2, x, x - 1) # Preserves attributes unlike base::ifelse
nif(x == 1, "one", x == 2, "two", default = "other")psort: Parallel sort for character vectorstopn: Efficient partial sort (top N values) without full sorting
topn(x, n = 6L, decreasing = TRUE) # Much faster than order()[1:6]charToFact: Fast character-to-factor conversionsetlevels: Change factor levels by reference
funique,fduplicated: Fast unique/duplicated operationsuniqLen: Fast equivalent tolength(unique(x))count,countNA,countOccur: Count element occurrences
funique(iris$Species) # Faster than base::unique
uniqLen(iris$Species) # Faster than length(unique())fpos: Find matrix/vector positions within larger structuresshareData,getData,clearData: Share data between R sessions
Full documentation available at: https://fastverse.org/kit/
GPL-3