Skip to content

Commit 8844e63

Browse files
Progress on dcast.data.frame
1 parent d732e4d commit 8844e63

File tree

3 files changed

+250
-9
lines changed

3 files changed

+250
-9
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ S3method(split, data.table)
125125

126126
export(dcast, melt)
127127
S3method(dcast, data.table)
128+
S3method(dcast, data.frame)
129+
S3method(dcast, default)
128130
S3method(melt, data.table)
129131
S3method(melt, data.frame)
130132
S3method(melt, default)

R/fcast.R

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@ guess = function(x) {
99
}
1010

1111
dcast <- function(
12-
data, formula, fun.aggregate = NULL, ..., margins = NULL,
13-
subset = NULL, fill = NULL, value.var = guess(data)
12+
data, formula, fun.aggregate=NULL, ..., margins=NULL,
13+
subset=NULL, fill=NULL, value.var=guess(data)
1414
) {
15-
# TODO(>=1.19.0): Remove this, just let dispatch to 'default' method fail.
16-
if (!is.data.table(data))
17-
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
18-
UseMethod("dcast", data)
15+
UseMethod("dcast")
16+
}
17+
18+
# TODO(>=1.19.0): Remove this, just let dispatch to 'default' method fail.
19+
dcast.default = function(data, formula, fun.aggregate=NULL, ..., margins=NULL, subset=NULL, fill=NULL, value.var=guess(data))
20+
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
21+
22+
dcast.data.frame = function(data, formula, fun.aggregate=NULL, ..., margins=NULL, subset=NULL, fill=NULL, value.var=guess(data)) {
23+
# lazy-eval means we need to do 'subset' here, not in dcast.data.table
24+
if (!is.null(subset)) {
25+
idx = which(eval(substitute(subset), data, parent.frame()))
26+
data = data[idx, ]
27+
}
28+
setDF(dcast(as.data.table(data), formula=formula, fun.aggregate=fun.aggregate, ..., margins=margins, fill=fill, value.var=value.var))
1929
}
2030

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

0 commit comments

Comments
 (0)