Clark
Clark

Reputation: 3

Looping through multiple datasets applying multiple parameters unique to each dataset

I have multiple datasets that will be manipulated through a set of parameters unique to each dataset using a niche and complex package/functions. I am trying to loop through multiple datasets and apply the corresponding set of parameters A simplified version of the function with df1 being an individual dataset and df1_p the vector of parameters generated through another specialized function earlier in the code.

special_function(data = df1, parameter = df1_p)

I can index the parameters to apply the first of the parameters for each dataset through multiple datasets in a loop

#list of datasets 
df_list <- list(df1, df2, df3)

#vector of parameters for each dataset 
df1_pms <- c(df1_p1, df1_p2, df1_p3, df1_p4) 
df2_pms <- c(df2_p1, df2_p2, df2_p3, df2_p4) 
df3_pms <- c(df3_p1, df3_p2, df3_p3, df3_p4) 

parameter_list <- list(df1_pms, df2_pms, df3_pms) 
para_index_list <- lapply(parameter_list, '[[',1)

for (i in 1:length(df_list)){
for (j in 1:length(parameter_index_list)){ 
special_function(data = df_list[i], parameter = parameter_index_list[[j]]
}} 

When I try to loop through the datasets with the parameter sets, I end of taking each dataset through each parameter set instead of one dataset-one parameter set

for (i in 1:length(df_list)){ 
for (j in 1:length(parameter_list)){
special_function(data = df_list[i], parameter = parameter_list[[j]] 
}} 

Is there a way to do what I want with the way my data is currently structured, or do I need to reformat my parameters so the list is like df1_p1, df2_p1...df1_p4, df2_p4, df3_p4? Is there an easy way to rearrange my parameters like that?

Upvotes: 0

Views: 53

Answers (0)

Related Questions