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 apply a function iteratively over a single dataframe in R?

I’m trying to turn this operation into a function where I could set the n argument and without using for loop. This example is for 3 times. I thought I could do it with purrr::reduce but it needs a list (?).

tibble::add_row(tibble::add_row(tibble::add_row(df, .before = 1), .before = 1), .before = 1)

>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

reduce(rep(list(add_row), 3), ~.y(.x, .before = 1), .init =df)

   x  y
1 NA NA
2 NA NA
3 NA NA
4  1  3
5  2  4
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