Reputation: 255
I wanted to create a class in R. I have number of tables and I wanted to plot them by a function. The code I have used is:
temp <- data.frame(gsbi1_30,gsbi1_29,ob_30,ob_29)
where gsbi1_30,gsbi1_29,ob_30,ob_29 are tables.
par(mfrow=c(2,2))
for (i in temp){ plot(i$ambtemp,type="o", pch=22, lty=2, col="brown",xlab = "Hour 2007/09/29" , ylab= "Ambient Tempreture" )
title(main="Hourly Mean, node 25", col.main="brown", font.main=1) }
And I came up with this error:
Error in plot(i$ambtemp, type = "o", pch = 22, lty = 2, col = "brown", :
error in evaluating the argument 'x' in selecting a method for function 'plot': Error in i$ambtemp : $ operator is invalid for atomic vectors
Sample Data:
-0.6 -1.2 -1.0 -0.8 -0.4 -0.2
All the other samples are in the same structure.
Upvotes: 2
Views: 355
Reputation: 3963
The problem is that you shouldn't create temp
as a data.frame in the first place. If gsbi1_30, gsbi1_29, ob_30 and ob_29 are themselves data.frames (as I suspect), data.frame()
will combine their columns to produce a big data.frame.
Instead, create a list
:
temp <- list(gsbi1_30,gsbi1_29,ob_30,ob_29)
and iterate over it with lapply()
(for
loops are very inefficient in R):
par(mfrow=c(2,2))
lapply(temp, function(i) {
plot(i$ambtemp, type = "o", pch = 22, lty = 2, col = "brown", xlab = "Hour 2007/09/29" , ylab = "Ambient Tempreture")
})
Upvotes: 1