Skip to content

Commit 291a711

Browse files
nafill logical vector support (#7537)
* nafill logical vector support * documentation * error message
1 parent dc69b3d commit 291a711

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
3. options `"datatable.old.matrix.autoname"` is now `FALSE` by default, meaning `names(data.table(x=1, cbind(1)))` is now `c("x", "V2")`. Toggle the option to retain the old behavior for now; future releases will work to remove this possibility. See the release notes for 1.18.0, item 1 under `NOTE OF INTENDED FUTURE POTENTIAL BREAKING CHANGES`.
1414

15+
### NEW FEATURES
16+
17+
1. `nafill()`, `setnafill()` extended to work on logical vectors (part of [#3992](https://github.com/Rdatatable/data.table/issues/3992)). Thanks @jangorecki for the request and @MichaelChirico for the PR.
18+
1519
### Notes
1620

1721
1. {data.table} now depends on R 3.5.0 (2018).

inst/tests/nafill.Rraw

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test(1.06, nafill(x, fill=NA), x)
3131
test(1.07, nafill(x, fill=NA_real_), x)
3232
test(1.08, nafill(x, fill=Inf), x, warning="precision lost")
3333
test(1.09, nafill(x, fill=NaN), x)
34-
y = x/2
34+
y = x/2 # double input
3535
test(1.11, nafill(y, "locf"), c(NA,NA,3,4,4,4,7,8,8,8)/2)
3636
test(1.12, nafill(y, "nocb"), c(3,3,3,4,7,7,7,8,NA,NA)/2)
3737
test(1.13, nafill(y, fill=0L), c(0,0,3,4,0,0,7,8,0,0)/2)
@@ -112,8 +112,8 @@ x = 1:10
112112
test(3.01, nafill(x, "locf", fill=0L), x)
113113
test(3.02, setnafill(list(copy(x)), "locf", fill=0L), list(x))
114114
test(3.03, setnafill(x, "locf"), error="in-place update is supported only for list")
115-
test(3.04, nafill(letters[1:5], fill=0), error="must be numeric type, or list/data.table")
116-
test(3.05, setnafill(list(letters[1:5]), fill=0), error="must be numeric type, or list/data.table")
115+
test(3.04, nafill(letters[1:5], fill=0), error="must be logical/numeric type, or list/data.table")
116+
test(3.05, setnafill(list(letters[1:5]), fill=0), error="must be logical/numeric type, or list/data.table")
117117
test(3.06, nafill(x, fill=1:2), error="fill must be a vector of length 1.*fcoalesce")
118118
test(3.07, nafill(x, "locf", fill=1:2), error="fill must be a vector of length 1.*x\\.$")
119119
test(3.08, nafill(x, fill="asd"), x, warning=c("Coercing.*character.*integer","NAs introduced by coercion"))
@@ -320,6 +320,19 @@ test(11.08, coerceAs(a, 1L), error="must not be matrix or array")
320320
test(11.09, coerceAs(1L, a), error="must not be matrix or array")
321321

322322
# nafill, setnafill for character, factor and other types #3992
323+
## logical input
324+
x = c(NA, NA, TRUE, FALSE, NA, NA, FALSE, TRUE, NA, NA)
325+
test(12.01, nafill(x, "locf"), c(NA, NA, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE))
326+
test(12.02, nafill(x, "nocb"), c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, NA, NA))
327+
test(12.03, nafill(x, fill=TRUE), c(TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE))
328+
test(12.04, nafill(x, fill=0L), c(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE))
329+
test(12.05, nafill(x, fill=5.0), c(TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE), warning="double.*taken as TRUE")
330+
test(12.06, nafill(x, fill=Inf), c(TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE), warning="double.*taken as TRUE")
331+
test(12.07, nafill(x, fill=NA), x)
332+
test(12.08, nafill(x, fill=NA_integer_), x)
333+
test(12.09, nafill(x, fill=NA_real_), x)
334+
test(12.10, nafill(x, fill=NaN), x)
335+
323336
## logical
324337
## character
325338
## factor

man/nafill.Rd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ nafill(x, type=c("const", "locf", "nocb"), fill=NA, nan=NA)
1414
setnafill(x, type=c("const", "locf", "nocb"), fill=NA, nan=NA, cols=seq_along(x))
1515
}
1616
\arguments{
17-
\item{x}{ Vector, list, data.frame or data.table of numeric columns. }
17+
\item{x}{ Vector, list, data.frame or data.table of logical/numeric columns. }
1818
\item{type}{ Character, one of \emph{"const"}, \emph{"locf"} or \emph{"nocb"}. Defaults to \code{"const"}. }
19-
\item{fill}{ Numeric value to be used to replace missing observations. See examples. }
19+
\item{fill}{ Value to be used to replace missing observations. See examples. }
2020
\item{nan}{ Either \code{NaN} or \code{NA}; if the former, \code{NaN} is treated as distinct from \code{NA}, otherwise, they are treated the same during replacement. See Examples. }
2121
\item{cols}{ Numeric or character vector specifying columns to be updated. }
2222
}

src/nafill.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
113113
if (obj_scalar) {
114114
if (binplace)
115115
error(_("'x' argument is atomic vector, in-place update is supported only for list/data.table"));
116-
else if (!isReal(obj) && !isInteger(obj))
117-
error(_("'x' argument must be numeric type, or list/data.table of numeric types"));
116+
else if (!isReal(obj) && !isInteger(obj) && !isLogical(obj))
117+
error(_("'x' argument must be logical/numeric type, or list/data.table of logical/numeric types"));
118118
SEXP obj1 = obj;
119119
obj = PROTECT(allocVector(VECSXP, 1)); protecti++; // wrap into list
120120
SET_VECTOR_ELT(obj, 0, obj1);
@@ -124,8 +124,8 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
124124
int *icols = INTEGER(ricols);
125125
for (int i=0; i<length(ricols); i++) {
126126
SEXP this_col = VECTOR_ELT(obj, icols[i]-1);
127-
if (!isReal(this_col) && !isInteger(this_col))
128-
error(_("'x' argument must be numeric type, or list/data.table of numeric types"));
127+
if (!isReal(this_col) && !isInteger(this_col) && !isLogical(this_col))
128+
error(_("'x' argument must be logical/numeric type, or list/data.table of logical/numeric types"));
129129
SET_VECTOR_ELT(x, i, this_col);
130130
}
131131
R_len_t nx = length(x);
@@ -210,7 +210,7 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
210210
nafillDouble(dx[i], inx[i], itype, hasFill ? ((double *)fillp[i])[0] : NA_REAL, nan_is_na, &vans[i], verbose);
211211
}
212212
} break;
213-
case INTSXP : {
213+
case LGLSXP: case INTSXP : {
214214
nafillInteger(ix[i], inx[i], itype, hasFill ? ((int32_t *)fillp[i])[0] : NA_INTEGER, &vans[i], verbose);
215215
} break;
216216
}

0 commit comments

Comments
 (0)