fill.SpatVector {tidyterra}R Documentation

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 class 'SpatVector'
fill(data, ..., .direction = c("down", "up", "downup", "updown"))

Arguments

data

A SpatVector.

...

<tidy-select> Columns to fill.

.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).

Value

A SpatVector object.

Methods

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

Grouped SpatVector

With grouped SpatVector created by group_by.SpatVector(), fill() will be applied within each group, meaning that it won't fill across group boundaries.

See Also

tidyr::fill()

Other tidyr verbs for handling missing values: drop_na.Spat, replace_na.Spat

Other tidyr methods: drop_na.Spat, pivot_longer.SpatVector(), pivot_wider.SpatVector(), replace_na.Spat

Examples

library(dplyr)

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

# Leave some blanks for demo purporses

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()


[Package tidyterra version 0.6.0 Index]