Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

fill..html

Fill in missing values with previous or next value


Description

Fills missing values in the selected columns using the next or previous entry. Can be done by group.

Supports tidyselect

Usage

fill.(.df, ..., .direction = c("down", "up", "downup", "updown"), .by = NULL)

Arguments

.df

A data.frame or data.table

...

A selection of columns. tidyselect compatible.

.direction

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

.by

Columns to group by when filling should be done by group

Examples

test_df <- data.table(
  a = c(1, NA, 3, 4, 5),
  b = c(NA, 2, NA, NA, 5),
  groups = c("a", "a", "a", "b", "b")
 )

test_df %>%
  fill.(a, b)

test_df %>%
  fill.(a, b, .direction = "downup")

test_df %>%
  fill.(a, b, .direction = "downup", .by = groups)

tidytable

Tidy Interface to 'data.table'

v0.6.1
MIT + file LICENSE
Authors
Mark Fairbanks [aut, cre], Abdessabour Moutik [ctb], Matt Carlson [ctb], Ivan Leung [ctb], Ross Kennedy [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.