The object "A" in the question was not, in fact, a data.table. na.omit()
is a generic method, and extra arguments can get eaten by the dots. So, while no error was thrown, either na.omit.matrix()
or na.omit.data.frame()
was called instead, which would omit any row with an NA value.
This is a curse of the S3 system which can bite you. When I receive unexpected output, the first thing is I do is, for example, execute na.omit
(no parentheses) at the console. This will print the function definition. If I see something like UseMethod("na.omit")
, that indicates behavior differs by class, so then I check the class of my object.
The R package ellipsis is aimed at addressing this deficiency. Below is a way to stop this from happening again (based very much so on that readme!)
library(data.table)
library(ellipsis)
mat <- matrix(c(1, 2, 3, NA), nrow = 2)
colnames(mat) <- c("a", "b")
safe_na.omit <- function(object, ...) {
check_dots_used()
na.omit(object, ...)
}
safe_na.omit(mat, col = "a")
#> Error: 1 components of `...` were not used.
#>
#> We detected these problematic arguments:
#> * `col`
#>
#> Did you misspecify an argument?
dt <- as.data.table(mat)
safe_na.omit(dt, cols = "a")
#> a b
#> 1: 1 3
#> 2: 2 NA