Helena
Helena

Reputation: 217

How to do a double loop in R?

I have a list of dataframe (6 sub-dataframes) and a list of self-defined function (func1~func5). Is there any concise way to write a double loop so that each dataframes can be passed to each function with the expected output a large list?

I have tried below but couldn't work

df_list <- (mget(paste0('df',1:6)))
func_list <- list(func1, func2, func3, func4, func5)
df_func <- lapply(df_list, func_list)

Upvotes: 0

Views: 57

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388797

Try double lapply which would be similar to double for loop.

result <- lapply(df_list, function(x) lapply(func_list, function(f) f(x)))

Every dataframe in df_list will be applied every function in func_list.

Upvotes: 1

Related Questions