【发布时间】:2013-12-14 11:09:41
【问题描述】:
我想遍历数字数据框列表并使用 for 循环为每个数据框的特定列创建图。我有一个工作代码,但结果很奇怪。我希望只创建两个图,但 R 创建了四个,我根本无法理解为什么,特别是因为当我使用 print 而不是 plot 时,他正在打印我期望的值。下面是更大数据集的一个小例子。任何想法都非常感谢。非常感谢!
# Create data
a <- c(1,2,3,4,5)
b <- c(6,7,8,9,10)
c <- c(0,0,0,1,0)
d <- c(1,2,3,4,5,6,7,8,9,10)
e <- c(11,12,13,14,15,16,17,18,19,20)
f <- c(0,0,0,0,0,0,0,1,0,0)
# Create data frames
df1 <- data.frame(cbind(a,b,c))
df2 <- data.frame(cbind(d,e,f))
names(df2) <- c("a","b","c")
# Create list of data frames
l <- list(df1,df2)
# Create titles for plots
titlenames <- c("Graph 1","Graph 2")
# Loop over list of data frames and create plots
for (i in l){ for(j in titlenames) {
plot(x=(i$a[i$c==0]),y=(i$b[i$c==0]),main="",xlab="",ylab="")
title(main=paste(j))
}}
【问题讨论】:
-
for (i in l){ for(j in titlenames)...2 x 2 = 4 -
哇,现在我明白了。非常感谢!