Nested `for` loops versus `apply`
DougManuel opened this issue · 2 comments
A common programming style for R is to use apply
family, instead of nested for
loops.
Where there any design considerations for you to use nested for
loops? What are pro/cons of using one approach over another?
(A totally reasonable answer could be that you've come from a programming environment where you've mostly used for
loops. But I thought that your lamba background and CS courses may have discussed the approach). https://www.datacamp.com/community/tutorials/r-tutorial-apply-family.
Apply seems to "apply a function onto the data" but i wanted to "extract the data and use it in my calculation. Basically i did not want to modify any of the data i was reading which is why first thing i do when i find it is assign it to a variable
The function that is applied to the data doesn't need to mutate the data. Basically, the function can the same as what you have. https://github.com/DarkShadeKnigh/SmallCellCheck/blob/b6022d1d236d6ba8a278b3b8107f9560c56a3401/R/table-one.R#L26
It is just about how that function is iterated. Flag it to look at further. apply
is a handy way of working in R (and other languages). I think of it as similar to map functions in other languages.