【发布时间】: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 下的列表?