&引用;由「;在data.table(groupby)中-我缺少什么?

&引用;由「;在data.table(groupby)中-我缺少什么?,r,data.table,R,Data.table,我正在使用一个大数据表,并使用“按”到“按”3个变量分组 我的data.table是d,键为“ma”(10位整数,但我在下面缩短了它) 但是设置by=“ma,year,month”(这对我来说是更直观的分组方式)并不能满足我的需求。例如,2011年11月ma=284有3个条目,或2011年12月ma=672有2个条目 > d[,list(n=length(trx_num)),by=list(ma,year,month)] ma year month n 1: 284 201

我正在使用一个大数据表,并使用“按”到“按”3个变量分组

我的data.table是
d
,键为“ma”(10位整数,但我在下面缩短了它)

但是设置
by=“ma,year,month”
(这对我来说是更直观的分组方式)并不能满足我的需求。例如,2011年11月ma=284有3个条目,或2011年12月ma=672有2个条目

> d[,list(n=length(trx_num)),by=list(ma,year,month)]
      ma year month n
  1: 284 2011    12 3
  2: 284 2012     1 1
  3: 284 2011    11 5
  4: 284 2011    11 1
  5: 284 2011    11 2
 ---
5782971: 672 2012     7 1
5782972: 672 2011    12 1
5782973: 672 2012     2 1
5782974: 672 2011    12 1
5782975: 672 2012     1 1
然而,颠倒“按”顺序可以得到所需的结果

> d[,list(n=length(trx_num)),by=list(month,year,ma)]
     month year ma  n
  1:    12 2011 284  3
  2:     1 2012 284  1
  3:    11 2011 284  8
  4:     5 2012 543  7
  5:     7 2012 543  3
 ---
1214686:     5 2012 672 28
1214687:     4 2012 672 13
1214688:    12 2011 672 11
1214689:     7 2012 672  9
1214690:     9 2012 672 11
我错过了什么?提前谢谢

编辑:

给出错误结果的数据的str()

> str(d)
Classes âdata.tableâ and 'data.frame':  14688135 obs. of  3 variables:
 $ ma   : num  3e+10 3e+10 3e+10 3e+10 3e+10 ...
 $ year : int  2011 2012 2011 2011 2011 2011 2011 2011 2011 2011 ...
 $ month: int  12 1 11 12 11 11 11 11 11 11 ...
 - attr(*, ".internal.selfref")=<externalptr>
 - attr(*, "sorted")= chr "ma"
str(d) 类–data.table–和“data.frame”:14688135 obs。共有3个变量: $ma:num3E+103E+103E+103E+103E+103E+103E+10。。。 $year:int 2011 2011 2011。。。 $month:int 12 11 11 11 11 11 11。。。 -属性(*,“.internal.selfref”)= -属性(*,“排序”)=chr“ma” str()的错误结果:

> str(d[,.N,by=list(ma,year,month)])
Classes âdata.tableâ and 'data.frame':  5782975 obs. of  4 variables:
 $ ma   : num  3e+10 3e+10 3e+10 3e+10 3e+10 ...
 $ year : int  2011 2012 2011 2011 2011 2012 2012 2012 2012 2012 ...
 $ month: int  12 1 11 11 11 5 7 6 9 8 ...
 $ N    : int  3 1 5 1 2 1 1 1 1 1 ...
 - attr(*, ".internal.selfref")=<externalptr>
>str(d[,.N,by=list(ma,年,月)])
类–data.table–和'data.frame':5782975 obs。共有4个变量:
$ma:num3E+103E+103E+103E+103E+103E+103E+10。。。
$year:int 2011 2012 2012 2012。。。
$month:int 12 11 11 5 7 6 9 8。。。
$N:int 3 1 5 1 2 1 1 1 1 1 1。。。
-属性(*,“.internal.selfref”)=
和正确结果的str()

> str(d[,.N,by=list(month,year,ma)])
Classes âdata.tableâ and 'data.frame':  1214690 obs. of  4 variables:
 $ month: int  12 1 11 5 7 6 9 8 11 12 ...
 $ year : int  2011 2012 2011 2012 2012 2012 2012 2012 2011 2011 ...
 $ ma   : num  3e+10 3e+10 3e+10 3e+10 3e+10 ...
 $ N    : int  3 1 8 7 3 12 15 3 6 6 ...
 - attr(*, ".internal.selfref")=<externalptr>
>str(d[,.N,by=list(月、年、马)])
类–data.table–和'data.frame':1214690 obs。共有4个变量:
$month:int 12 11 5 7 6 9 8 11 12。。。
$year:int 2011 2012 2012 2011。。。
$ma:num3E+103E+103E+103E+103E+103E+103E+10。。。
$N:INT318731215366。。。
-属性(*,“.internal.selfref”)=

我构建了一个小测试用例,在这个对话框的某一点上,我认为它显示了意外的行为(但我读取了错误的对象进行比较):


在注释跟踪后总结,
ma
列的类型为
numeric
,包含的值完全不同,但非常接近,几乎在机器公差范围内,但不完全相同。换言之,这种情况:

 x < y < z
 (y-x) just less than machine tolerance so considered equal
 (z-y) just less than machine tolerance so considered equal
 (z-x) just over machine tolerance so considered not equal
x
当该列与其他两列(即
by=
3列)一起分组时,如果其中一列具有上述值,则这3列的顺序可以更改这些值是否相等(且在同一组中)

解决方案是不对此类数据使用type
numeric
double
是另一个名称)。使用
integer
,或者在这种情况下,如果整数大于2^31(导致强制执行
double
并失去准确性,iiuc),
字符
<代码>数据。表格
在排序
整数
字符
时速度很快。无论如何,它在排序
double
方面还没有那么快

我们将尝试向
数据添加一个新的
警告


我认为您缺少一个较小的例子来说明这一点。请同意。如果没有可以在R中进行交互的代码,就很难诊断出问题所在。你能提供一个列表吗?另外,如果你想知道每组中的行数,你可以使用成语
.N
,比如
d[,.N,by=list(month,year,ma)]
。谢谢你的
.N
,我不知道这个。这似乎并没有改变我的完整数据集上的任何东西,不过-我将尝试并构建一个可复制的例子。谢谢德温。所以我没有在你的示例数据上得到奇怪的结果。但我仍然得到了它(在我的完整数据中有没有.N)。将尝试在较小的数据集上复制并发布。好的。更奇怪的是,因为我用那个物体得到了奇怪的结果。现在可能需要开始描述机器设置。我在运行data.table ver 1.8.2R 2.15.1和data.table 1.8.6的Mac 10.6.8上使用R2.15.2。因此,我只是简单地尝试创建一个新的data.table,其中只包含我在qn中打印的两个ma编号,并且没有奇怪的行为(无论是
length()
还是
.N
都适用于
by
中的两种排序)。然而,有了完整的数据,问题仍然存在。不确定。当我移动到另一台有data.table ver 1.8.6.+1的机器时,我也无法记录它。我用1.8.2运行了它,无法复制。DWin,你真的用你的小例子在结果中看到了重复的组吗?你让我担心!我不记得在这方面有任何错误修复。我所能想到的是,可能有一个或多个分组列是
数字的
,数字很接近,但不在机器公差范围内,它们在打印时看起来是一样的@user1853769,您能提供
str(yourrealdata)
str(thebadresult)
的结果吗?这样我们就可以查看类型了。@user1853769:谢谢聊天室里的朋友。@user1853769,现在给自己取个正确的名字。;-)
dat <- read.table(text=" ma year month n
 284 2011    12 3
 284 2012     1 1
 284 2011    11 5
 284 2011    11 1
 284 2011    11 2
 672 2012     7 1
 672 2011    12 1
 672 2012     2 1
 672 2011    12 1
 672 2012     1 1", header=TRUE)
require(data.table)
d <- data.table( data.frame(dat[rep(rownames(dat), times=dat$n), 1:3], trx_num=unlist(sapply(dat$n, sample, x=1:10)) ) )
setkey(d, ma)
d[,list(n=length(trx_num)),by=list(ma,year,month)]
d[,list(n=length(trx_num)),by=list(month,year,ma)]
d[,.N, by=list(month,year,ma)]
d[,.N, by=list(ma,year,month)] # same result modulo row order
 x < y < z
 (y-x) just less than machine tolerance so considered equal
 (z-y) just less than machine tolerance so considered equal
 (z-x) just over machine tolerance so considered not equal