Juan C
Juan C

Reputation: 301

Apply a function to different dataframes

I am trying to run a function over different datasets, and can't seem to get it work. The variable names x and y are the same across datasets, but the dataset (argument z in my custom function) is different.

I have tried lapply but it is not working

Running the function over individual datasets works fine:

resultsmadrid <- customfunction (x=types, y=score, z=madrid)
resultsnavarra <- customfunction (x=types, y=score, z=navarra)
resultsaragon <- customfunction (x=types, y=score, z=aragon)

Trying to do it in one take is not working

regiones <- list(madrid, navarra, aragon) #Creates the list 

resultregiones <- lapply(regiones, customfunction(x=types, y=score, z=regiones)) #Applies that to the list (?)

It's not looping the analysis across the dataframes in the list, the error message says there is a missing argument in the function.

I am not clear on how to call each dataframe from the function argument that does that (z, in my case). It seems the name of the comprehensive list object is not the right approach. Thanks for the help!

Upvotes: 0

Views: 53

Answers (1)

G.Fernandes
G.Fernandes

Reputation: 321

since types and scores are the same, you need to 'loop' throught the elements of you regiones list. Try it like this:

regiones <- list(madrid, navarra, aragon) #Creates the list 

resultregiones <- lapply(regiones,function(X)  customfunction(x=types, y=score, z=X)) 

Upvotes: 1

Related Questions