Skip to content

Commit 813ed95

Browse files
Allow setnafill() to accept logical vectors in cols (#4113) (#7842)
* incorporate review comments --------- Co-authored-by: Benjamin Schwendinger <benjaminschwe@gmail.com>
1 parent 25c9dde commit 813ed95

4 files changed

Lines changed: 19 additions & 1 deletion

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
12. `frank()` gains an `order` argument (matching `frankv()`) and now intercepts the unary minus symbol (e.g., `frank(-dates)`) to support reverse ranking even for types where unary `-` is not defined in R, such as `Date` or `character` vectors, [#5489](https://github.com/Rdatatable/data.table/issues/5489). Thanks @hope-data-science for the request and @venom1204 for the implementation.
4848

49+
13. `setnafill()` now accepts a logical vector for the `cols` argument, which must be the same length as the number of columns in `x`, [#4113](https://github.com/Rdatatable/data.table/issues/4113). Thanks to @MichaelChirico for the suggestion and @venom1204 for the PR.
50+
4951
### BUG FIXES
5052

5153
1. `fread()` with `skip=0` and `(header=TRUE|FALSE)` no longer skips the first row when it has fewer fields than subsequent rows, [#7463](https://github.com/Rdatatable/data.table/issues/7463). Thanks @emayerhofer for the report and @ben-schwen for the fix.

R/shift.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@ nafill = function(x, type=c("const","locf","nocb"), fill=NA, nan=NA) {
3333

3434
setnafill = function(x, type=c("const","locf","nocb"), fill=NA, nan=NA, cols=seq_along(x)) {
3535
type = match.arg(type)
36+
if (is.logical(cols)) {
37+
if (length(cols) != length(x)) stopf("'cols' is a logical vector of length %d but there are %d columns", length(cols), length(x))
38+
if (anyNA(cols)) stopf("'cols' contains NA at position %d", which(is.na(cols))[1L])
39+
cols = which(cols)
40+
}
3641
invisible(.Call(CnafillR, x, type, fill, nan_is_na(nan), TRUE, cols))
3742
}

inst/tests/tests.Rraw

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21974,3 +21974,14 @@ test(2386.10, frank(-dates, order=-1L), frankv(dates, order=-1L), warning=warn)
2197421974
test(2386.11, frank(+dates), frankv(dates, order=1L))
2197521975
chars = c("b", "a", "c", "a")
2197621976
test(2386.12, frank(-chars), frankv(chars, order=-1L))
21977+
21978+
# setnafill accepts logical cols #4113
21979+
DT = data.table(a=c(1,NA,3), b=c(4,NA,6), c=c(7,NA,9))
21980+
test(2387.01, setnafill(copy(DT), type="locf", cols=c(TRUE,FALSE,TRUE)), setnafill(copy(DT), type="locf", cols=c(1L,3L)))
21981+
test(2387.02, setnafill(copy(DT), type="locf", cols=c(TRUE,FALSE,TRUE)), setnafill(copy(DT), type="locf", cols=c("a","c")))
21982+
DT2 = data.table(a=c(1,NA), b=c(2,NA))
21983+
test(2387.03, setnafill(copy(DT2), type="locf", cols=integer()), setnafill(copy(DT2), type="locf", cols=c(FALSE,FALSE)))
21984+
DT3 = data.table(a=c(1,NA), b=c("x",NA), c=c(3,NA))
21985+
test(2387.04, setnafill(copy(DT3), type="locf", cols=sapply(DT3, is.numeric)), data.table(a=c(1,1), b=c("x",NA), c=c(3,3)))
21986+
test(2387.05, setnafill(copy(DT3), type="locf", cols=c(TRUE,NA,FALSE)), error="'cols' contains NA at position 2")
21987+
test(2387.06, setnafill(copy(DT3), type="locf", cols=c(TRUE,FALSE)), error="'cols' is a logical vector of length 2 but there are 3 columns")

man/nafill.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ setnafill(x, type=c("const", "locf", "nocb"), fill=NA, nan=NA, cols=seq_along(x)
1818
\item{type}{ Character, one of \emph{"const"}, \emph{"locf"} or \emph{"nocb"}. Defaults to \code{"const"}. }
1919
\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. }
21-
\item{cols}{ Numeric or character vector specifying columns to be updated. }
21+
\item{cols}{ Numeric, character or logical vector specifying columns to be updated. A logical vector must be the same length as the number of columns in \code{x}. }
2222
}
2323
\details{
2424
Supported types are \emph{logical}, \emph{integer}, \emph{double}, \emph{character}, and \emph{factor}, as well as classes built on top of these such as \code{Date}, \code{IDate}, and \code{POSIXct}.

0 commit comments

Comments
 (0)