Fill in Missing Values with Previous or Next Value
fill() fills missing value NA in selected columns using the value in the next or previous entry. This method is particularly useful in scenarios where data is recorded only when it changes and not repeated across consecutive entries.
e.g.1.fill() defaults to replacing missing data from top to bottom; that is, it fills missing values using the value from the previous non-missing entry in the column.
library(tidyr)library(dplyr)library(tibble) # create a datasetsales <-tribble(~quarter, ~year, ~sales,"Q1", 1998, 5.52,"Q2", NA, 1.72,"Q3", NA, 6.69,"Q4", NA, 4.74,"Q1", 2000, 9.77,"Q2", NA, 9.68,"Q3", NA, 9.27,"Q4", NA, 5.96) # fill in missing values downwards (the default if not specified)sales %>%fill(year, .direction ="down")
# A tibble: 6 × 3 rank pet_type breed <int> <chr> <chr> 1 4 Dog French Bulldogs 2 5 Dog Bulldogs 3 6 Dog Beagles 4 1 Cat Persian 5 2 Cat Maine Coon 6 6 Cat American Short
e.g.3. Use .direction = "downup" to fill missing values in both directions: preferentially downwards; if not applicable, then upward. Alternatively, you an use "updown" to switch the preference order. In addition, the filling can be also operated on a per group basis specified by group_by().
# create a datasetfruits <-tribble(~store, ~product, ~stock,"A", "Apple", 10,"A", "Banana", NA,"A", "Orange", 20,"B", "Apple", NA,"B", "Banana", 15,"B", "Orange", NA,"C", "Apple", NA,"C", "Banana", NA,"C", "Orange", 30) fruits %>%# separately for each store:group_by(store) %>%# fill in missing values upwards preferentially, otherwise downwardsfill(stock, .direction ="updown")
Output:
# A tibble: 9 × 3 # Groups: store [3] store product stock <chr> <chr> <dbl> 1 A Apple 10 2 A Banana 20 3 A Orange 20 4 B Apple 15 5 B Banana 15 6 B Orange 15 7 C Apple 30 8 C Banana 30 9 C Orange 30