Open ashiklom opened 5 years ago
Some thoughts re the loops section:
I really liked @pralitp 's framing of "know and use language-specific powers/features" or something like that. So, loops in R aren't always bad, but be darn sure you know about the *apply
functions; you don't need to template/polymorph everything but if writing in C++ be aware of their power and pitfalls. Et cetera. Anyway, I do like that as an alternative and more positive way to frame that section.
Just a few comments, while they're fresh on my mind after this tutorial was run. At some point, I can open a PR with the revisions indicated here.
Style guides
Comments
Modularity / Don't repeat yourself
Loops
apply
is slower than a loop, especially if you pre-allocate memory for the output. But all the options pale in comparison, and in terms of clarity, with base R'scolMeans
function:lapply
,purrr::map
, etc.) are framed in the Iteration chapter of R for Data Science. The general argument is that functionals may be preferable to loops when they are less verbose and more clear by removing all the loop boilerplate and emphasizing the things that are changing (what you're looping over, and what you're doing in each loop).purrr
package, but it's not necessarily obvious.x[i]
depends onx[i-1]
).for (i in seq_len(length(x)) result <- c(result, x[i])
depends on what the starting value ofresult
is outside the loop. If you forget to re-setresult
, you could end up withresult
that is several times longer than what you need!...may be better written with an early return, like this:
else
statements inif
clauses.