2

我想在 R 中创建一个类。我有很多表,我想通过一个函数来绘制它们。我使用的代码是:

temp <- data.frame(gsbi1_30,gsbi1_29,ob_30,ob_29)

其中 gsbi1_30,gsbi1_29,ob_30,ob_29 是表。

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) }

我想出了这个错误:

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

样本数据:

-0.6 -1.2 -1.0 -0.8 -0.4 -0.2

所有其他样本都具有相同的结构。

4

1 回答 1

1

问题是您不应该temp首先创建为 data.frame 。如果 gsbi1_30、gsbi1_29、ob_30 和 ob_29 本身是 data.frames(我怀疑),data.frame()将结合它们的列来生成一个大的 data.frame。

相反,创建一个list

temp <- list(gsbi1_30,gsbi1_29,ob_30,ob_29)

并用lapply()for循环在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")
})
于 2013-01-07T13:49:41.923 回答