Learn R Programming

tidyterra (version 1.3.0)

fill.SpatVector: Fill in missing values with previous or next value on a SpatVector

Description

Fills missing values in selected columns using the next or previous entry. This is useful in the common output format where values are not repeated, and are only recorded when they change.

Usage

# S3 method for SpatVector
fill(data, ..., .by = NULL, .direction = c("down", "up", "downup", "updown"))

Value

A SpatVector object.

Arguments

data

A SpatVector.

...

<tidy-select> Columns to fill.

.by

[Experimental]

<tidy-select> Optionally, a selection of columns to group by for just this operation, functioning as an alternative to group_by(). For details and examples, see ?dplyr_by.

.direction

Direction in which to fill missing values. Currently either "down" (the default), "up", "downup" (i.e. first down and then up) or "updown" (first up and then down).

Methods

Implementation of the generic tidyr::fill() method for SpatVector objects.

Grouped <code>SpatVector</code>

With grouped SpatVector objects created by group_by.SpatVector(), fill() is applied within each group and does not fill across group boundaries.

See Also

tidyr::fill().

Other tidyr verbs for handling missing values: complete.SpatVector(), drop_na.Spat, expand.SpatVector(), replace_na.Spat

Examples

Run this code
library(dplyr)

lux <- terra::vect(system.file("ex/lux.shp", package = "terra"))

# Leave some blanks for demo purposes

lux_blnk <- lux |>
  mutate(NAME_1 = if_else(NAME_1 != NAME_2, NA, NAME_2))

as_tibble(lux_blnk)

# `fill()` defaults to replacing missing data from top to bottom
lux_blnk |>
  fill(NAME_1) |>
  as_tibble()

# direction = "up"
lux_blnk |>
  fill(NAME_1, .direction = "up") |>
  as_tibble()

# Grouping and downup - will restore the initial state
lux_blnk |>
  group_by(ID_1) |>
  fill(NAME_1, .direction = "downup") |>
  as_tibble()

Run the code above in your browser using DataLab