[datatable-help] convenience function for transforming variables, and adding them to the table

Sasha Goodman sashag at stanford.edu
Thu May 27 00:04:23 CEST 2010


I'm trying to make a simple convenience function for the following common
procedure, where one variable is transformed with an arbitrary function and
merged as a variable to the table:

dt <- data.table(A = rep(1:3, each=4), B = rep(1:4, each=3), C = rep(1:2,
6))
dt[, transform(.SD,D=mean(A)), by="B"]

Here is my first attempt, but it won't run because of scoping issues.

dt.groupby <- function(data,grouping, ...) {
data[, transform(.SD,expr=...), by=grouping]
}

Any suggestions?
It would also be nice if duplicate columns were not created, such as the
"B.1" the first procedure adds.


-- 
Sasha Goodman
Doctoral Candidate, Organizational Behavior
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20100526/9afdc361/attachment.htm>


More information about the datatable-help mailing list