From ae31bda141c995082e6108c5569fbd021a73e89a Mon Sep 17 00:00:00 2001 From: Tom Andrews Date: Wed, 18 Apr 2018 12:41:48 +0100 Subject: [PATCH] Fix multi-column filling when first column all NA The firstNonNA check only checks the first column for a 2-dimensional SEXP. Therefore if the first column is NA, the function returns without examining any other columns. Fix by removing this check. Fixes #233 --- inst/unitTests/runit.na.locf.R | 14 ++++++++++++++ src/na.c | 3 --- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/inst/unitTests/runit.na.locf.R b/inst/unitTests/runit.na.locf.R index 42eca17d..37723980 100644 --- a/inst/unitTests/runit.na.locf.R +++ b/inst/unitTests/runit.na.locf.R @@ -188,3 +188,17 @@ test.nalocf_by_column_1NA_fromLast <- function() { checkEquals(x, as.xts(z), check.attributes = TRUE) } } + +test.nalocf_first_column_all_NA <- function() { + nacol <- 1L + for (m in MODES) { + xdat <- XDAT2 + xdat[,nacol] <- xdat[,nacol] * NA + storage.mode(xdat) <- m + zdat <- as.zoo(xdat) + + x <- na.locf(xdat) + z <- na.locf(zdat) + checkEquals(x, as.xts(z), check.attributes = TRUE) + } +} diff --git a/src/na.c b/src/na.c index 4e4fcea5..28346917 100644 --- a/src/na.c +++ b/src/na.c @@ -158,9 +158,6 @@ SEXP na_locf (SEXP x, SEXP fromLast, SEXP _maxgap, SEXP _limit) limit = asReal(_limit); gap = 0; - if(firstNonNA(x) == nr) - return(x); - PROTECT(result = allocMatrix(TYPEOF(x), nr, nc)); P++; switch(TYPEOF(x)) {