11

custId、saleDate、DeliveDateTimeの3つの列を持つデータフレームがあります。

> head(events22)
     custId            saleDate      DelivDate
1 280356593 2012-11-14 14:04:59 11/14/12 17:29
2 280367076 2012-11-14 17:04:44 11/14/12 20:48
3 280380097 2012-11-14 17:38:34 11/14/12 20:45
4 280380095 2012-11-14 20:45:44 11/14/12 23:59
5 280380095 2012-11-14 20:31:39 11/14/12 23:49
6 280380095 2012-11-14 19:58:32 11/15/12 00:10

これがdputです:

> dput(events22)
structure(list(custId = c(280356593L, 280367076L, 280380097L, 
280380095L, 280380095L, 280380095L, 280364279L, 280364279L, 280398506L, 
280336395L, 280364376L, 280368458L, 280368458L, 280368456L, 280368456L, 
280364225L, 280391721L, 280353458L, 280387607L, 280387607L), 
    saleDate = structure(c(1352901899.215, 1352912684.484, 1352914714.971, 
    1352925944.429, 1352925099.247, 1352923112.636, 1352922476.55, 
    1352920666.968, 1352915226.534, 1352911135.077, 1352921349.592, 
    1352911494.975, 1352910529.86, 1352924755.295, 1352907511.476, 
    1352920108.577, 1352906160.883, 1352905925.134, 1352916810.309, 
    1352916025.673), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    DelivDate = c("11/14/12 17:29", "11/14/12 20:48", "11/14/12 20:45", 
    "11/14/12 23:59", "11/14/12 23:49", "11/15/12 00:10", "11/14/12 23:35", 
    "11/14/12 22:59", "11/14/12 20:53", "11/14/12 19:52", "11/14/12 23:01", 
    "11/14/12 19:47", "11/14/12 19:42", "11/14/12 23:31", "11/14/12 23:33", 
    "11/14/12 22:45", "11/14/12 18:11", "11/14/12 18:12", "11/14/12 19:17", 
    "11/14/12 19:19")), .Names = c("custId", "saleDate", "DelivDate"
), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", 
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"
), class = "data.frame")

それぞれDelivDateの最新のを見つけようとしています。saleDatecustId

私は次のようにplyr::ddplyを使用してそれを行うことができます:

dd1 <-ddply(events22, .(custId),.inform = T, function(x){
x[x$saleDate == max(x$saleDate),"DelivDate"]
})

私の質問は、ddplyメソッドは少し時間がかかるのでこれを行うためのより速い方法があるかどうかです(完全なデータセットは約400k行です)。の使用を検討しましたがaggregate()、並べ替えている値以外の値を取得する方法がわかりません。

助言がありますか?

編集:

10回の反復での10k行のベンチマーク結果は次のとおりです。

      test replications elapsed relative user.self
2   AGG2()           10    5.96    1.000      5.93
1   AGG1()           10   20.87    3.502     20.75
5 DATATABLE()        10   61.32        1     60.31
3  DDPLY()           10   80.04   13.430     79.63
4 DOCALL()           10   90.43   15.173     88.39

EDIT2:最速ですが、AGG2()は正しい答えを出しません。

    > head(agg2)
     custId            saleDate      DelivDate
1 280336395 2012-11-14 16:38:55 11/14/12 19:52
2 280353458 2012-11-14 15:12:05 11/14/12 18:12
3 280356593 2012-11-14 14:04:59 11/14/12 17:29
4 280364225 2012-11-14 19:08:28 11/14/12 22:45
5 280364279 2012-11-14 19:47:56 11/14/12 23:35
6 280364376 2012-11-14 19:29:09 11/14/12 23:01
> agg2 <- AGG2()
> head(agg2)
     custId      DelivDate
1 280336395 11/14/12 17:29
2 280353458 11/14/12 17:29
3 280356593 11/14/12 17:29
4 280364225 11/14/12 17:29
5 280364279 11/14/12 17:29
6 280364376 11/14/12 17:29
> agg2 <- DDPLY()
> head(agg2)
     custId             V1
1 280336395 11/14/12 19:52
2 280353458 11/14/12 18:12
3 280356593 11/14/12 17:29
4 280364225 11/14/12 22:45
5 280364279 11/14/12 23:35
6 280364376 11/14/12 23:01
4

4 に答える 4

10

私もdata.tableここをお勧めしますが、解決策を求めたので、ここにすべての列aggregateを組み合わせaggregateて取得するものがあります。merge

merge(events22, aggregate(saleDate ~ custId, events22, max))

またはaggregate、「custId」列と「DelivDate」列のみが必要な場合:

aggregate(list(DelivDate = events22$saleDate), 
          list(custId = events22$custId),
          function(x) events22[["DelivDate"]][which.max(x)])

最後に、以下を使用するオプションがありますsqldf

library(sqldf)
sqldf("select custId, DelivDate, max(saleDate) `saleDate` 
      from events22 group by custId")

ベンチマーク

私はベンチマークや専門家ではありませんが、ここで速くないdata.tableことに驚いています。私の疑惑は、たとえば40万行のデータセットなど、より大きなデータセットでは結果がまったく異なることです。とにかく、ここに@mnelの回答をモデルにしたベンチマークコードがあります。これにより、将来の参照用に実際のデータセットでいくつかのテストを実行できます。data.table

library(rbenchmark)

まず、ベンチマークしたいものに合わせて関数を設定します。

DDPLY <- function() { 
  x <- ddply(events22, .(custId), .inform = T, 
             function(x) {
               x[x$saleDate == max(x$saleDate),"DelivDate"]}) 
}
DATATABLE <- function() { x <- dt[, .SD[which.max(saleDate), ], by = custId] }
AGG1 <- function() { 
  x <- merge(events22, aggregate(saleDate ~ custId, events22, max)) }
AGG2 <- function() { 
  x <- aggregate(list(DelivDate = events22$saleDate), 
                 list(custId = events22$custId),
                 function(x) events22[["DelivDate"]][which.max(x)]) }
SQLDF <- function() { 
  x <- sqldf("select custId, DelivDate, max(saleDate) `saleDate` 
             from events22 group by custId") }
DOCALL <- function() {
  do.call(rbind, 
          lapply(split(events22, events22$custId), function(x){
            x[which.max(x$saleDate), ]
          })
  )
}

次に、ベンチマークを実行します。

benchmark(DDPLY(), DATATABLE(), AGG1(), AGG2(), SQLDF(), DOCALL(), 
          order = "elapsed")[1:5]
#          test replications elapsed relative user.self
# 4      AGG2()          100   0.285    1.000     0.284
# 3      AGG1()          100   0.891    3.126     0.896
# 6    DOCALL()          100   1.202    4.218     1.204
# 2 DATATABLE()          100   1.251    4.389     1.248
# 1     DDPLY()          100   1.254    4.400     1.252
# 5     SQLDF()          100   2.109    7.400     2.108
于 2012-12-27T04:27:53.330 に答える
7

ddplyとの間で最速aggregateだと思いますがaggregate、特にあなたが持っているような巨大なデータではそうでしょう。ただし、最速はですdata.table

require(data.table)
dt <- data.table(events22)
dt[, .SD[which.max(saleDate),], by=custId]

From ?data.table.SDdata.table、グループ列を除く、各グループのxのデータのサブセットを含みます。

于 2012-12-27T02:58:06.997 に答える
3

これはかなり速いはずですが、data.tableおそらくもっと速いでしょう:

do.call(rbind, 
    lapply(split(events22, events22$custId), function(x){
        x[which.max(x$saleDate), ]
    })
)
于 2012-12-27T04:42:38.913 に答える
2

これがはるかに高速なdata.table関数です。

DATATABLE <- function() { 
  dt <- data.table(events, key=c('custId', 'saleDate'))
  dt[, maxrow := 1:.N==.N, by = custId]
  return(dt[maxrow==TRUE, list(custId, DelivDate)])
}

この関数はdata.table、データを作成して並べ替えることに注意してください。これは、1回だけ実行する必要があるステップです。このステップを削除すると(おそらく、マルチステップのデータ処理パイプラインがありdata.table、最初のステップとして1回作成する)、関数は2倍以上高速になります。

また、比較を容易にするために、以前のすべての関数を変更して結果を返しました。

DDPLY <- function() { 
  return(ddply(events, .(custId), .inform = T, 
               function(x) {
                 x[x$saleDate == max(x$saleDate),"DelivDate"]}))
}
AGG1 <- function() { 
  return(merge(events, aggregate(saleDate ~ custId, events, max)))}

SQLDF <- function() { 
  return(sqldf("select custId, DelivDate, max(saleDate) `saleDate` 
             from events group by custId"))}
DOCALL <- function() {
  return(do.call(rbind, 
                 lapply(split(events, events$custId), function(x){
                   x[which.max(x$saleDate), ]
                 })
  ))
}

10回繰り返された10k行の結果は次のとおりです。

library(rbenchmark)
library(plyr)
library(data.table)
library(sqldf)
events <- do.call(rbind, lapply(1:500, function(x) events22))
events$custId <- sample(1:nrow(events), nrow(events))

benchmark(a <- DDPLY(), b <- DATATABLE(), c <- AGG1(), d <- SQLDF(),
 e <- DOCALL(), order = "elapsed", replications=10)[1:5]

              test replications elapsed relative user.self
2 b <- DATATABLE()           10    0.13    1.000      0.13
4     d <- SQLDF()           10    0.42    3.231      0.41
3      c <- AGG1()           10   12.11   93.154     12.03
1     a <- DDPLY()           10   32.17  247.462     32.01
5    e <- DOCALL()           10   56.05  431.154     55.85

すべての関数が結果を返すため、すべてが同じ答えを返すことを確認できます。

c <- c[order(c$custId),]
dim(a); dim(b); dim(c); dim(d); dim(e)
all(a$V1==b$DelivDate)
all(a$V1==c$DelivDate)
all(a$V1==d$DelivDate)
all(a$V1==e$DelivDate)

/編集:小さい20行のデータセットでdata.tableは、依然として最速ですが、マージンが狭くなります。

              test replications elapsed relative user.self
2 b <- DATATABLE()          100    0.22    1.000      0.22
3      c <- AGG1()          100    0.42    1.909      0.42
5    e <- DOCALL()          100    0.48    2.182      0.49
1     a <- DDPLY()          100    0.55    2.500      0.55
4     d <- SQLDF()          100    1.00    4.545      0.98

/ Edit2:data.table関数から作成を削除すると、次の結果が得られます。

dt <- data.table(events, key=c('custId', 'saleDate'))
DATATABLE2 <- function() { 
  dt[, maxrow := 1:.N==.N, by = custId]
  return(dt[maxrow==TRUE, list(custId, DelivDate)])
}
benchmark(a <- DDPLY(), b <- DATATABLE2(), c <- AGG1(), d <- SQLDF(),
           e <- DOCALL(), order = "elapsed", replications=10)[1:5]
              test replications elapsed relative user.self
2 b <- DATATABLE()           10    0.09    1.000      0.08
4     d <- SQLDF()           10    0.41    4.556      0.39
3      c <- AGG1()           10   11.73  130.333     11.67
1     a <- DDPLY()           10   31.59  351.000     31.50
5    e <- DOCALL()           10   55.05  611.667     54.91
于 2013-05-28T15:15:11.387 に答える