From 51141fa01c8950cd6430120b413452e4646c6b41 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Tue, 10 Nov 2015 22:35:48 +0000 Subject: [PATCH] uniqueN handle NULL, closes #1429 --- R/duplicated.R | 2 +- README.md | 2 ++ inst/tests/tests.Rraw | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/R/duplicated.R b/R/duplicated.R index a139bc76c2..86ca2167fb 100644 --- a/R/duplicated.R +++ b/R/duplicated.R @@ -91,7 +91,7 @@ anyDuplicated.data.table <- function(x, incomparables=FALSE, fromLast=FALSE, by= # of groups in a vector or data.table. Here by data.table, # we really mean `.SD` - used in a grouping operation uniqueN <- function(x, by = if (is.data.table(x)) key(x) else NULL) { - if (!is.atomic(x) && !is.data.frame(x)) + if (is.null(x) || (!is.atomic(x) && !is.data.frame(x))) return(length(unique(x))) if (is.atomic(x)) x = as_list(x) if (is.null(by)) by = seq_along(x) diff --git a/README.md b/README.md index b40bee0731..c871fdd1ac 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,8 @@ 16. `rleid()` does not affect attributes of input vector, [#1419](https://github.com/Rdatatable/data.table/issues/1419). Thanks @JanGorecki. + 17. `uniqueN()` now handle NULL properly, [#1429](https://github.com/Rdatatable/data.table/issues/1429). Thanks @JanGorecki. + #### NOTES 1. Updated error message on invalid joins to reflect the new `on=` syntax, [#1368](https://github.com/Rdatatable/data.table/issues/1368). Thanks @MichaelChirico. diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index cf2774f1fb..ccea67459b 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -7214,6 +7214,8 @@ test(1581.12, any(grepl("^GForce optimized j", opt2)), TRUE) options(datatable.optimize=optim) +# handle NULL value correctly #1429 +test(1582, uniqueN(NULL), 0L) ##########################