Learn R Programming

dendextend (version 1.13.4)

na_locf: Last Observation Carried Forward

Description

A function for replacing each NA with the most recent non-NA prior to it.

Usage

na_locf(x, first_na_value = 0, recursive = TRUE, ...)

Arguments

x

some vector

first_na_value

If the first observation is NA, fill it with "first_na_value"

recursive

logical (TRUE). Should na_locf be re-run until all NA values are filled?

...

ignored.

Value

The original vector, but with all the missing values filled by the value before them.

See Also

na.locf

Examples

Run this code
# NOT RUN {
na_locf(c(NA, NA))
na_locf(c(1, NA))
na_locf(c(1, NA, NA, NA))
na_locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4))
na_locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4), recursive = FALSE)
# }
# NOT RUN {
library(microbenchmark)
library(zoo)

microbenchmark(
  na_locf = na_locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4)),
  na.locf = na.locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4))
) # my implementation is 6 times faster :)

microbenchmark(
  na_locf = na_locf(rep(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4), 1000)),
  na.locf = na.locf(rep(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4), 1000))
) # my implementation is 3 times faster
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab