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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:52:35+00:00 2026-05-22T00:52:35+00:00

Just had a conversation with coworkers about this, and we thought it’d be worth

  • 0

Just had a conversation with coworkers about this, and we thought it’d be worth seeing what people out in SO land had to say. Suppose I had a list with N elements, where each element was a vector of length X. Now suppose I wanted to transform that into a data.frame. As with most things in R, there are multiple ways of skinning the proverbial cat, such as as.dataframe, using the plyr package, comboing do.call with cbind, pre-allocating the DF and filling it in, and others.

The problem that was presented was what happens when either N or X (in our case it is X) becomes extremely large. Is there one cat skinning method that’s notably superior when efficiency (particularly in terms of memory) is of the essence?

  • 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-22T00:52:36+00:00Added an answer on May 22, 2026 at 12:52 am

    Since a data.frame is already a list and you know that each list element is the same length (X), the fastest thing would probably be to just update the class and row.names attributes:

    set.seed(21)
    n <- 1e6
    x <- list(x=rnorm(n), y=rnorm(n), z=rnorm(n))
    x <- c(x,x,x,x,x,x)
    
    system.time(a <- as.data.frame(x))
    system.time(b <- do.call(data.frame,x))
    system.time({
      d <- x  # Skip 'c' so Joris doesn't down-vote me! ;-)
      class(d) <- "data.frame"
      rownames(d) <- 1:n
      names(d) <- make.unique(names(d))
    })
    
    identical(a, b)  # TRUE
    identical(b, d)  # TRUE
    

    Update – this is ~2x faster than creating d:

    system.time({
      e <- x
      attr(e, "row.names") <- c(NA_integer_,n)
      attr(e, "class") <- "data.frame"
      attr(e, "names") <- make.names(names(e), unique=TRUE)
    })
    
    identical(d, e)  # TRUE
    

    Update 2 – I forgot about memory consumption. The last update makes two copies of e. Using the attributes function reduces that to only one copy.

    set.seed(21)
    f <- list(x=rnorm(n), y=rnorm(n), z=rnorm(n))
    f <- c(f,f,f,f,f,f)
    tracemem(f)
    system.time({  # makes 2 copies
      attr(f, "row.names") <- c(NA_integer_,n)
      attr(f, "class") <- "data.frame"
      attr(f, "names") <- make.names(names(f), unique=TRUE)
    })
    
    set.seed(21)
    g <- list(x=rnorm(n), y=rnorm(n), z=rnorm(n))
    g <- c(g,g,g,g,g,g)
    tracemem(g)
    system.time({  # only makes 1 copy
      attributes(g) <- list(row.names=c(NA_integer_,n),
        class="data.frame", names=make.names(names(g), unique=TRUE))
    })
    
    identical(f,g)  # TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just had a conversation with my manager relating to checkin\out policies on a
Here's an interesting question I never really thought about until I had a conversation
Just had a discussion at work about the merits of using PostgreSQL over MySQL
just had a general question about how to approach a certain problem I'm facing.
I just had a quick question about git - how backwards compatible are git
I just had a question about aligning two expand-collapse boxes and got an answer
Had a conversation about sitemaps with someone from marketing. It was stated that a
I just had a conversation with my lead developer who disagreed that unit tests
I had been using using PIL but I just found out that it doesn't
I just had a small conversation with a friend, and he had mentioned that

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.