| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/pvclust.R |
| 3 |
\name{na_locf} |
| 4 |
\alias{na_locf} |
| 5 |
\title{Last Observation Carried Forward} |
| 6 |
\source{ |
| 7 |
\url{https://stat.ethz.ch/pipermail/r-help/2003-November/042126.html} |
| 8 |
\url{https://stackoverflow.com/questions/5302049/last-observation-carried-forward-na-locf-on-panel-cross-section-time-series} |
| 9 |
|
| 10 |
This could probably be solved MUCH faster using Rcpp. |
| 11 |
} |
| 12 |
\usage{ |
| 13 |
na_locf(x, first_na_value = 0, recursive = TRUE, ...) |
| 14 |
} |
| 15 |
\arguments{ |
| 16 |
\item{x}{some vector} |
| 17 |
|
| 18 |
\item{first_na_value}{If the first observation is NA, fill it with "first_na_value"} |
| 19 |
|
| 20 |
\item{recursive}{logical (TRUE). Should na_locf be re-run until all NA values are filled?} |
| 21 |
|
| 22 |
\item{...}{ignored.} |
| 23 |
} |
| 24 |
\value{ |
| 25 |
The original vector, but with all the missing values filled by the value |
| 26 |
before them. |
| 27 |
} |
| 28 |
\description{ |
| 29 |
A function for replacing each NA with the most recent non-NA prior to it. |
| 30 |
} |
| 31 |
\examples{ |
| 32 |
na_locf(c(NA, NA)) |
| 33 |
na_locf(c(1, NA)) |
| 34 |
na_locf(c(1, NA, NA, NA)) |
| 35 |
na_locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4)) |
| 36 |
na_locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4), recursive = FALSE) |
| 37 |
\dontrun{ |
| 38 |
|
| 39 |
# library(microbenchmark) |
| 40 |
# library(zoo) |
| 41 |
|
| 42 |
# microbenchmark( |
| 43 |
# na_locf = na_locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4)), |
| 44 |
# na.locf = na.locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4)) |
| 45 |
#) # my implementation is 6 times faster :) |
| 46 |
|
| 47 |
#microbenchmark( |
| 48 |
# na_locf = na_locf(rep(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4), 1000)), |
| 49 |
# na.locf = na.locf(rep(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4), 1000)) |
| 50 |
# ) # my implementation is 3 times faster |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
\seealso{ |
| 56 |
\link[zoo]{na.locf} |
| 57 |
} |