Reputation: 13
I have a matrix / data frame with i.e. 10 columns and 1.000.000 rows. Now I want to apply 20 different functions on this matrix and store the results. Each function is like
df %>% group_by(`column1`, `column2`, `column3`, `column4`) %>% filter (n()>20 )
After that function, I want to save this matrix/data frame into a list, so there should be a list with 20 different matrices/data frames.
I do this in a loop, but that's really slow. I did some tries with apply
, but couldn't really figure out, what I miss.
My actual try is:
apply(X=df,MARGIN=c(1,2),FUN=function(df) df %>% group_by(`column1`, `column2`, `column3`, `column4`) %>% filter (n()>20 ))
It gives an error:
Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "character"
I can smell that I am not far away from the solution. Can you give me a hint?
Thanks and greetings
Upvotes: 1
Views: 198
Reputation: 4233
Here is an example. Here, funs
contains names of functions to be applied, and ml
is the resulting list. Hope this gives you enough hints to achieve what you want. Please provide some data if you need help with your specific problem.
funs <- c("sqrt", "log", "exp")
m <- matrix(runif(1.e4), 100, 100)
am <- function(f, m){
apply(m, MARGIN = 1:2, match.fun(f))
}
ml <- lapply(funs, am, m = m)
Upvotes: 1