repeat_last: repeat last non-NA value

Description Usage Arguments Examples

View source: R/data_wrangling.R

Description

Will repeat the last non-NA value. This is also known as carrying the last observation forward/backward. It's faster than zoo::na.locf http://rpubs.com/rubenarslan/repeat_last_na_locf and other alternatives. By specifying maxgap, you can choose not to bridge overly long gaps. By specifying forward = FALSE, you can carry the last observation backward.

Usage

1
repeat_last(x, forward = TRUE, maxgap = Inf, na.rm = FALSE)

Arguments

x

vector to be repeated

forward

carry last observation forward? or backward (FALSE)

maxgap

bridge only up to x NAs (defaults to Inf)

na.rm

whether to omit NAs at the beginning (defaults to FALSE)

Examples

1
2
3
4
5
6
x = c(NA,NA,1,NA,NA,NA,NA,NA,NA,NA,NA,2,3,4,NA,NA,NA,NA,NA,5, NA)
data.frame(x, 
   repeat_last(x), 
   repeat_last(x, forward = FALSE), 
   repeat_last(x, maxgap = 5), 
check.names = FALSE)

grebbel/formr_test documentation built on May 17, 2019, 8:34 a.m.