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: <br><br>dt <- data.table(A = rep(1:3, each=4), B = rep(1:4, each=3), C = rep(1:2, 6))<br>
dt[, transform(.SD,D=mean(A)), by="B"]<br><br>Here is my first attempt, but it won't run because of scoping issues. <br><br>dt.groupby <- function(data,grouping, ...) {<br>data[, transform(.SD,expr=...), by=grouping]<br>
}<br><br>Any suggestions? <br>It would also be nice if duplicate columns were not created, such as the "B.1" the first procedure adds. <br><br><br>-- <br>Sasha Goodman<br>Doctoral Candidate, Organizational Behavior<br>