Skip to content

Commit

Permalink
Progress on dcast.data.frame
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico committed Dec 12, 2024
1 parent d732e4d commit 8844e63
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 9 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ S3method(split, data.table)

export(dcast, melt)
S3method(dcast, data.table)
S3method(dcast, data.frame)
S3method(dcast, default)
S3method(melt, data.table)
S3method(melt, data.frame)
S3method(melt, default)
Expand Down
22 changes: 16 additions & 6 deletions R/fcast.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ guess = function(x) {
}

dcast <- function(
data, formula, fun.aggregate = NULL, ..., margins = NULL,
subset = NULL, fill = NULL, value.var = guess(data)
data, formula, fun.aggregate=NULL, ..., margins=NULL,
subset=NULL, fill=NULL, value.var=guess(data)
) {
# TODO(>=1.19.0): Remove this, just let dispatch to 'default' method fail.
if (!is.data.table(data))
stopf("The %1$s generic in data.table has been passed a %2$s, but data.table::%1$s currently only has a method for data.tables. Please confirm your input is a data.table, with setDT(%3$s) or as.data.table(%3$s). If you intend to use a method from reshape2, try installing that package first, but do note that reshape2 is superseded and is no longer actively developed.", "dcast", class1(data), deparse(substitute(data))) # nocov
UseMethod("dcast", data)
UseMethod("dcast")
}

# TODO(>=1.19.0): Remove this, just let dispatch to 'default' method fail.
dcast.default = function(data, formula, fun.aggregate=NULL, ..., margins=NULL, subset=NULL, fill=NULL, value.var=guess(data))
stopf("The %1$s generic in data.table has been passed a %2$s, but data.table::%1$s currently only has a method for data.tables. Please confirm your input is a data.table, with setDT(%3$s) or as.data.table(%3$s). If you intend to use a method from reshape2, try installing that package first, but do note that reshape2 is superseded and is no longer actively developed.", "dcast", class1(data), deparse(substitute(data))) # nocov

dcast.data.frame = function(data, formula, fun.aggregate=NULL, ..., margins=NULL, subset=NULL, fill=NULL, value.var=guess(data)) {
# lazy-eval means we need to do 'subset' here, not in dcast.data.table
if (!is.null(subset)) {
idx = which(eval(substitute(subset), data, parent.frame()))
data = data[idx, ]
}
setDF(dcast(as.data.table(data), formula=formula, fun.aggregate=fun.aggregate, ..., margins=margins, fill=fill, value.var=value.var))
}

check_formula = function(formula, varnames, valnames, value.var.in.LHSdots, value.var.in.RHSdots) {
Expand Down
Loading

0 comments on commit 8844e63

Please sign in to comment.