It seems like every question involving loops in R is met with “Loops are bad” and “You’re doing it wrong” with advice to use list, or tapply or whatnot.
I’m learning R, and have implemented the following loop to create image files for each factor level, with the # of factor levels changing each time I run it:
for(i in unique(df$factor)) {
lnam <- paste("test_", i, sep="")
assign(lnam, subset(df, factor==i))
lfile <- paste(lnam, ".png", sep="")
png(file = lfile, bg="transparent")
with(get(lnam), hist(x, main = paste("Histogram of x for ", i, " factor", sep="")))
dev.off()
}
This works. I want to expand it to perhaps run various tests on those subgroups (also output to files), etc.
Is this a valid and legitimate use of loops? Or is there a preferred way to skin this cat?
There’s nothing wrong with loops in general. Sometimes, particularly when you’re working with files or calling functions for their side-effects rather than their outputs, loops can be easier to follow than
*applycalls. However, when you use a loop to simulate a operation that can be vectorised, it’s often much slower, hence the recommendation to avoid them.Re your specific example, though, I’d make the following comments:
levels(factor)rather thanunique(factor).With that in mind: