Skip to content

Commit

Permalink
Closes #693. dcast returns better error message when fun.agg returns …
Browse files Browse the repository at this point in the history
…length!=1
  • Loading branch information
arunsrinivasan committed Jun 20, 2014
1 parent f404b2c commit b142804
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions R/fcast.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ dcast.data.table <- function(data, formula, fun.aggregate = NULL, ..., margins =
}
else data = data[, eval(fun.aggregate), by=c(ff_)]
setkeyv(data, ff_) # can't use 'oo' here, but should be faster as it's uncommon to have huge number of groups.
fun_agg_chk <- function(x) {
pos = uniqlist(as.list(x)[key(x)]) # as.list shallow copies, I believe
len = uniqlengths(pos, nrow(x))
any(len != 1L)
}
if (fun_agg_chk(data))
stop("Aggregating function provided to argument 'fun.aggregate' should return a length 1 vector for each group, but returns length != 1 for atleast one group. Please have a look at the DETAILS section of ?dcast.data.table ")
} else {
if (is.null(subset))
data = data[, unique(c(ff_, value.var)), with=FALSE] # data is untouched so far. subset only required columns
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ DT[, list(.N, mean(y), sum(y)), by=x] # 1.9.3+ - will use GForce.
now fixed such that only very obvious cases coerces silently, ex: `DT[, a := 1]` where `a` is `integer`. Closes # 5442 (git [#35](https://github.com/Rdatatable/data.table/issues/35)).
Thanks to Michele Carriero and John Laing for reporting.

* `dcast.data.table` provides better error message when `fun.aggregate` is specified but it returns length > 1. Closes git [#693](https://github.com/Rdatatable/data.table/issues/35). Thanks to Trevor Alexander for reporting [here on SO](http://stackoverflow.com/questions/24152733/undocumented-error-in-dcast-data-table).

#### NOTES

Expand Down
3 changes: 3 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -4670,6 +4670,9 @@ test(1313.20, DT[, list(y=max(y)), by=x], DT[c(1,6)])
test(1313.21, DT[, list(y=min(y, na.rm=TRUE)), by=x], DT[c(2,7)])
test(1313.22, DT[, list(y=max(y, na.rm=TRUE)), by=x], DT[c(5,10)])

# bug git #693 - dcast.data.table error message improvement:
dt <- data.table(x=c(1,1), y=c(2,2), z = 3:4)
test(1314, dcast.data.table(dt, x ~ y, value.var="z", fun.aggregate=identity), error="Aggregating function provided to argument 'fun.aggregate' should return a length 1")

##########################

Expand Down

0 comments on commit b142804

Please sign in to comment.