3

如何优化大型 R 数据框中每一行的过滤和计数

我有一个data.table如下:

  name day wages hour colour
1  Ann   1   100    6  Green
2  Ann   1   150   18   Blue
3  Ann   2   200   10   Blue
4  Ann   3   150   10  Green
5  Bob   1   100   11    Red
6  Bob   1   200   17    Red
7  Bob   1   150   20  Green
8  Bob   2   100   11    Red

我想知道,对于每个唯一的名称/日期对,对于四个时间段之一,一些事实。我关心的时间段是:

t1 (hour < 9) 
t2 (hour < 17) 
t3 (hour > 9) 
t4 (hour > 17)

一些事实示例可能是:

wages > 175
colour = "Green"

我可以使用以下data.table过滤器完成此操作

setkey(dt,name,day)
result <- dt[,list(wages.t1=sum(wages>175&hour<9),
     wages.t2=sum(wages>175&hour<17),
     wages.t3=sum(wages>175&hour>9),
     wages.t4=sum(wages>175&hour>17),
     green.t1=sum(colour=="Green"&hour<9),
     green.t2=sum(colour=="Green"&hour<17),
     green.t3=sum(colour=="Green"&hour>9),
     green.t4=sum(colour=="Green"&hour>17)),

列表(姓名,日期)]

给我

     name day wages.t1 wages.t2 wages.t3 wages.t4 green.t1 green.t2 green.t3 green.t4
[1,]  Ann   1        0        0        0        0        1        1        0        0
[2,]  Ann   2        0        1        1        0        0        0        0        0
[3,]  Ann   3        0        0        0        0        0        1        1        0
[4,]  Bob   1        0        0        1        0        0        0        1        1
[5,]  Bob   2        0        0        0        0        0        0        0        0

但这 a) 读写起来很糟糕,b) 似乎效率低下。

关于如何做得更好的任何提示?请注意,在我的真实场景中,我有数十万行、四个时间段和每个时间段 30-35 个事实。

-- 创建代码dt

dt = data.table(
  name = factor(c("Ann", "Ann", "Ann", "Ann", 
                  "Bob", "Bob", "Bob", "Bob")), 
  day = c(1, 1, 2, 3, 1, 1, 1, 2), 
  wages = c(100, 150, 200, 150, 100, 200, 150, 100), 
  hour = c(6, 18, 10, 10, 11, 17, 20, 11), 
  colour = c("Green", "Blue", "Blue", "Green", "Red",
             "Red", "Green", "Red")
)
4

1 回答 1

4

怎么样:

f = list(quote(wages>175),quote(colour=="Green"))
t = list(quote(hour<9),quote(hour<17),quote(hour>9),quote(hour>17))
dt = as.data.table(df)
dt[,as.list(mapply("%*%",
            lapply(t,eval,.SD),
            rep(lapply(f,eval,.SD),each=length(t))
           )), by=list(name,day)]
     name day V1 V2 V3 V4 V5 V6 V7 V8
[1,]  Ann   1  0  0  0  0  1  1  0  0
[2,]  Ann   2  0  1  1  0  0  0  0  0
[3,]  Ann   3  0  0  0  0  0  1  1  0
[4,]  Bob   1  0  0  1  0  0  0  1  1
[5,]  Bob   2  0  0  0  0  0  0  0  0

显然,列名没有被处理,但如果这种方法没问题,可以添加。

这应该更有效,因为每个tf只对每个组进行一次评估,然后组合这些结果的组合。

于 2012-04-19T14:45:20.737 回答