naToLastNonNa: Replace NA With "Last" non-NA

View source: R/main.R

naToLastNonNaR Documentation

Replace NA With "Last" non-NA

Description

replace NA values in a vector with the "last" non-NA values (at the nearest smaller indices in each case) in the vector

Usage

naToLastNonNa(x, method = 2)

Arguments

x

vector in which NA are to be replaced with the last non-NA value (at greatest of smaller indices) in the vector

method

integer (1 or 2) distinguishing two different methods

Examples

naToLastNonNa(c(1, 2, NA, NA, 3, NA, NA, 4, NA, NA, 5))
## Result: [1] 1 2 2 2 3 3 3 4 4 4 5
  
# You will get an error if method = 1 and the first element is NA!
  
# naToLastNonNa(c(NA, 1, NA, 2), method = 1)
  
## Error in naToLastNonNa(c(NA, 1, NA, 2)) :
##   The first element must not be NA
  

KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.