我创建了一个标题名称列表,然后修改了一个找到 here
的函数mcollist<-list(A,B,C,D)# where A,B,C and D are dataframes
namelist<-c("A","B","C","D")
for(i in 1:length(namelist)){
mypath <- file.path("C:","Users","myname","Desktop","PLOTS",paste("myplot_", namelist[i], ".jpeg", sep = ""))
jpeg(file=mypath)
mytitle = paste("my title is", namelist[i])
multicolplot<-lapply(mcollist,function(data){pairs(data)})##this is a function that is creating multicollinearity plot for multiple
##datasets stored as a list in 'mcollist'.
dev.off()
}
虽然我得到了数量很多的情节&#34;正确,即与我创建的列表中的名称一样多。这些图都是相同的数据集。我没有收到任何错误,因此我不确定问题出在哪里
多次保存的相同图是运行多图功能的最后一个图。
当应用于保存Correlogram时,相同的功能
for(i in 1:length(data1)){
mypath <- file.path("C:","Users","myname","Desktop","PLOTS",paste("myplot_", data1[i], ".jpeg", sep = ""))
jpeg(file=mypath)
mytitle = paste("Correlogram for", data1[i])
{
a`cf(data1[,i],na.action=na.pass,main=paste(names(data1)[i]))`
}
dev.off()
}
Data1是一个包含变量(作为列)的数据框,我希望通过以下代码实现acf测试:
acf(data1[,i],na.action=na.pass,main=paste(names(data1)[i]))
它在每列data1上运行acf忽略了&#39; na&#39;值,然后粘贴data1列名称中的名称
我收到以下错误:
Error in jpeg(file = mypath) : unable to start jpeg() device
In addition: Warning messages:
1: In jpeg(file = mypath) :
unable to open file 'C:/Users/myname/Desktop/PLOTS/myplot_c(0.62, 1.13, 2.53, -0.97, 0.82, 3.9, 1.77, -0.85, 2.57, -1.1, 1.67, 1.38, 0.67, 2.73, 1.32, 0.84, 0.65, 0.96, -0.86, 0.97, 0.7, 1.05, -0.26, 0.04, 1.14, 1.46, 2.08, 1.2, 2.53, 2.49, 2.79, 2.16, 2.3, -1.29, 0.49, -1.28, 1.78, 1.26, -1.71, -3.89, 1.45, 2.2, 2.26, 1.13, 2.82, 0.83, -0.91, -0.02, 0.79, 0.71, 0, 1.37, 2.81).jpeg' for writing
2: In jpeg(file = mypath) : opening device failed
感谢您的帮助
答案 0 :(得分:1)
您的代码中的问题是您尝试使用两个循环(显式的for
和隐式的lapply
)迭代一个列表。因此,您可以像在此示例中那样简化代码:
namelist<-c("A","B","C","D")
mcollist <- split(as.data.frame(matrix(runif(60),ncol=3)),rep(1:4,each=5))
names(mcollist) <- namelist
for(i in 1:length(namelist)){
jpeg(file=sprintf('~/RR%s.jpeg',namelist[i]))
mytitle = paste("my title is", namelist[i])
pairs(mcollist[[i]],main=mytitle)
dev.off()
}
它生成四个具有不同绘图的文件(此处仅显示前两个):