You were close. I think you were also trying to get Type
at the end.
data<-read.csv("glass.csv")
# names<-(attributes(data)$names)
names<-names(data)
classes<-sapply(data,class)
for(name in names[classes == 'numeric'])
{
dev.new()
hist(data[,name]) # subset with [] not $
}
You could also just loop through the columns directly:
for (column in data[class=='numeric']) {
dev.new()
hist(column)
}
But ggplot2
is designed for multiple plots. Try it like this:
library(ggplot2)
library(reshape2)
ggplot(melt(data),aes(x=value)) + geom_histogram() + facet_wrap(~variable)