我对 R for 循环的一段感到困惑

I'm confused about a segment of an R for loop

我正在修改现有的 R 脚本,我基本上是在学习 R 的同时掌握它。我没有很强的编程背景(我正在学习),所以我希望有人可以简单地帮助解释这个 for 循环的一部分。 (注意这是在其他一些包中使用 ggplot2)

files <- unique(SQLTable$file_names)
for (i in 1:3){
    dat <- subset(SQLTable,file_names==files[])
    g <- ggplot(dat) + geom_point(aes(x = id, y = Value), size = 1, shape = 19, colour = 'red') + 
        geom_line(aes(x = id, y = Value), size =.5) + 
        facet_grid(Measure ~., scales = "free")
    print(g)
}

不用担心 SQLTable、file_names、id 或 Value,它们都是 SQL table 或列名.上面的代码是一个更大的脚本的一部分,但我试图弄清楚 (i in 1:3) 在 for 循环中做了什么。我几乎得到了循环中发生的所有其他事情,但我已经将它修改为 (i in 1:2) 一直到 (i in 1:100) 并且它似乎没有很大的变化。那么目的是什么?为什么具体是1:3?我希望它不依赖于其他东西,但如果它依赖于其他东西,也许你可以解释一下那部分在做什么?

编辑: 当我 运行 那段代码时,我收到以下消息 6 次。 geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

其次是

Warning messages: 
1: In File_names == secnarios[]:
  longer object length is not a multiple of shorter object length
2: In File_names == secnarios[]:
  longer object length is not a multiple of shorter object length
3: In File_names == secnarios[]:
  longer object length is not a multiple of shorter object length

当我 运行 使用 file_names==files[i] 的代码时,我得到这个:

geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
Warning messages:
1: In loop_apply(n, do.ply) :
  Removed 3 rows containing missing values (geom_point).
2: In loop_apply(n, do.ply) :
  Removed 3 rows containing missing values (geom_point).
3: In loop_apply(n, do.ply) :
  Removed 3 rows containing missing values (geom_path).
4: In loop_apply(n, do.ply) :
  Removed 3 rows containing missing values (geom_path).

如果循环本身没有 i,这只会重复 { ... } 中的内容三次。您只是重新计算所有内容并覆盖同一个变量三次,或者在您的情况下覆盖 100 次。这就是为什么它不会改变任何东西。

您遇到 6 次错误,因为每次迭代都会导致两个问题。