14.1 polars

Show the code
if(!require(polars))
    install.packages("polars", repos = "https://rpolars.r-universe.dev")

polars_info()
#> Polars R package version : 0.18.0
#> Rust Polars crate version: 0.41.3
#> 
#> Thread pool size: 8 
#> 
#> Features:                               
#> default                    TRUE
#> full_features              TRUE
#> disable_limit_max_threads  TRUE
#> nightly                    TRUE
#> sql                        TRUE
#> rpolars_debug_print       FALSE
#> 
#> Code completion: deactivated
polars_code_completion_activate()

Polars 的主要函数存储在 “pl” 命名空间中,可以使用 “pl$” 前缀进行访问,以防止与其他组件和base R 函数名称冲突

Show the code
iris_polars <- pl$DataFrame(iris)
iris_polars
shape: (150, 5)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
f64 f64 f64 f64 cat
5.1 3.5 1.4 0.2 "setosa"
4.9 3.0 1.4 0.2 "setosa"
4.7 3.2 1.3 0.2 "setosa"
4.6 3.1 1.5 0.2 "setosa"
5.0 3.6 1.4 0.2 "setosa"
5.4 3.9 1.7 0.4 "setosa"
4.6 3.4 1.4 0.3 "setosa"
5.0 3.4 1.5 0.2 "setosa"
4.4 2.9 1.4 0.2 "setosa"
4.9 3.1 1.5 0.1 "setosa"
5.4 3.7 1.5 0.2 "setosa"
4.8 3.4 1.6 0.2 "setosa"
4.8 3.0 1.4 0.1 "setosa"
4.3 3.0 1.1 0.1 "setosa"
5.8 4.0 1.2 0.2 "setosa"
5.7 4.4 1.5 0.4 "setosa"
5.4 3.9 1.3 0.4 "setosa"
5.1 3.5 1.4 0.3 "setosa"
5.7 3.8 1.7 0.3 "setosa"
5.1 3.8 1.5 0.3 "setosa"
7.4 2.8 6.1 1.9 "virginica"
7.9 3.8 6.4 2.0 "virginica"
6.4 2.8 5.6 2.2 "virginica"
6.3 2.8 5.1 1.5 "virginica"
6.1 2.6 5.6 1.4 "virginica"
7.7 3.0 6.1 2.3 "virginica"
6.3 3.4 5.6 2.4 "virginica"
6.4 3.1 5.5 1.8 "virginica"
6.0 3.0 4.8 1.8 "virginica"
6.9 3.1 5.4 2.1 "virginica"
6.7 3.1 5.6 2.4 "virginica"
6.9 3.1 5.1 2.3 "virginica"
5.8 2.7 5.1 1.9 "virginica"
6.8 3.2 5.9 2.3 "virginica"
6.7 3.3 5.7 2.5 "virginica"
6.7 3.0 5.2 2.3 "virginica"
6.3 2.5 5.0 1.9 "virginica"
6.5 3.0 5.2 2.0 "virginica"
6.2 3.4 5.4 2.3 "virginica"
5.9 3.0 5.1 1.8 "virginica"

访问属性

Show the code
iris_polars$shape
#> [1] 150   5
iris_polars$height
#> [1] 150
iris_polars$width
#> [1] 5

# polars syntax
pl$DataFrame(iris)$
  select(c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"))$
  with_columns(
    pl$when(
      (pl$col("Petal.Length") / pl$col("Petal.Width") > 3)
    )$then(pl$lit("long"))$
      otherwise(pl$lit("large"))$
      alias("petal_type")
  )$
  filter(pl$col("Sepal.Length")$is_between(4.5, 5.5))$
  head(6)
shape: (6, 5)
Sepal.Length Sepal.Width Petal.Length Petal.Width petal_type
f64 f64 f64 f64 str
5.1 3.5 1.4 0.2 "long"
4.9 3.0 1.4 0.2 "long"
4.7 3.2 1.3 0.2 "long"
4.6 3.1 1.5 0.2 "long"
5.0 3.6 1.4 0.2 "long"
5.4 3.9 1.7 0.4 "long"
Show the code
pl$read_csv("data/Advertising.csv")
shape: (200, 5)
TV radio newspaper sales
i64 f64 f64 f64 f64
1 230.1 37.8 69.2 22.1
2 44.5 39.3 45.1 10.4
3 17.2 45.9 69.3 9.3
4 151.5 41.3 58.5 18.5
5 180.8 10.8 58.4 12.9
6 8.7 48.9 75.0 7.2
7 57.5 32.8 23.5 11.8
8 120.2 19.6 11.6 13.2
9 8.6 2.1 1.0 4.8
10 199.8 2.6 21.2 10.6
11 66.1 5.8 24.2 8.6
12 214.7 24.0 4.0 17.4
13 23.8 35.1 65.9 9.2
14 97.5 7.6 7.2 9.7
15 204.1 32.9 46.0 19.0
16 195.4 47.7 52.9 22.4
17 67.8 36.6 114.0 12.5
18 281.4 39.6 55.8 24.4
19 69.2 20.5 18.3 11.3
20 147.3 23.9 19.1 14.6
181 156.6 2.6 8.3 10.5
182 218.5 5.4 27.4 12.2
183 56.2 5.7 29.7 8.7
184 287.6 43.0 71.8 26.2
185 253.8 21.3 30.0 17.6
186 205.0 45.1 19.6 22.6
187 139.5 2.1 26.6 10.3
188 191.1 28.7 18.2 17.3
189 286.0 13.9 3.7 15.9
190 18.7 12.1 23.4 6.7
191 39.5 41.1 5.8 10.8
192 75.5 10.8 6.0 9.9
193 17.2 4.1 31.6 5.9
194 166.8 42.0 3.6 19.6
195 149.7 35.6 6.0 17.3
196 38.2 3.7 13.8 7.6
197 94.2 4.9 8.1 9.7
198 177.0 9.3 6.4 12.8
199 283.6 42.0 66.2 25.5
200 232.1 8.6 8.7 13.4

14.2 tidypolars

Show the code
if(!require(tidypolars))
    install.packages('tidypolars',
                     repos = c('https://etiennebacher.r-universe.dev', getOption("repos"))
    )

iris |> 
  as_polars_df() |> 
  select(starts_with(c("Sep", "Pet"))) |> 
  mutate(
    petal_type = ifelse((Petal.Length / Petal.Width) > 3, "long", "large")
  ) |> 
  filter(between(Sepal.Length, 4.5, 5.5)) |> 
  head()
shape: (6, 5)
Sepal.Length Sepal.Width Petal.Length Petal.Width petal_type
f64 f64 f64 f64 str
5.1 3.5 1.4 0.2 "long"
4.9 3.0 1.4 0.2 "long"
4.7 3.2 1.3 0.2 "long"
4.6 3.1 1.5 0.2 "long"
5.0 3.6 1.4 0.2 "long"
5.4 3.9 1.7 0.4 "long"