-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dcast.data.table should be able to handle multiple aggregation functions #716
Comments
Implemented in commit fc753c2 in 1.9.8. Will merge and add news later. |
Final setting should be more or less: require(data.table)
DT = data.table(x=1:5, y=paste("v", 1:5, sep=""),
v1=6:10, v2=11:15,
k1=letters[1:5], k2=letters[6:10])
# current syntax if we'd like to sum v1,v2 cols and paste k1,k2 cols
# writing it separately for clarity
DT.m = melt(DT, id=1:2, measure=list(3:4, 5:6))
paste_ = function(x) paste(x, collapse="")
dcast.data.table(DT.m, x ~ y, fun.aggregate =
funs(.(sum, vars="value1"), .(paste_, vars="value2")), value.var=c("value1", "value2")) which is a lot of "bla" and redundancy. Instead it could be: dcast.data.table(DT.m, x ~ y, fun.aggregate =
list(sum, function(x) paste(x, collapse="")), value.var=list("value1", "value2")) Idea: If |
Done. dcast.data.table(DT.m, x ~ y, fun.aggregate =
list(sum, function(x) paste(x, collapse="")), value.var=list("value1", "value2"))
# x v1_sum_value1 v2_sum_value1 v3_sum_value1 v4_sum_value1 v5_sum_value1 v1_function_value2
# 1: 1 17 0 0 0 0 af
# 2: 2 0 19 0 0 0
# 3: 3 0 0 21 0 0
# 4: 4 0 0 0 23 0
# 5: 5 0 0 0 0 25
# v2_function_value2 v3_function_value2 v4_function_value2 v5_function_value2
# 1:
# 2: bg
# 3: ch
# 4: di
# 5: ej |
Any chance of adding an option to have the columns in the result alternate, like |
I would like to second @franknarf1. Here is another case on SO where an option to reorder the columns in an alternating way would have been handy: http://stackoverflow.com/a/43650705/3817004 |
Aggregation is not cheap (esp. on large data) and it'd be great to have multiple aggregations done in the same cast function call. Roughly I'm thinking it should be something like:
where
prefix*
would be the string that'd be attached to the cast columns.The text was updated successfully, but these errors were encountered: