简体   繁体   中英

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!

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)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM