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

  • Home
  • SEARCH
  • 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 9219391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:06:26+00:00 2026-06-18T03:06:26+00:00

I have a list of data.frames . Within each data.frame I want to split

  • 0

I have a list of data.frames. Within each data.frame I want to split by a grouping (z) run a function, put the results back together, then put all the results of the of the nested lapply together in a data.frame, then flatten the list of result data.frames into one data.frame.

library(plyr)
df <- data.frame(x = sample(1:200, 30000, replace = TRUE), 
                y = sample(1:200, 30000, replace = TRUE), 
                z = sample(LETTERS, 30000, replace = TRUE))

alist <- list(df,df,df) # longer in real life
answer <- lapply(alist, function(q) {
    a <- split(q,q$z)
    result.1 <- lapply(a, function(w) {
        neww <- cbind(w[,1],w[,2])
        result.2 <- colSums(neww)
    })
    ldply(result.1)
})
# cor(neww) can actually be a variey of foos I just use cor() for easy reproducibility
ldply(answer)

This has some really tough memory usage and is also slow. Thanks to @Andrie I know how to clear my workspace before I start like:

 rm(list=setdiff(ls(), "alist"))

But is there a way to modify my approach like junking w in the second lapply etc to try reduce memory usage and speed things up? In this case foo likes a matrix and so data.table won’t be my answer. In other foos I will need all w and class will need to be a data.frame

  • 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-18T03:06:27+00:00Added an answer on June 18, 2026 at 3:06 am

    Try something like this:

    ldply(alist, ddply, "z", summarize, xy.foo = foo(x, y))
    

    If you want x and y to show up in your final data.frame, replace summarize with transform. Also, looking at your foo usage, you might have to replace (x, y) with cbind(x, y).

    Also, I would recommend you profile your code. In the end, foo might be what is slowing you down, not the split/combine part.

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

Sidebar

Related Questions

Let's say I have a list of data frames. Where each data frame has
I have a list within a data frame (in this example, df$candpct ), and
I have a list object that contains nested lists, each includes a data frame.
Possible Duplicate: write.csv() a list of unequally sized data.frames I have a data frame
I have a named list of data.frame objects ( training_data ). Each data.frame object
I have a list of data points (0.2, 0.8, 0.95) that I want to
I have a list of data that is a schedule. Each item has a
I have a list of data of the form: [line1,a] [line2,c] [line3,b] I want
I have a list and I want to extract to another list the data
I have a list of core data objects each has a longitude and latitude

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.