rdataframesubset

Why does my function iterate through the rows of a dataframe without a for loop?


I've got a dataframe that looks like this:

ImportantNumber ImportantWord LookupVar1 LookupVar2 LookupVar3
100 Mxyztplk 2 3 NA
200 Murgatroyd NA 3 2
300 Veeblefetzer 3 NA NA

And I've got a function that looks like this:

my_function <- function(LookupVar){
     ImportantNumber <-(df$ImportantNumber[!is.na(df[[LookupVar]])])
     ImportantWord <- (df$ImportantWord[!is.na(df[[LookupVar]])])
     cat(paste(ImportantNumber, ImportantWord, collapse = "\n"))
}

Now, the thing that works exactly the way I want it is that, say,

my_function("LookupVar1")

produces:

#> 100 Mxyztplk
#> 300 Veeblefetzer

or

my_function("LookupVar2")

produces

#> 100 Mxyztplk  
#> 200 Murgatroyd

In other words, the function is iterating down the rows of each column tapped by LookupVarN.

Why is it iterating without me specifying a for-loop or any flow control like that?


Solution

  • Because R operations like `[`() and is.na() are "vectorized": they perform iteration internally in optimized C code. You don't see a for loop because the looping happens behind the scenes. See:

    Side note: Be cautious with row-wise operations; they are slow in R code. Prefer built-in vectorized functions or mapply() when possible.