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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:20:53+00:00 2026-06-11T17:20:53+00:00

I have a dataset composed of values obtained from studies and experiments. Experiments are

  • 0

I have a dataset composed of values obtained from studies and experiments. Experiments are nested within studies. I want to subsample the dataset so that only 1 experiment is represented for each study. I want to repeat this procedure 10,000 times, randomly drawing the 1 experiment each time, and then calculate some summary statistics for the values. Here is an example dataset:

df=data.frame(study=c(1,1,2,2,2,3,4,4),expt=c(1,2,1,2,3,1,1,2),value=runif(8))

I wrote the following function to do the above, but it is taking forever. Does anyone have any suggestions for streamlining this code? Thanks!

subsample=function(x,A) {
  subsample.list=sapply(1:A,function(m) {
    idx=ddply(x,c("study"),function(i) sample(1:nrow(i),1)) #Sample one experiment from each study
    x[paste(x$study,x$expt,sep="-") %in% paste(idx$study,idx$V1,sep="-"),"value"] } ) #Match the study-experiment combinations and retrieve values
  means.list=ldply(subsample.list,mean) #Calculate the mean of 'values' for each iteration
  c(quantile(means.list$V1,0.025),mean(means.list$V1),upper=quantile(means.list$V1,0.975)) } #Calculate overall means and 95% CIs
  • 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-11T17:20:54+00:00Added an answer on June 11, 2026 at 5:20 pm

    You can vectorise this way more (even using plyr), and go much much faster:

    function=yoursummary(x)c(quantile(x,0.025),mean(x),upper=quantile(x,0.975))
    subsampleX=function(x,M)
      yoursummary(
        aaply(
          daply(.drop_o=F,df,.(study),
            function(x)sample(x$value,M,replace=T)
          ),1,mean
        )
      )
    

    The trick here is to do all the sampling up front. If we want to sample M times, why not do all that while you have access to the study.

    Original code:

    > system.time(subsample(df,20000))
       user  system elapsed 
     123.23    0.06  124.74 
    

    New vectorised code:

    > system.time(subsampleX(df,20000))
       user  system elapsed 
       0.24    0.00    0.25 
    

    That’s about 500x faster.

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

Sidebar

Related Questions

I have a DataSet that I fill with values from a XML-file. I would
i have C# program, i have DataSet that i want to bind to crystal
I have a dataset that I read in from a complex xml structure.... here
I have a DataSet which I get a DataTable from that I am being
I have a dataset that has two tables in it. I want to do
The dataset have tables and columns details got from the XML file.I want to
I have a dataset that I need to filter by time. e.g. I want
I have a dataset that i am trying to set the default value as
I have a dataset with one datatable. Now I want to set a filter
I have a DataSet with some DataTables that are linked together with DataRelations (classic

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.