【问题标题】:Nested for loop for different variables in a dataframe数据框中不同变量的嵌套for循环
【发布时间】:2018-09-01 01:07:00
【问题描述】:

我有以下

df
P M amount date
1 1 100    03/2012
1 1 200    04/2012
1 2 100    03/2012
1 2 200    04/2012
1 3 300    03/2012
1 4 400    03/2012
...

unique(df$P)unique(df$M) 返回[i] 1 2 3 4 5 6 7 8 9 10

我正在尝试为每对 P 和 M 绘制金额与日期的关系(假设日期在 POXIct 中),因此我为此使用了嵌套的 for 循环。

for(i in unique(df$P)) {
for(j in unique(df$M)) {
    plot(amount ~ date, subset(df, P == i & M == j), 
         type = "l", main = print(paste("P", i, "and M", j)))
}
}

但后来我得到了这个错误:

Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf

当我尝试做进一步的功能时,结果证明对于 P = 1:4,每个在 M 中出现 10 次,即每个 P = 1 有 M = 1:10

但是当我们达到 P = 5 时,它只有一对,M = 2,因此 for 循环被破坏了。

如何修改我的 for 循环以考虑每一对?

【问题讨论】:

  • 对不起,但这并没有真正解决它。问题是,我使用什么表达式来限制我的嵌套 for 循环只计算每个 P 下的 M?假设 M 是 P 下的列表?

标签: r for-loop


【解决方案1】:

2 次编辑:(1) 在第二个 for 循环中使唯一 M 成为当前 P 的子集,并且 (2) 仅在您有足够数据的情况下绘制;我选择了“足以成为 ”>2 个数据点,但使用任何有效的数据点。

for(i in unique(df$P)) {
for(j in unique(df[df$P==i, "M")) {
    if(sum(df$P==i & df$M==j)>2) {
        plot(amount ~ date, subset(df, P == i & M == j), 
            type = "l", main = print(paste("P", i, "and M", j)))
    }
}
}

【讨论】:

  • 我发现了另一种使用管道与 dplyr 解决它的方法。不过谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 2017-06-21
  • 2022-11-18
相关资源
最近更新 更多