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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:43:05+00:00 2026-06-12T03:43:05+00:00

For each level of factor I need to extract values aggregated over all subsets

  • 0

For each level of factor I need to extract values aggregated over all subsets of data.frame except the current one. For example, there is a several subjects doing a reaction time task during several days, and I need to compute mean reaction time for all subjects and all days, but not including the subject for whom the mean is computed. Currently, I do it like this:

 library(lme4)
 ddply(sleepstudy, .(Subject, Days), summarise, 
       avg_rt = mean(sleepstudy[sleepstudy$Subject != Subject &
                   sleepstudy$Days == Days,"Reaction"]), .progress="text")

It works fine for small data sets, but for large ones it can be very slow. Is there a way to do it faster?

  • 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-12T03:43:07+00:00Added an answer on June 12, 2026 at 3:43 am
    #create big dataset
    n <- 1e4
    set.seed(1)
    sleepstudy <- data.frame(Reaction=rnorm(n),Subject=1:4,Days=sort(rep((1:(n/4)),4)))
    
    
    library(plyr)
    system.time(
      res <- ddply(sleepstudy, .(Subject, Days), summarise, 
                   avg_rt = mean(sleepstudy[sleepstudy$Subject != Subject &
                     sleepstudy$Days == Days,"Reaction"]))
    )
    #User      System      elapsed 
    #6.532       0.013       6.556  
    
    #use data.table for big datasets
    library(data.table)
    
    dt<- as.data.table(sleepstudy)
    system.time(
     {dt[,avg_rt:=mean(Reaction),by=Days];
      dt[,n:=.N,by=Days];
      dt[,avg_rt:=(avg_rt*n-Reaction)/(n-1)]}
    )
    #User      System      elapsed 
    #0.005       0.001       0.005 
    
    
    #test if results are equal
    dt2 <- as.data.table(res)
    setkey(dt2,Subject,Days)
    setkey(dt,Subject,Days)
    all.equal(dt[,avg_rt],dt2[,avg_rt])
    #[1] TRUE
    

    For really large datasets the speed gain should be more pronounced. I just couldn’t compare with larger datasets since ddply is so slow.

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

Sidebar

Related Questions

Each request in my web app can get one data access object instance (of
I'm trying to create separate data.frame objects based on levels of a factor. So
I have an R data frame containing a factor that I want to expand
I am developing a game with dozens of levels and each level has a
I have 10 top-level domains e.g. example1.com, example2.com, example3.com, etc, and each domain has
I try to create a two level mux which contains two wide muxes. Each
I have a table of categories. Each category can either be a root level
Each key in dictionary has list of MANY integers. I need to iterate through
I have a tree data structure that is L levels deep each node has
I'm trying to plot a bar chart in ggplot2 where each factor gets the

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.