Reputation: 1754
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?
Upvotes: 2
Views: 2843
Reputation: 108613
It's easier if you treat a data.frame as a list. An object of class data.frame
is a list, so you can use the list selection [[
here:
for( i in names(iris)){
tmp <- iris[[i]]
if(is.numeric(tmp))
hist(tmp, main = paste("Histogram of",i))
}
See also the answers to this question : Pass a data.frame column name to a function
Upvotes: 3