r - Aggregate data with custom group function -
r - Aggregate data with custom group function -
i have info frame , want apply aggregate function on of it's columns value, grouping them custom key.
i have custom function takes input row of info frame , generates key. how can phone call aggregate
function (or sapply
, tapply
...)
something basically:
getrowkey <- function(row_value) { getrowkey = row_value[1] % 5 } aggregate(my_data, getrowkey, fun=max)
with input this:
1,1 6,2 1,3 7,3 12,5 11,8
i'll have next results:
1,8 2,5
in r, should utilize %%
, not single %
symbol. in opinion, don't need custom function here. it's easier substitute function body straight aggregate()
function.
> d <- read.table(text = "1,1 6,2 1,3 7,3 12,5 11,8", sep = ",") > aggregate(d[[2]], d[1] %% 5, max) # v1 x # 1 1 8 # 2 2 5
as stands, custom function not homecoming anything. if adjust
> getrowkey <- function(row_value) { row_value[1] %% 5 }
we can utilize in aggregate()
follows,
> aggregate(dat[[2]], getrowkey(dat[1]), max) v1 x 1 1 8 2 2 5
r group aggregate-functions
Comments
Post a Comment