aggregate2 | R Documentation |
wrapper for stats::aggregate that forces aggregate to return a nice data.frame. When the user passes FUN = c(mean, sd) it causes aggregate(y~id, ..., FUN = c(mean, sd) ) to create a nested matrix inside of the y column. This is annoying to work with. This wrapper explicitly handles this bizarre behavior and offers an option to modify the concatenation between function name and y
aggregate2(x, ..., nm_sep = "_")
x |
an R object. For the formula method a formula, such as y ~ x or cbind(y1, y2) ~ x1 + x2, where the y variables are numeric data to be split into groups according to the grouping x variables (usually factors). |
... |
other arguments to aggregate |
nm_sep |
a character used to separate y variables from functions |
This program is free software but it is provided WITHOUT WARRANTY and with ABSOLUTELY NO GUARANTEE of fitness or functionality for any purpose; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
Revision History
1.0 | 10/1/2024 implemented |
1.1 | date and revisions.. |
typical aggregate output - UNLESS "FUN" argument returns multiple values if "FUN" argument returns multiple values, then the y column(s) are converted to a data.frame
Jacob Strunk <some.body@somewhere.com>
aggregate
data.frame
paste
#test datasets
test1 = data.frame(x=1:50,y=1:10)
test2 = data.frame(x=1:50,y=1:10, z=50:1)
#traditional aggregate with strange x column that is a nested matrix
ag1 = aggregate(x~y, test1, FUN = function(x,...){ c(sd=sd(x,...), mean=mean(x,...)) } )
str(ag1)
#upgraded aggregate function with simple data.frame output
ag2 = aggregate2(x~y, test1, FUN = function(x,...){ c(sd=sd(x,...), mean=mean(x,...)) } , nm_sep="_" )
str(ag2)
#upgraded aggregate function with simple data.frame output - multiple response summations
ag3 = aggregate2(cbind(x,z)~y, test2, FUN = function(x,...){ c(sd=sd(x,...), mean=mean(x,...), n=length(x)) } , nm_sep="." )
str(ag3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.