Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9047541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:56:09+00:00 2026-06-16T11:56:09+00:00

I have a data frame with 3 columns: custId, saleDate, DelivDateTime. > head(events22) custId

  • 0

I have a data frame with 3 columns: custId, saleDate, DelivDateTime.

> 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

Here’s the 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")

I’m trying to find the DelivDate for the most recent saleDate for each custId.

I can do that using plyr::ddply like this:

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

My question is whether there is a faster way to do this as the ddply method is a bit time consuming (the full data set is ~ 400k lines). I’ve looked at using aggregate() but don’t know how to get a value other than the one I’m sorting by.

Any suggestions?

EDIT:

Here’s the benchmark results for 10k lines @ 10 iterations:

      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 :
While being quickest AGG2() doesn’t give the correct answer.

    > 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:56:11+00:00Added an answer on June 16, 2026 at 11:56 am

    I, too, would recommend data.table here, but since you asked for an aggregate solution, here is one which combines aggregate and merge to get all the columns:

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

    Or just aggregate if you only want the “custId” and “DelivDate” columns:

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

    Finally, here’s an option using sqldf:

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

    Benchmarks

    I’m not a benchmarking or data.table expert, but it surprised me that data.table is not faster here. My suspicion is that the results would be quite different on a larger dataset, say for instance, your 400k lines one. Anyway, here’s some benchmarking code modeled after @mnel’s answer here so you can do some tests on your actual dataset for future reference.

    library(rbenchmark)
    

    First, set up your functions for what you want to benchmark.

    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), ]
              })
      )
    }
    

    Second, do the benchmarking.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame with 2 columns: Node A, Node B. Each entry in
I have a data frame with two columns. First column contains categories such as
I have a data frame where some of the columns contain NA values. How
I have a data frame with 50000 rows and 200 columns. There are duplicate
I have a data frame of daily data: df with four columns: Date, A,
I have a data frame with an id column and some (potentially many) columns
I have a data frame that is some 35,000 rows, by 7 columns. it
Let's assume that we have a data frame x which contains the columns job
I have two columns in a data frame, and I have been able to
I have a data frame with columns that, when concatenated (row-wise) as a string,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.