Matthew G.
Matthew G.

Reputation: 1350

data.table compute column filter

I'm trying to compute a column using data.table;

The goal here is to compute a speedup column for the runtime, relative to 1 thread.

    setup       mode name threads runtime
 1:     A      short    K       1      10
 2:     A      short    K       1      11
 3:     A      short    K       1      10
 4:     A      short    K       2       4
 5:     A      short    K       2       5
 6:     A      short    K       2       8
 7:     B      short    K       1      11
 8:     B      short    K       1      12
 9:     B      short    K       1      10
10:     B      short    K       2       9
11:     B      short    K       2       6
12:     B      short    K       2       8

Here's what I got to...

valT[, speedup:=mean(runtime)/runtime, by=c("setup","threads","name","mode") ]

Of course, the speedups that come out aren't what I want though; For example, the first row speedup computed should be 1.1; for the 4th should be 2.75. This is why I need to narrow the selection. which seemed to be the answer, but I can't deploy it correctly:

valT[, speedup:=mean(runtime)/runtime, which(threads==1), by=c("setup","threads","name","mode") ]
    Error in `[.data.table`(valT, , runtime/mean(runtime), which(threads ==  : 
      Provide either 'by' or 'keyby' but not both

Data:

valT = data.table(structure(list(setup = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), 
    mode = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = "     short", class = "factor"), name = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "  K", class = "factor"), 
    threads = c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L
    ), runtime = c(10, 11, 10, 4, 5, 8, 11, 12, 10, 9, 6, 8)), .Names = c("setup", 
"mode", "name", "threads", "runtime"), class = "data.frame", row.names = c(NA, 
-12L)))

Upvotes: 1

Views: 2278

Answers (1)

flodel
flodel

Reputation: 89057

This works:

valT[, speedup := mean(runtime[threads == 1]) / runtime,
     by = c("setup","name","mode")]

Upvotes: 3

Related Questions