Extract Parts of a Polars Object
Description
Mimics the behavior of [x[i, j, drop =
TRUE]
][Extract] for data.frame or R vector.
Usage
## S3 method for class 'RPolarsDataFrame'
x[i, j, drop = TRUE]
# S3 method for class 'RPolarsLazyFrame'
x[i, j, drop = TRUE]
# S3 method for class 'RPolarsSeries'
x[i]
Arguments
x
|
A DataFrame, LazyFrame, or Series |
i
|
Rows to select. Integer vector, logical vector, or an Expression. |
j
|
Columns to select. Integer vector, logical vector, character vector, or an Expression. For LazyFrames, only an Expression can be used. |
drop
|
Convert to a Polars Series if only one column is selected. For
LazyFrames, if the result has one column and drop = TRUE ,
an error will occur.
|
Details
<Series>[i]
is equivalent to
pl$select(<Series>)[i, , drop =
TRUE]
.
See Also
<DataFrame>$select()
,
<LazyFrame>$select()
,
<DataFrame>$filter()
, <LazyFrame>$filter()
Examples
library("polars")
df = as_polars_df(data.frame(a = 1:3, b = letters[1:3]))
lf = df$lazy()
# Select a row
df[1, ]
#> shape: (1, 2)
#> ┌─────┬─────┐
#> │ a ┆ b │
#> │ --- ┆ --- │
#> │ i32 ┆ str │
#> ╞═════╪═════╡
#> │ 1 ┆ a │
#> └─────┴─────┘
#> shape: (3, 1)
#> ┌─────┐
#> │ a │
#> │ --- │
#> │ i32 │
#> ╞═════╡
#> │ 1 │
#> │ 2 │
#> │ 3 │
#> └─────┘
#> polars Series: shape: (3,)
#> Series: 'b' [str]
#> [
#> "a"
#> "b"
#> "c"
#> ]
# Can use Expression for filtering and column selection
lf[pl$col("a") >= 2, pl$col("b")$alias("new"), drop = FALSE] |>
as.data.frame()
#> new
#> 1 b
#> 2 c