From dead5cb9eba773403211534fb7f82ab7e88b6f9e Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 29 Aug 2019 02:29:54 +0800 Subject: [PATCH] Skip guess() step in dcast for fun.aggregate=length --- NEWS.md | 1 + R/fcast.R | 5 +++++ inst/tests/tests.Rraw | 3 +++ 3 files changed, 9 insertions(+) diff --git a/NEWS.md b/NEWS.md index 454abff8c6..23fd88ad7f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -289,6 +289,7 @@ 17. The warning message when using `strptime` in `j` has been improved, [#2068](https://github.com/Rdatatable/data.table/issues/2068). Thanks to @tdhock for the report. +18. `dcast` no longer emits a message when `value.var` is missing but `fun.aggregate` is explicitly set to `length` (since `value.var` is arbitrary in this case), [#2980](https://github.com/Rdatatable/data.table/issues/2980). ### Changes in [v1.12.2](https://github.com/Rdatatable/data.table/milestone/14?closed=1) (07 Apr 2019) diff --git a/R/fcast.R b/R/fcast.R index 004cd1f9a3..a619014b04 100644 --- a/R/fcast.R +++ b/R/fcast.R @@ -112,6 +112,11 @@ dcast.data.table = function(data, formula, fun.aggregate = NULL, sep = "_", ..., if (!is.data.table(data)) stop("'data' must be a data.table.") drop = as.logical(rep(drop, length.out=2L)) if (anyNA(drop)) stop("'drop' must be logical TRUE/FALSE") + # #2980 if explicitly providing fun.aggregate=length but not a value.var, + # just use the last column (as guess(data) would do) because length will be + # the same on all columns + if (missing(value.var) && !missing(fun.aggregate) && identical(fun.aggregate, length)) + value.var = names(data)[ncol(data)] lvals = value_vars(value.var, names(data)) valnames = unique(unlist(lvals)) lvars = check_formula(formula, names(data), valnames) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 0a6b04f73f..e347d1e386 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -15814,6 +15814,9 @@ DT = data.table(a=1, b=2) i = 2L test(2091, DT[ , i-1L, with=FALSE], data.table(a=1)) +# #2980 use arbitrary column without message when fun.aggregate=length +DT = data.table(a=c(3L, 3L, 2L, 9L, 5L, 10L, 3L, 2L, 9L, 8L), b=rep(1:5, 2)) +test(2092, any(grepl('override', capture.output(dcast(DT, a~b, fun.aggregate=length)), fixed=TRUE)), FALSE) ################################### # Add new tests above this line #