A very cool video! I find loops easier to understand and use than the apply functions. However I always had a bad feeling about using them as they are supposed to be inefficient. This video made me feel better about my terrible loopy loops :).
@spacelem3 ай бұрын
I do write plenty of for loops in R, but they're typically in places where either the speed really doesn't matter (e.g. `for (i in vars) ggplot(...)`) or where there's no way to vectorise the body (e.g. in stochastic simulations where there's a load of stuff going on and the loop depends on what happened in the previous loop, and I'm not that good at using `reduce()`). Typically I do what's easiest to read, and usually when something can be vectorised, it's easier to read that way anyway. If I need it to be fast, I have Julia, which says for loops are good actually since all the code is JITted, although Julia makes vectorising code trivial with its dot syntax (e.g. `log.(x)`).
@BarryRowlingsonBaz Жыл бұрын
At 06:01 you've got log(x)[i] in the loop which will compute all of log(x) and then take one value. Seems fixed in the benchmarks seconds later though.
@josiahparry Жыл бұрын
oof! i thought i caught all of those. oops! should set eval = TRUE in the future to catch these 😅
@BarryRowlingsonBaz Жыл бұрын
@@josiahparry It would still work perfectly when evalled because log(x)[i] == log(x[i]) surely? :)