I have a process that I'd like to loop through one variable at a time.
Although my process is much more complicated, I've used the below to illustrate the basic problem.
Suppose I want to build a histogram and do lots of other stuff for each variable in iris
. The following accomplishes that goal:
hist(iris$Sepal.Length, main = paste("Histogram of Sepal.Length"))
hist(iris$Sepal.Width, main = paste("Histogram of Sepal.Width"))
hist(iris$Petal.Length, main = paste("Histogram of Petal.Length"))
hist(iris$Petal.Width, main = paste("Histogram of Petal.Width"))
However, my data frame is much larger, and my process much more complex. I'd like to wrap this in a loop like below (this does not work, but is how I'm envisioning in my head).
name.list <- names(iris)
for (i in 1:4) {
print(i)
print(name.list[i])
print(paste0('iris$', name.list[i]))
hist(paste0('iris$', name.list[i]), main = paste("Histogram of ", name.list[i]))
# A bunch of other stuff I need to do with this variable
# ...
# ...
}
What am I missing here? How can I wrap this code to loop through one at a time?