Skip to content

Converting a list to a data frame

8 messages · Jeff Reichman, Bert Gunter, Rui Barradas +1 more

#
How does one convert a list into a data frame?
List of 1

$ : 'mcmc' num [1:100000, 1:3] -105 -105 -105 -104 -103 ...

  ..- attr(*, "dimnames")=List of 2

  .. ..$ : NULL

  .. ..$ : chr [1:3] "a" "b" "s"

  ..- attr(*, "mcpar")= num [1:3] 1001 101000 1

- attr(*, "class")= chr "mcmc.list"

 

Such that ..
weight_chains



               a         b         s  

1     -104.72512 1.0141407  9.369227

2     -104.52297 1.0167432  9.131354

3     -104.72669 1.0139528  9.219877
#
Here is a reprex that does what I think you want:

ex <- list(mcmc = matrix(1:12, ncol = 3, byrow=TRUE),
           NULL, c("a","b", "s"))

dex <- data.frame(ex$mcmc)
names(dex) <- ex[[3]]
a  b  s
1  1  2  3
2  4  5  6
3  7  8  9
4 10 11 12

If this is not correct, you should provide a **plain text** reprex.

Of course, even if correct, this is not a template. The exact process will
depend on the structure of the list.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Jul 24, 2021 at 6:19 AM Jeff Reichman <reichmanj at sbcglobal.net>
wrote:

  
  
#
Hello,

This should do it:


as.data.frame(weight_chains$mcmc)


The only list member already has a dim attribute of length 2 and 
dimnames' 2nd member are the colnames, just coerce to df.

Hope this helps,

Rui Barradas

?s 14:18 de 24/07/21, Jeff Reichman escreveu:
#
Others have shown you how to extract the matrix and convert it to a 
dataframe.  My only addition is to suggest that you don't do this: 
matrix methods are often much more efficient than dataframe methods, so 
if you can work with the matrix without conversion, you'll often find 
things run a lot faster.

Duncan Murdoch
On 24/07/2021 9:18 a.m., Jeff Reichman wrote:
#
Duncan

I need to plot the results (ggplot2) and I'm thinking I can only use a data.frame object in ggplot2. It is a rath r large "list" over 1 million rows. It is possible to work with a matrix in ggplot2?

Jeff

-----Original Message-----
From: Duncan Murdoch <murdoch.duncan at gmail.com> 
Sent: Saturday, July 24, 2021 12:03 PM
To: reichmanj at sbcglobal.net; R-help at r-project.org
Subject: Re: [R] Converting a list to a data frame

Others have shown you how to extract the matrix and convert it to a dataframe.  My only addition is to suggest that you don't do this: 
matrix methods are often much more efficient than dataframe methods, so if you can work with the matrix without conversion, you'll often find things run a lot faster.

Duncan Murdoch
On 24/07/2021 9:18 a.m., Jeff Reichman wrote:
#
Thanks for the tips

-----Original Message-----
From: Rui Barradas <ruipbarradas at sapo.pt> 
Sent: Saturday, July 24, 2021 11:40 AM
To: reichmanj at sbcglobal.net; R-help at r-project.org
Subject: Re: [R] Converting a list to a data frame

Hello,

This should do it:


as.data.frame(weight_chains$mcmc)


The only list member already has a dim attribute of length 2 and dimnames' 2nd member are the colnames, just coerce to df.

Hope this helps,

Rui Barradas

?s 14:18 de 24/07/21, Jeff Reichman escreveu:
#
Hello,

No, it's not possible to work with a matrix in ggplot2.
Not even with an object of class "list".



l <- list(x=1:5, y=1:5)
d <- as.data.frame(l)
m <- as.matrix(d)

library(ggplot2)
ggplot(l, aes(x, y)) + geom_point()  # Error
ggplot(d, aes(x, y)) + geom_point()  # OK, as expected
ggplot(m, aes(x, y)) + geom_point()  # Error



Hope this helps,

Rui Barradas


?s 22:31 de 24/07/21, Jeff Reichman escreveu:
#
On 24/07/2021 5:40 p.m., Rui Barradas wrote:
If that's the main thing Jeff is doing, then he should convert to a 
dataframe.  But it *is* possible to work with the original matrix in 
ggplot2, just not very natural:
ggplot(NULL, aes(x=m[,"x"], y=m[,"y"])) + geom_point() # OK

Duncan Murdoch