首页 > 解决方案 > 数据框中不同变量的嵌套for循环

问题描述

我有以下

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 循环以考虑每一对?

标签: rfor-loop

解决方案


2 次编辑:(1)在第二个循环中使唯一的 M 成为当前 P 的子集for,以及(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)))
    }
}
}

推荐阅读