Skip to content

Commit

Permalink
Closes #688. dast preserves attributes except when value.var is fac…
Browse files Browse the repository at this point in the history
…tor/ordered factor.
  • Loading branch information
arunsrinivasan committed Jun 20, 2014
1 parent c914391 commit 5087b1d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ DT[, list(.N, mean(y), sum(y)), by=x] # 1.9.3+ - will use GForce.

* `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).

* `dcast.data.table` tries to preserve attributes whereever possible, except when `value.var` is a `factor` (or ordered factor). For `factor` types, the casted columns will be coerced to type `character` thereby losing the `levels` attribute. Closes git [#688](https://github.com/Rdatatable/data.table/issues/688). Thanks to juancentro for reporting.


#### NOTES

* Reminder: using `rolltolast` still works but since v1.9.2 now issues the following warning :
Expand Down
4 changes: 4 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -4674,6 +4674,10 @@ test(1313.22, DT[, list(y=max(y, na.rm=TRUE)), by=x], DT[c(5,10)])
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")

# bug #688 - preserving attributes
DT = data.table(id = c(1,1,2,2), ty = c("a","b","a","b"), da = as.Date("2014-06-20"))
test(1315, dcast.data.table(DT, formula = id ~ ty, value.var="da"), data.table(id=c(1,2), a=as.Date("2014-06-20"), b=as.Date("2014-06-20"), key="id"))

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

# TO DO: Add test for fixed bug #5519 - dcast.data.table returned error when a package imported data.table, but dint happen when "depends" on data.table. This is fixed (commit 1263 v1.9.3), but not sure how to add test.
Expand Down
2 changes: 1 addition & 1 deletion man/dcast.data.table.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ If the formula's LHS contains the same column more than once, ex: \code{dcast.da
Names for columns that are being cast are generated in the same order (separated by a \code{_}) from the (unique) values in each column mentioned in the formula RHS.
The only feature that's not implemented from \code{reshape2:::dcast} *yet* is the argument \code{margins}.
From \code{v1.9.3}, \code{dcast.data.table} tries to preserve attributes whereever possible, except when \code{value.var} is a factor (or ordered factor). For \code{factor} types, the resulting casted columns will be coerced to character type thereby losing \code{levels} attribute.
}
\value{
A keyed \code{data.table} that has been cast. The key columns are equal to the variables in the \code{formula} LHS in the same order.
Expand Down
1 change: 1 addition & 0 deletions src/fcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ SEXP fcast(SEXP DT, SEXP inames, SEXP mnames, SEXP vnames, SEXP fill, SEXP fill_
break;
default : error("Unknown 'value' column type '%s'", type2char(TYPEOF(tmp)));
}
copyMostAttrib(vdt, tmp);
UNPROTECT(1); // tmp
SET_VECTOR_ELT(ans, i+ilen, tmp);
}
Expand Down

0 comments on commit 5087b1d

Please sign in to comment.