Skip to contents

Replace NAs with specified values

Usage

# S3 method for class 'RPolarsDataFrame'
replace_na(data, replace, ...)

# S3 method for class 'RPolarsLazyFrame'
replace_na(data, replace, ...)

Arguments

data

A Polars Data/LazyFrame

replace

Either a scalar that will be used to replace NA in all columns, or a named list with the column name and the value that will be used to replace NA in it.

...

Dots which should be empty.

Examples

pl_test <- polars::pl$DataFrame(x = c(NA, 1), y = c(2, NA))

# replace all NA with 0
replace_na(pl_test, 0)
#> shape: (2, 2)
#> ┌─────┬─────┐
#> │ x   ┆ y   │
#> │ --- ┆ --- │
#> │ f64 ┆ f64 │
#> ╞═════╪═════╡
#> │ 0.0 ┆ 2.0 │
#> │ 1.0 ┆ 0.0 │
#> └─────┴─────┘

# custom replacement per column
replace_na(pl_test, list(x = 0, y = 999))
#> shape: (2, 2)
#> ┌─────┬───────┐
#> │ x   ┆ y     │
#> │ --- ┆ ---   │
#> │ f64 ┆ f64   │
#> ╞═════╪═══════╡
#> │ 0.0 ┆ 2.0   │
#> │ 1.0 ┆ 999.0 │
#> └─────┴───────┘