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 1076827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:29:16+00:00 2026-05-16T21:29:16+00:00

I have a simulation that has a huge aggregate and combine step right in

  • 0

I have a simulation that has a huge aggregate and combine step right in the middle. I prototyped this process using plyr’s ddply() function which works great for a huge percentage of my needs. But I need this aggregation step to be faster since I have to run 10K simulations. I’m already scaling the simulations in parallel but if this one step were faster I could greatly decrease the number of nodes I need.

Here’s a reasonable simplification of what I am trying to do:

library(Hmisc)

# Set up some example data
year <-    sample(1970:2008, 1e6, rep=T)
state <-   sample(1:50, 1e6, rep=T)
group1 <-  sample(1:6, 1e6, rep=T)
group2 <-  sample(1:3, 1e6, rep=T)
myFact <-  rnorm(100, 15, 1e6)
weights <- rnorm(1e6)
myDF <- data.frame(year, state, group1, group2, myFact, weights)

# this is the step I want to make faster
system.time(aggregateDF <- ddply(myDF, c("year", "state", "group1", "group2"),
                     function(df) wtd.mean(df$myFact, weights=df$weights)
                                 )
           )

All tips or suggestions are appreciated!

  • 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-05-16T21:29:17+00:00Added an answer on May 16, 2026 at 9:29 pm

    Instead of the normal R data frame, you can use a immutable data frame which returns pointers to the original when you subset and can be much faster:

    idf <- idata.frame(myDF)
    system.time(aggregateDF <- ddply(idf, c("year", "state", "group1", "group2"),
       function(df) wtd.mean(df$myFact, weights=df$weights)))
    
    #    user  system elapsed 
    # 18.032   0.416  19.250 
    

    If I was to write a plyr function customised exactly to this situation, I’d do something like this:

    system.time({
      ids <- id(myDF[c("year", "state", "group1", "group2")], drop = TRUE)
      data <- as.matrix(myDF[c("myFact", "weights")])
      indices <- plyr:::split_indices(seq_len(nrow(data)), ids, n = attr(ids, "n"))
    
      fun <- function(rows) {
        weighted.mean(data[rows, 1], data[rows, 2])
      }
      values <- vapply(indices, fun, numeric(1))
    
      labels <- myDF[match(seq_len(attr(ids, "n")), ids), 
        c("year", "state", "group1", "group2")]
      aggregateDF <- cbind(labels, values)
    })
    
    # user  system elapsed 
    # 2.04    0.29    2.33 
    

    It’s so much faster because it avoids copying the data, only extracting the subset needed for each computation when it’s computed. Switching the data to matrix form gives another speed boost because matrix subsetting is much faster than data frame subsetting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The situation is like this: I have an Observable Collection that has a bunch
I have a huge form that has around 110 fields columns (single row) that
FYI: I have edited this significantly since my first edition. This simulation has been
I have written a function using a quantum simulation class QuTIP that returns a
I'm thinking about a situation where I have an object Transaction, that has quite
Here's my situation. I have a graph that has different sets of data being
I have a simulation that uses OpenGL for drawing. While it is running, I
I'm programming an agent-based simulation and have decided that Boost's MultiIndex is probably the
I have a code base that deals with simulation of certain data. It involves
I have some code that causes the box2d physics simulation to stutter forever after

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.