Why is it iterating without me specifying a for-loop or any flow control like that?
Actually, there is no iterating going on here. You are using vectorized functions (functions that accept a vector and return a vector), here specifically you are using is.na()
and [
, which means that you don't need to use for loops or other control-flow methods. Vectorization means that operations are performed in parallel, not one by one on each element.
There's a lot more to vectorization in R, but these SO links can give you a lot more reading:
How to define a vectorized function in R
How do I know a function or an operation in R is vectorized?