mwelz / GenericML

R implementation of Generic Machine Learning Inference (Chernozhukov, Demirer, Duflo and Fernández-Val, 2020).
GNU General Public License v3.0
64 stars 14 forks source link

Define sequences with seq_len() and seq_along() instead of the : operater #16

Closed aalfons closed 2 years ago

aalfons commented 2 years ago

Never define sequences with the : operator inside a package, for example 1:n, in particular if you loop over this sequence is some way. If n is 0, 1:n returns the vector c(1, 0), which can have unintended consequences. You may check in other places wheter n is 0, but you may not always catch all such cases. It's better to get into the habit of a safer alternative.

You should use seq_len(n) instead. If n is 0, this just creates an empty vector. So if for example you loop over seq_len(n) and n is 0, you just don't enter the loop.

Instead of 1:length(x), you can use seq_along(x).

mwelz commented 2 years ago

You're right; I have replaced the 1:n-like operations in for loops with their seq()-based counterparts. PR #17 will close this issue. Thank you!