Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use a for loop in R [closed]

Almost as soon as you finish your introduction to R, where you probably learned the syntax for a for loop, you are told to avoid for loops in R!

R is a vectorized language, so you are told to NEVER do things like

x = 1:10
y = 2:11
z = rep(NA, 10)
for i in 1:10{    
    z[i] = x[i] + y[i]    
}

and instead do

z = x + y

You are also told to use the family of *ply functions for iterative looping purposes.

My question is, other than potential code readability scenarios, is there ever a good time to use for loops in R?

like image 849
NewNameStat Avatar asked Apr 06 '16 12:04

NewNameStat


2 Answers

Advanced R mentions three cases for loops: modifying in place, recursive functions and while loops. Since you are asking for for loops, here is what is written on the first two cases:

Modifying in place

If you need to modify part of an existing data frame, it’s often better to use a for loop. For example, the following code performs a variable-by-variable transformation by matching the names of a list of functions to the names of variables in a data frame.

trans <- list(
  disp = function(x) x * 0.0163871,
  am = function(x) factor(x, levels = c("auto", "manual"))
)
for(var in names(trans)) {
  mtcars[[var]] <- trans[[var]](mtcars[[var]])
}

We wouldn’t normally use lapply() to replace this loop directly, but it is possible.

Recursive relationships

It’s hard to convert a for loop into a functional when the relationship between elements is not independent, or is defined recursively. For example, exponential smoothing works by taking a weighted average of the current and previous data points. The exps() function below implements exponential smoothing with a for loop.

exps <- function(x, alpha) {
  s <- numeric(length(x) + 1)
  for (i in seq_along(s)) {
    if (i == 1) {
      s[i] <- x[i]
    } else {
      s[i] <- alpha * x[i - 1] + (1 - alpha) * s[i - 1]
    }
  }
  s
}
x <- runif(6)
exps(x, 0.5)
#> [1] 0.6622163 0.6622163 0.4758159 0.2703593 0.1896377 0.5506731 0.7300305

We can’t eliminate the for loop because none of the functionals we’ve seen allow the output at position i to depend on both the input and output at position i - 1.

like image 154
sebastianmm Avatar answered Nov 22 '22 17:11

sebastianmm


Creating a multipage pdf of graphs. Or really anything that takes a long time each iteration, as the generation of a plot does. If the loop isn't the bottleneck, it's almost always more readable that way to me, so I do it.

pdf("file.pdf", onefile=TRUE)
for(var in unique(df$some_var)){
  p <- ggplot(df[df$some_var==var, ], aes(x=x, y=y)) + geom_line()
  print(p)
}
dev.off()
like image 29
cory Avatar answered Nov 22 '22 17:11

cory