From fa2c86da01f81ecb2d4f23fcb755cf2d188d085e Mon Sep 17 00:00:00 2001 From: Benjamin Schwendinger Date: Wed, 13 May 2026 18:15:00 +0200 Subject: [PATCH] add setnafill support for character vectors --- NEWS.md | 2 +- inst/tests/nafill.Rraw | 17 ++++++++++++----- src/nafill.c | 5 ++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/NEWS.md b/NEWS.md index 5de499cc8..1266fb844 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,7 +14,7 @@ ### NEW FEATURES -1. `nafill()`, `setnafill()` extended to work on logical and factor vectors (part of [#3992](https://github.com/Rdatatable/data.table/issues/3992)). Includes support for `Date`, `IDate`, `POSIXct`, etc. `nafill()` works for character vectors, but not yet `setnafill()`. Thanks @jangorecki for the request and @jangorecki and @MichaelChirico for the PRs. +1. `nafill()`, `setnafill()` extended to work on logical, factor and character vectors (part of [#3992](https://github.com/Rdatatable/data.table/issues/3992)). Includes support for `Date`, `IDate`, `POSIXct` and character vectors. Thanks @jangorecki for the request and @jangorecki, @MichaelChirico and @ben-schwen for the PRs. 2. `[,showProgress=]` and `options(datatable.showProgress)` now accept an integer to control the progress bar update interval in seconds, allowing finer control over progress reporting frequency; `TRUE` uses the default 3-second interval, [#6514](https://github.com/Rdatatable/data.table/issues/6514). Thanks @ethanbsmith for the report and @ben-schwen for the PR. diff --git a/inst/tests/nafill.Rraw b/inst/tests/nafill.Rraw index 4ab0d30b9..af6f21afa 100644 --- a/inst/tests/nafill.Rraw +++ b/inst/tests/nafill.Rraw @@ -452,10 +452,15 @@ test(16.07, setnafill(copy(DT), fill="a", cols='f1')$f1, test(16.08, setnafill(copy(DT), fill="a", cols=c('f1', 'f2'))[, .(f1, f2)], data.table(f1=as.factor(c("a", "a", "a", "b", "a", "a", "b", "c", "a", "a")), f2=as.factor(c("a", "a", "c", "b", "a", "a", "b", "a", "a", "a")))) -test(16.09, setnafill(DT, fill="z", cols='c1'), error="not yet supported") -# test(16.10, setnafill(copy(DT), fill="z", cols=c('c1', 'c2'))[, .(c1, c2)], -# data.table(c1=c("z", "z", "a", "b", "z", "z", "c", "d", "z", "z"), -# c2=c("z", "z", "d", "c", "z", "z", "b", "a", "z", "z"))) +test(16.09, setnafill(copy(DT), fill="z", cols='c1')$c1, + c("z", "z", "a", "b", "z", "z", "c", "d", "z", "z")) +test(16.10, setnafill(copy(DT), fill="z", cols=c('c1', 'c2'))[, .(c1, c2)], + data.table(c1=c("z", "z", "a", "b", "z", "z", "c", "d", "z", "z"), + c2=c("z", "z", "d", "c", "z", "z", "b", "a", "z", "z"))) +test(16.101, setnafill(copy(DT), type="locf", cols='c1')$c1, + c(NA, NA, "a", "b", "b", "b", "c", "d", "d", "d")) +test(16.102, setnafill(copy(DT), type="nocb", cols='c1')$c1, + c("a", "a", "a", "b", "c", "c", "c", "d", NA, NA)) test(16.11, setnafill(copy(DT), fill=as.POSIXct("2027-01-01"), cols='t1')$t1, replace(DT$t1, c(1:2, 5:6, 9:10), as.POSIXct("2027-01-01"))) test(16.12, setnafill(copy(DT), fill=as.POSIXct("2027-01-01"), cols=c('t1', 't2'))[, .(t1, t2)], @@ -467,7 +472,9 @@ test(16.13, setnafill(copy(DT), fill=list(TRUE, 9L, 9.0, "a", as.POSIXct("2027-0 d1=c(9.0, 9L, 0L, 1L, 9L, 9L, 2:3, 9L, 9L), f1=as.factor(c("a", "a", "a", "b", "a", "a", "b", "c", "a", "a")), t1=replace(DT$t1, c(1:2, 5:6, 9:10), as.POSIXct("2027-01-01")))) -test(16.14, setnafill(DT, cols=c("l1", "c1")), error="not yet supported") +test(16.14, setnafill(copy(DT), fill=list(TRUE, "z"), cols=c("l1", "c1"))[, .(l1, c1)], + data.table(l1=c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE), + c1=c("z", "z", "a", "b", "z", "z", "c", "d", "z", "z"))) DT = data.table(l=c(NA, FALSE), i=c(NA, 0L)) setnafill(DT, fill=list(TRUE, 1L)) test(16.15, DT, data.table(l=c(TRUE, FALSE), i=1:0)) diff --git a/src/nafill.c b/src/nafill.c index 72ed178c7..abba67cba 100644 --- a/src/nafill.c +++ b/src/nafill.c @@ -203,11 +203,10 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S } vans[i] = ((ans_t) { .dbl_v=(double *)p, .int_v=(int *)p, .int64_v=(int64_t *)p, .char_v=(SEXP)p, .status=0, .message={"\0","\0","\0","\0"} }); } - } else if (any_char) { - error(_("In-place filling of character columns is not yet supported.")); } else { for (R_len_t i=0; i