aggregate through across multiple columns in R -
suppose have data.frame
called df
x y z 1 2 3 2 3 4 3 4 5 1 3 4 2 4 5
i want
group infox
for each sub data.frame
, func(df$y, df$z)
how without loop?
you can utilize regular plyr
this:
library(plyr) dlply(df, .(x), function(sub_df) func(sub_df$y, sub_df$z)) # or ddply or daply desired
or dplyr
:
library(dplyr) df %>% group_by(x) %>% summarize(func(y, z))
or data.table
:
library(data.table) df <- as.data.table(df) df[, func(y, z), = x]
r
No comments:
Post a Comment