【发布时间】:2014-11-14 09:29:22
【问题描述】:
我对在 for 循环中使用数据表有疑问。这是代码和错误。
警告消息意味着我得到了错误的答案,只使用了 56184.97 的第一个数字。 当我使用 K 索引时它有效,当我索引时它不起作用。
找到答案:data.table 不喜欢 [i,j,by] 中“i”列中的变量“i”。
> len<-dim(dd)[1]
> for(i in 1:3) {
+ k<-i
+ cat("i is:", i,"\n")
+ cat("k is:", k,"\n")
+ cat(dd[i:len,savg],"\n")
+ cat(dd[k:len,savg],"\n")
+ }
i is: 1
k is: 1
56184.97 53838.91 51527.4 49264.21 47063.1 44937.83
56184.97 53838.91 51527.4 49264.21 47063.1 44937.83
i is: 2
k is: 2
56184.97 53838.91 51527.4 49264.21 47063.1 44937.83
53838.91 51527.4 49264.21 47063.1 44937.83
i is: 3
k is: 3
56184.97 53838.91 51527.4 49264.21 47063.1 44937.83
51527.4 49264.21 47063.1 44937.83
Warning messages:
1: In i:len : numerical expression has 6 elements: only the first used
2: In i:len : numerical expression has 6 elements: only the first used
3: In i:len : numerical expression has 6 elements: only the first used
【问题讨论】:
标签: r indexing data.table