Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert loop into apply function

e <<- data.env ## here i am storing my rdata
data_frames <- Filter(function(x) is.data.frame(get(x)), ls(envir = e)) ## getting only dataframe
for(i in data_frames) e[[i]] <<- mytest_function(e[[i]]) ###  here i am iterating the dataframe 

Now, how do I convert the for loop into an apply function? The loop takes so long to iterate.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’s unclear what you want the result to be. However, if you are just wanting to apply a function to each column in a dataframe, then you can just use sapply.

sapply(df, function(x) mytest_function(x))

Or if you already have a list of a dataframes and are applying a function to each dataframe, then I would use purrr.

library(purrr)

purrr::map(data_frames, mytest_function)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading