Polars R bindings
polars (R Polars)
Polars is a blazingly fast DataFrame library, written in Rust.
The goal of Polars is to deliver fast, efficient data processing that:
- Utilizes all available cores on your machine.
- Optimizes queries to reduce unneeded work/memory allocations.
- Handles datasets much larger than your available RAM.
- Follows a consistent and predictable API.
- Adheres to a strict schema (data-types should be known before running
This polars R package provides the R bindings for Polars. It can be used to process Polars DataFrames and other data structures, convert objects between Polars and R, and can be integrated with other common R packages.
To learn more, read the online documentation for this R package, and the user guide for Python/Rust Polars.
Install
The recommended way to install this package is from the R-multiverse community repository:
r
Sys.setenv(NOT_CRAN = "true")
install.packages("polars", repos = "https://community.r-multiverse.org")
More recent (development) version may be installed from the rpolars R-universe repository:
r
Sys.setenv(NOT_CRAN = "true")
install.packages('polars', repos = c("https://rpolars.r-universe.dev", "https://cloud.r-project.org"))
Usage
To avoid conflicts with other packages and base R function names, many of polarsβ functions are hosted in the pl environment, and accessible via the pl$ prefix. Most of the functions for polars objects should be chained with the $ operator.
Additionally, the majority of the functions are intended to match the Python Polars API.
These mean that Polars queries written in Python and in R are very similar.
For example, writing the example from the user guide of Python/Rust in R:
r
library(polars)
Prepare a CSV file
csv_file <- tempfile(fileext = ".csv")
write.csv(iris, csv_file, row.names = FALSE)
Create a query plan (LazyFrame) with filtering and group aggregation
q <- pl$scancsv(csvfile)$filter(pl$col("Sepal.Length") > 5)$group_by(
"Species",
.maintain_order = TRUE
)$agg(pl$all()$sum())
Execute the query plan and collect the result as a Polars DataFrame
df <- q$collect()
df #> shape: (3, 5) #> ββββββββββββββ¬βββββββββββββββ¬ββββββββββββββ¬βββββββββββββββ¬ββββββββββββββ #> β Species β Sepal.Length β Sepal.Width β Petal.Length β Petal.Width β #> β --- β --- β --- β --- β --- β #> β str β f64 β f64 β f64 β f64 β #> ββββββββββββββͺβββββββββββββββͺββββββββββββββͺβββββββββββββββͺββββββββββββββ‘ #> β setosa β 116.9 β 81.7 β 33.2 β 6.1 β #> β versicolor β 281.9 β 131.8 β 202.9 β 63.3 β #> β virginica β 324.5 β 146.2 β 273.1 β 99.6 β #> ββββββββββββββ΄βββββββββββββββ΄ββββββββββββββ΄βββββββββββββββ΄ββββββββββββββ
There are also some functions to manipulate polars objects using base R and some popular other packages.
r
Subset a Polars DataFrame using the [ operator
df[1:2, 1:2]
#> shape: (2, 2)
#> ββββββββββββββ¬βββββββββββββββ
#> β Species β Sepal.Length β
#> β --- β --- β
#> β str β f64 β
#> ββββββββββββββͺβββββββββββββββ‘
#> β setosa β 116.9 β
#> β versicolor β 281.9 β
#> ββββββββββββββ΄βββββββββββββββ
Execute a query plan and collect the result as a tibble data frame
tibble::as_tibble(q)
#> # A tibble: 3 Γ 5
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 setosa 117. 81.7 33.2 6.1
#> 2 versicolor 282. 132. 203. 63.3
#> 3 virginica 324. 146. 273. 99.6
The Get Started vignette (vignette("polars")) provides a more detailed introduction.
Extensions
While one can use this package as-is, other packages build on it to provide different APIs:
- polarssql provides a polars
- tidypolars allows one to use
Maintainers
Version 0 of R Polars was previously maintained by SΓΈren Havelund Welling.Acknowledgements
This package is based on the Polars open source project, originally founded by Ritchie Vink and developed by many contributors.
License
MIT @ polars authors