Skip to content

Commit fd689ba

Browse files
Open up factor support
1 parent 2a2f2c4 commit fd689ba

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
### NEW FEATURES
1616

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.
17+
1. `nafill()`, `setnafill()` extended to work on logical and factor vectors (part of [#3992](https://github.com/Rdatatable/data.table/issues/3992)). Thanks @jangorecki for the request and @MichaelChirico for the PR.
1818

1919
### Notes
2020

inst/tests/nafill.Rraw

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,23 @@ test(12.08, nafill(x, fill=NA_integer_), x)
333333
test(12.09, nafill(x, fill=NA_real_), x)
334334
test(12.10, nafill(x, fill=NaN), x)
335335

336+
## factor input
337+
x = rep(NA_character_, 10L)
338+
x[c(3:4, 7:8)] = c("a", "b", "a", "c")
339+
x = as.factor(x)
340+
test(13.01, nafill(x, "locf"), replace(replace(x, 5:6, "b"), 9:10, "c"))
341+
test(13.02, nafill(x, "nocb"), replace(x, c(1:2, 5:6), "a"))
342+
test(13.03, nafill(x, fill="a"), x_fill_a <- replace(x, c(1:2, 5:6, 9:10), "a"))
343+
test(13.04, nafill(x, fill=1L), x_fill_a)
344+
test(13.05, nafill(x, fill=1.0), x_fill_a)
345+
test(13.06, nafill(x, fill=factor("a")), x_fill_a)
346+
test(13.07, nafill(x, fill=factor("a", levels=levels(x))), x_fill_a)
347+
test(13.08, nafill(x, fill=factor("a", levels=c("a", "b"))), x_fill_a)
348+
test(13.09, nafill(x, fill=factor("a", levels=c("a", "d"))), x_fill_a)
349+
test(13.10, nafill(x, fill=NA), x)
350+
test(13.11, nafill(x, fill=NA_integer_), x)
351+
test(13.12, nafill(x, fill=NA_character_), x)
352+
336353
## logical
337354
## character
338355
## factor

man/nafill.Rd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ x = c(1, NA, NaN, 3, NaN, NA, 4)
3737
nafill(x, "locf")
3838
nafill(x, "locf", nan=NaN)
3939

40+
# works for factors
41+
x = gl(3, 2, 10)
42+
is.na(x) = 1:2
43+
nafill(x, "nocb")
44+
4045
# fill= applies to any leftover NA
4146
nafill(c(NA, x), "locf")
4247
nafill(c(NA, x), "locf", fill=0)

src/nafill.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ 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) && !isLogical(obj))
116+
else if (!isReal(obj) && TYPEOF(obj) != INTSXP && !isLogical(obj))
117117
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
@@ -124,7 +124,7 @@ 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) && !isLogical(this_col))
127+
if (!isReal(this_col) && TYPEOF(this_col) != INTSXP && !isLogical(this_col))
128128
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
}

0 commit comments

Comments
 (0)