Re: [R] Converting a list to a data frame

2021-07-24 Thread Duncan Murdoch

On 24/07/2021 5:40 p.m., Rui Barradas wrote:

Hello,

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


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:




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


ggplot(NULL, aes(x=m[,"x"], y=m[,"y"])) + geom_point() # OK

Duncan Murdoch





Hope this helps,

Rui Barradas


Às 22:31 de 24/07/21, Jeff Reichman escreveu:

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 
Sent: Saturday, July 24, 2021 12:03 PM
To: reichm...@sbcglobal.net; R-help@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:

How does one convert a list into a data frame?




str(weight_chains)


List of 1

$ : 'mcmc' num [1:10, 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





[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2021-07-24 Thread Jeff Reichman
Thanks for the tips

-Original Message-
From: Rui Barradas  
Sent: Saturday, July 24, 2021 11:40 AM
To: reichm...@sbcglobal.net; R-help@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:
> How does one convert a list into a data frame?
> 
>   
> 
>> str(weight_chains)
> 
> List of 1
> 
> $ : 'mcmc' num [1:10, 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
> 
>   
> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2021-07-24 Thread Rui Barradas

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:

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 
Sent: Saturday, July 24, 2021 12:03 PM
To: reichm...@sbcglobal.net; R-help@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:

How does one convert a list into a data frame?

   


str(weight_chains)


List of 1

$ : 'mcmc' num [1:10, 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

   



[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2021-07-24 Thread Jeff Reichman
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  
Sent: Saturday, July 24, 2021 12:03 PM
To: reichm...@sbcglobal.net; R-help@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:
> How does one convert a list into a data frame?
> 
>   
> 
>> str(weight_chains)
> 
> List of 1
> 
> $ : 'mcmc' num [1:10, 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
> 
>   
> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2021-07-24 Thread Duncan Murdoch
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:

How does one convert a list into a data frame?

  


str(weight_chains)


List of 1

$ : 'mcmc' num [1:10, 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

  



[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2021-07-24 Thread Rui Barradas

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:

How does one convert a list into a data frame?

  


str(weight_chains)


List of 1

$ : 'mcmc' num [1:10, 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

  



[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2021-07-24 Thread Bert Gunter
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]]

> dex
   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 
wrote:

> How does one convert a list into a data frame?
>
>
>
> > str(weight_chains)
>
> List of 1
>
> $ : 'mcmc' num [1:10, 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
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Converting a list to a data frame

2021-07-24 Thread Jeff Reichman
How does one convert a list into a data frame?

 

> str(weight_chains)

List of 1

$ : 'mcmc' num [1:10, 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

 


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-04 Thread Bill Poling
Oh, how funny, hence the term Novice usR. UGH!

Thank you Sir.

WHP



From: Kevin E. Thorpe [mailto:kevin.tho...@utoronto.ca]
Sent: Friday, May 04, 2018 9:08 AM
To: Bill Poling <bill.pol...@zelis.com>; Huzefa Khalil <huzefa.kha...@umich.edu>
Cc: R Help Mailing List <r-help@r-project.org>
Subject: Re: [R] Converting a list to a data frame

It looks like you made a copy/paste error below. Your ata.frame should
be data.frame.

Kevin

On 05/04/2018 08:18 AM, Bill Poling wrote:
> Good morning.
>
> Novice usR. Here.
>
> I am following this string, among many, learning as I go.
>
> Quick question please?
>
> I thought that perhaps ata.frame was part of the zoo pkg, b/c when I
> searched it came up in help?
>
> However, evidently not or I am not using it properly.
>
> Please advise, thank you.
>
> x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>
> x2 <- do.call<http://do.call>(rbind, lapply(names(x), function(z)
>
> ata.frame(type=z, dat[[z]])))
>
> #Error in ata.frame(type = z, dat[[z]]) : could not find function
> "ata.frame"
>
> ?ata.frame
>
> ??ata.frame #Looks like it's part of the zoo package?
>
> install.packages("zoo")
>
> #Typo: dat[[z]] should be x[[z]]:
>
> x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>
> x2 <- do.call<http://do.call>(rbind, lapply(names(x), function(z)
>
> ata.frame(type=z, x[[z]])))
>
> #Error in ata.frame(type = z, dat[[z]]) : still cannot find function
> "ata.frame"?
>
> *William H. Poling, Ph.D.*
>
> *From:* R-help [mailto:r-help-boun...@r-project.org] *On Behalf Of
> *Huzefa Khalil
> *Sent:* Wednesday, May 02, 2018 1:24 PM
> *To:* Kevin E. Thorpe 
> <kevin.tho...@utoronto.ca<mailto:kevin.tho...@utoronto.ca>>
> *Cc:* R Help Mailing List <r-help@r-project.org<mailto:r-help@r-project.org>>
> *Subject:* Re: [R] Converting a list to a data frame
>
> Hi Kevin,
>
> There is probably a better way, but it can be done in two steps like this
>
> temp <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>
> temp <- lapply(names(temp), function(n, temp) {
> temp[[n]]$type <- n
> return(temp[[n]])
> }, temp = temp)
>
> do.call<http://do.call>(rbind, temp)
>
>
>
> On Wed, May 2, 2018 at 1:11 PM, Kevin E. Thorpe
> <kevin.tho...@utoronto.ca 
> <mailto:kevin.tho...@utoronto.ca<mailto:kevin.tho...@utoronto.ca%20%3cmailto:kevin.tho...@utoronto.ca>>>
> wrote:
>
> > I suspect this is pretty easy, but I'm having trouble figuring it out.
> > Basically, I have a list of data frames such as the following example:
> >
> > list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
> >
> > I would like to turn this into data frame where the list elements are
> > essentially rbind'ed together and the element name becomes a new
> variable.
> > For example, I would like to turn the list above into a data frame that
> > looks like this:
> >
> > data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
> >
> > Appreciate any pointers.
> >
> > Kevin
> >
> > --
> > Kevin E. Thorpe
> > Head of Biostatistics, Applied Health Research Centre (AHRC)
> > Li Ka Shing Knowledge Institute of St. Michael's Hospital
> > Assistant Professor, Dalla Lana School of Public Health
> > University of Toronto
> > email: kevin.tho...@utoronto.ca<mailto:kevin.tho...@utoronto.ca> 
> > <mailto:kevin.tho...@utoronto.ca>
> Tel: 416.864.5776 Fax: 416.864.3016


--
Kevin E. Thorpe
Head of Biostatistics, Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's Hospital
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.tho...@utoronto.ca<mailto:kevin.tho...@utoronto.ca> Tel: 
416.864.5776 Fax: 416.864.3016

Confidentiality Notice This message is sent from Zelis. ...{{dropped:15}}

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-04 Thread Bill Poling
Good morning.

Novice usR. Here.

I am following this string, among many, learning as I go.

Quick question please?

I thought that perhaps ata.frame was part of the zoo pkg, b/c when I searched 
it came up in help?

However, evidently not or I am not using it properly.

Please advise, thank you.
x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
x2 <- do.call(rbind, lapply(names(x), function(z)
ata.frame(type=z, dat[[z]])))
#Error in ata.frame(type = z, dat[[z]]) : could not find function "ata.frame"

?ata.frame
??ata.frame #Looks like it's part of the zoo package?
install.packages("zoo")

#Typo: dat[[z]] should be x[[z]]:

x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
x2 <- do.call(rbind, lapply(names(x), function(z)
ata.frame(type=z, x[[z]])))
#Error in ata.frame(type = z, dat[[z]]) : still cannot find function 
"ata.frame"?

William H. Poling, Ph.D.

From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Huzefa Khalil
Sent: Wednesday, May 02, 2018 1:24 PM
To: Kevin E. Thorpe <kevin.tho...@utoronto.ca>
Cc: R Help Mailing List <r-help@r-project.org>
Subject: Re: [R] Converting a list to a data frame

Hi Kevin,

There is probably a better way, but it can be done in two steps like this

temp <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))

temp <- lapply(names(temp), function(n, temp) {
temp[[n]]$type <- n
return(temp[[n]])
}, temp = temp)

do.call(rbind, temp)



On Wed, May 2, 2018 at 1:11 PM, Kevin E. Thorpe 
<kevin.tho...@utoronto.ca<mailto:kevin.tho...@utoronto.ca>>
wrote:

> I suspect this is pretty easy, but I'm having trouble figuring it out.
> Basically, I have a list of data frames such as the following example:
>
> list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>
> I would like to turn this into data frame where the list elements are
> essentially rbind'ed together and the element name becomes a new variable.
> For example, I would like to turn the list above into a data frame that
> looks like this:
>
> data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
>
> Appreciate any pointers.
>
> Kevin
>
> --
> Kevin E. Thorpe
> Head of Biostatistics, Applied Health Research Centre (AHRC)
> Li Ka Shing Knowledge Institute of St. Michael's Hospital
> Assistant Professor, Dalla Lana School of Public Health
> University of Toronto
> email: kevin.tho...@utoronto.ca<mailto:kevin.tho...@utoronto.ca> Tel: 
> 416.864.5776 Fax: 416.864.3016
>
> __
> R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To 
> UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide 
> http://www.R-project.org/posti<http://www.R-project.org/posti>
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To 
UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html<http://www.R-project.org/posting-guide.html>
and provide commented, minimal, self-contained, reproducible code.

Confidentiality Notice This message is sent from Zelis. ...{{dropped:15}}

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-04 Thread Kevin E. Thorpe
It looks like you made a copy/paste error below. Your ata.frame should 
be data.frame.


Kevin

On 05/04/2018 08:18 AM, Bill Poling wrote:

Good morning.

Novice usR. Here.

I am following this string, among many, learning as I go.

Quick question please?

I thought that perhaps ata.frame was part of the zoo pkg, b/c when I 
searched it came up in help?


However, evidently not or I am not using it properly.

Please advise, thank you.

x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))

x2 <- do.call(rbind, lapply(names(x), function(z)

ata.frame(type=z, dat[[z]])))

#Error in ata.frame(type = z, dat[[z]]) : could not find function 
"ata.frame"


?ata.frame

??ata.frame #Looks like it’s part of the zoo package?

install.packages("zoo")

#Typo: dat[[z]] should be x[[z]]:

x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))

x2 <- do.call(rbind, lapply(names(x), function(z)

ata.frame(type=z, x[[z]])))

#Error in ata.frame(type = z, dat[[z]]) : still cannot find function 
"ata.frame"?


*William H. Poling, Ph.D.*

*From:* R-help [mailto:r-help-boun...@r-project.org] *On Behalf Of 
*Huzefa Khalil

*Sent:* Wednesday, May 02, 2018 1:24 PM
*To:* Kevin E. Thorpe <kevin.tho...@utoronto.ca>
*Cc:* R Help Mailing List <r-help@r-project.org>
*Subject:* Re: [R] Converting a list to a data frame

Hi Kevin,

There is probably a better way, but it can be done in two steps like this

temp <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))

temp <- lapply(names(temp), function(n, temp) {
temp[[n]]$type <- n
return(temp[[n]])
}, temp = temp)

do.call(rbind, temp)



On Wed, May 2, 2018 at 1:11 PM, Kevin E. Thorpe 
<kevin.tho...@utoronto.ca <mailto:kevin.tho...@utoronto.ca>>

wrote:

 > I suspect this is pretty easy, but I'm having trouble figuring it out.
 > Basically, I have a list of data frames such as the following example:
 >
 > list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
 >
 > I would like to turn this into data frame where the list elements are
 > essentially rbind'ed together and the element name becomes a new 
variable.

 > For example, I would like to turn the list above into a data frame that
 > looks like this:
 >
 > data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
 >
 > Appreciate any pointers.
 >
 > Kevin
 >
 > --
 > Kevin E. Thorpe
 > Head of Biostatistics, Applied Health Research Centre (AHRC)
 > Li Ka Shing Knowledge Institute of St. Michael's Hospital
 > Assistant Professor, Dalla Lana School of Public Health
 > University of Toronto
 > email: kevin.tho...@utoronto.ca <mailto:kevin.tho...@utoronto.ca> 
Tel: 416.864.5776 Fax: 416.864.3016



--
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's Hospital
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-03 Thread William Dunlap via R-help
If you require that the 'type' column be a factor with a level for each
element of the input list, then you need to do that after calling
dplyr::bind_rows(), just as with the base-R solutions.

> l <- list( A = data.frame(X=1:2, Y=11:12), B = data.frame(X=integer(),
Y=integer()), C = data.frame(X=3L, Y=13L) )
> str(d <- dplyr::bind_rows(l, .id = "Which") )
'data.frame':   3 obs. of  3 variables:
 $ Which: chr  "A" "A" "C"
 $ X: int  1 2 3
 $ Y: int  11 12 13
> d$Which <- factor(d$Which, levels=names(l))
> str(d)
'data.frame':   3 obs. of  3 variables:
 $ Which: Factor w/ 3 levels "A","B","C": 1 1 3
 $ X: int  1 2 3
 $ Y: int  11 12 13

Sometimes you need the names of the of the 0-row data.frames in the output
to make plots, etc., comparable across various samples of the data.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, May 3, 2018 at 10:28 AM, Hadley Wickham  wrote:

> On Wed, May 2, 2018 at 11:53 AM, Jeff Newmiller
>  wrote:
> > Another approach:
> >
> > 
> > library(tidyr)
> > L <- list( A = data.frame( x=1:2, y=3:4 )
> >  , B = data.frame( x=5:6, y=7:8 )
> >  )
> > D <- data.frame( Type = names( L )
> >, stringsAsFactors = FALSE
> >)
> > D$data <- L
> > unnest(D, data)
> > #>   Type x y
> > #> 1A 1 3
> > #> 2A 2 4
> > #> 3B 5 7
> > #> 4B 6 8
> > 
>
> I think a slightly more idiomatic tidyverse solution is dplyr::bind_rows()
>
> l <- list(
>   A = data.frame(x = 1:2, y = 3:4),
>   B = data.frame(x = 5:6, y = 7:8)
> )
>
> dplyr::bind_rows(l, .id = "type")
> #>   type x y
> #> 1A 1 3
> #> 2A 2 4
> #> 3B 5 7
> #> 4B 6 8
>
> This also has the advantage of returning a data frame when the inputs
> are data frames.
>
> Hadley
>
> --
> http://hadley.nz
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-03 Thread Kevin E. Thorpe

On 05/03/2018 01:28 PM, Hadley Wickham wrote:

On Wed, May 2, 2018 at 11:53 AM, Jeff Newmiller
 wrote:

Another approach:


library(tidyr)
L <- list( A = data.frame( x=1:2, y=3:4 )
  , B = data.frame( x=5:6, y=7:8 )
  )
D <- data.frame( Type = names( L )
, stringsAsFactors = FALSE
)
D$data <- L
unnest(D, data)
#>   Type x y
#> 1A 1 3
#> 2A 2 4
#> 3B 5 7
#> 4B 6 8



I think a slightly more idiomatic tidyverse solution is dplyr::bind_rows()

l <- list(
   A = data.frame(x = 1:2, y = 3:4),
   B = data.frame(x = 5:6, y = 7:8)
)

dplyr::bind_rows(l, .id = "type")
#>   type x y
#> 1A 1 3
#> 2A 2 4
#> 3B 5 7
#> 4B 6 8

This also has the advantage of returning a data frame when the inputs
are data frames.

Hadley



I _clearly_ need to learn the dplyr package.

--
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's Hospital
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-03 Thread Hadley Wickham
On Wed, May 2, 2018 at 11:53 AM, Jeff Newmiller
 wrote:
> Another approach:
>
> 
> library(tidyr)
> L <- list( A = data.frame( x=1:2, y=3:4 )
>  , B = data.frame( x=5:6, y=7:8 )
>  )
> D <- data.frame( Type = names( L )
>, stringsAsFactors = FALSE
>)
> D$data <- L
> unnest(D, data)
> #>   Type x y
> #> 1A 1 3
> #> 2A 2 4
> #> 3B 5 7
> #> 4B 6 8
> 

I think a slightly more idiomatic tidyverse solution is dplyr::bind_rows()

l <- list(
  A = data.frame(x = 1:2, y = 3:4),
  B = data.frame(x = 5:6, y = 7:8)
)

dplyr::bind_rows(l, .id = "type")
#>   type x y
#> 1A 1 3
#> 2A 2 4
#> 3B 5 7
#> 4B 6 8

This also has the advantage of returning a data frame when the inputs
are data frames.

Hadley

-- 
http://hadley.nz

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-03 Thread David Winsemius

> On May 2, 2018, at 2:43 PM, David L Carlson <dcarl...@tamu.edu> wrote:
> 
> Typo: dat[[z]] should be x[[z]]:
> 
> x2 <- do.call(rbind, lapply(names(x), function(z) 
>  data.frame(type=z, x[[z]])))
> x2
>  type x y
> 1A 1 3
> 2A 2 4
> 3B 5 7
> 4B 6 8

Sometimes one want to "bind" list that have some columns in common and others 
that you want to include from one but leave as NA for the rows from the other. 
The rbind.list function from package-plyr is from the pre-tidy/tibble era of 
Hadley's efforts and provides that facility out of the box. It also seems to 
"work" with the do.call(rbind strategy for named lists above.

x <- list(A=mtcars[c("mpg", "wt")] , B=mtcars[c("wt", "cyl")])  #mtcars
x2 <- do.call(rbind.fill, lapply(names(x), function(z) 
 data.frame(type=z, x[[z]])))
str(x2)
#-
'data.frame':   64 obs. of  4 variables:
 type: Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
 mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 cyl : num  NA NA NA NA NA NA NA NA NA NA ...

-- 
David, the other.

> 
> David C
> 
> -Original Message-
> From: R-help <r-help-boun...@r-project.org> On Behalf Of David L Carlson
> Sent: Wednesday, May 2, 2018 3:51 PM
> To: William Dunlap <wdun...@tibco.com>; Kevin E. Thorpe 
> <kevin.tho...@utoronto.ca>
> Cc: r-help mailing list <r-help@r-project.org>
> Subject: Re: [R] Converting a list to a data frame
> 
> Or add the type column first and then rbind:
> 
> x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
> x2 <- do.call(rbind, lapply(names(x), function(z) 
>  data.frame(type=z, dat[[z]])))
> 
> 
> David L Carlson
> Department of Anthropology
> Texas A University
> College Station, TX 77843-4352
> 
> -Original Message-
> From: R-help <r-help-boun...@r-project.org> On Behalf Of William Dunlap via 
> R-help
> Sent: Wednesday, May 2, 2018 12:28 PM
> To: Kevin E. Thorpe <kevin.tho...@utoronto.ca>
> Cc: R Help Mailing List <r-help@r-project.org>
> Subject: Re: [R] Converting a list to a data frame
> 
>> x1 <- do.call(rbind, c(x, list(make.row.names=FALSE)))
>> x2 <- cbind(type=rep(names(x), vapply(x, nrow, 0)), x1)
>> str(x2)
> 'data.frame':   4 obs. of  3 variables:
> $ type: Factor w/ 2 levels "A","B": 1 1 2 2
> $ x   : int  1 2 5 6
> $ y   : int  3 4 7 8
> 
> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
> 
> On Wed, May 2, 2018 at 10:11 AM, Kevin E. Thorpe <kevin.tho...@utoronto.ca>
> wrote:
> 
>> I suspect this is pretty easy, but I'm having trouble figuring it out.
>> Basically, I have a list of data frames such as the following example:
>> 
>> list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>> 
>> I would like to turn this into  data frame where the list elements are 
>> essentially rbind'ed together and the element name becomes a new variable.
>> For example, I would like to turn the list above into a data frame 
>> that looks like this:
>> 
>> data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
>> 
>> Appreciate any pointers.
>> 
>> Kevin
>> 
>> --
>> Kevin E. Thorpe
>> Head of Biostatistics,  Applied Health Research Centre (AHRC) Li Ka 
>> Shing Knowledge Institute of St. Michael's Hospital Assistant 
>> Professor, Dalla Lana School of Public Health University of Toronto
>> email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
>> 
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posti 
>> ng-guide.html and provide commented, minimal, self-contained, 
>> reproducible code.
>> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.

Re: [R] Converting a list to a data frame

2018-05-03 Thread Kevin E. Thorpe

On 05/03/2018 03:59 AM, Martin Maechler wrote:

David L Carlson <dcarl...@tamu.edu>
 on Wed, 2 May 2018 21:43:52 + writes:



Typo: dat[[z]] should be x[[z]]:

x2 <- do.call(rbind, lapply(names(x), function(z)
 data.frame(type=z, x[[z]])))
x2
   type x y
1A 1 3
2A 2 4
3B 5 7
4B 6 8

David C


Before this thread gets carried away to data.table and
tibbleverse (and as nobody else has done so) :

Let me nominate this beautiful solution by
Bill Dunlap and David Carlson to win the prize  with a 10 out 10 grade:

Beautiful use of  do.call() and lapply(), two of the most
versatile and important functions from the base R toolbox.

Congratulations!

Martin Maechler
R Core Team


First of all, thank you to all who responded. I have learned (and will 
learn) about several new things. The data.table/tibbleverse is one of 
those places I have yet to tread but it never seems to be a "good" time 
to learn as generally when these tools would be helpful is when I am 
trying to get something done but don't have the time to recast the 
problem in a different paradigm.


Second, I echo Martin's comment. The combined solution of Bill Dunlap 
and David Carlson is beautifully elegant.


Thanks again,

Kevin




-Original Message-
From: R-help <r-help-boun...@r-project.org> On Behalf Of David L Carlson
Sent: Wednesday, May 2, 2018 3:51 PM
To: William Dunlap <wdun...@tibco.com>; Kevin E. Thorpe 
<kevin.tho...@utoronto.ca>
Cc: r-help mailing list <r-help@r-project.org>
Subject: Re: [R] Converting a list to a data frame

Or add the type column first and then rbind:

x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
x2 <- do.call(rbind, lapply(names(x), function(z)
   data.frame(type=z, dat[[z]])))


David L Carlson
Department of Anthropology
Texas A University
College Station, TX 77843-4352

-Original Message-
From: R-help <r-help-boun...@r-project.org> On Behalf Of William Dunlap via 
R-help
Sent: Wednesday, May 2, 2018 12:28 PM
To: Kevin E. Thorpe <kevin.tho...@utoronto.ca>
Cc: R Help Mailing List <r-help@r-project.org>
Subject: Re: [R] Converting a list to a data frame


x1 <- do.call(rbind, c(x, list(make.row.names=FALSE)))
x2 <- cbind(type=rep(names(x), vapply(x, nrow, 0)), x1)
str(x2)

'data.frame':   4 obs. of  3 variables:
  $ type: Factor w/ 2 levels "A","B": 1 1 2 2
  $ x   : int  1 2 5 6
  $ y   : int  3 4 7 8


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, May 2, 2018 at 10:11 AM, Kevin E. Thorpe <kevin.tho...@utoronto.ca>
wrote:


I suspect this is pretty easy, but I'm having trouble figuring it out.
Basically, I have a list of data frames such as the following example:

list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))

I would like to turn this into  data frame where the list elements are
essentially rbind'ed together and the element name becomes a new variable.
For example, I would like to turn the list above into a data frame
that looks like this:

data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))

Appreciate any pointers.

Kevin




--
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's Hospital
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-03 Thread Martin Maechler
>>>>> David L Carlson <dcarl...@tamu.edu>
>>>>> on Wed, 2 May 2018 21:43:52 + writes:

> Typo: dat[[z]] should be x[[z]]:
> 
> x2 <- do.call(rbind, lapply(names(x), function(z) 
> data.frame(type=z, x[[z]])))
> x2
>   type x y
> 1A 1 3
> 2A 2 4
> 3B 5 7
> 4B 6 8
> 
> David C

Before this thread gets carried away to data.table and
tibbleverse (and as nobody else has done so) :

Let me nominate this beautiful solution by
Bill Dunlap and David Carlson to win the prize  with a 10 out 10 grade:

Beautiful use of  do.call() and lapply(), two of the most
versatile and important functions from the base R toolbox.

Congratulations!

Martin Maechler
R Core Team


> -Original Message-
> From: R-help <r-help-boun...@r-project.org> On Behalf Of David L Carlson
> Sent: Wednesday, May 2, 2018 3:51 PM
> To: William Dunlap <wdun...@tibco.com>; Kevin E. Thorpe 
> <kevin.tho...@utoronto.ca>
> Cc: r-help mailing list <r-help@r-project.org>
> Subject: Re: [R] Converting a list to a data frame
> 
> Or add the type column first and then rbind:
> 
> x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
> x2 <- do.call(rbind, lapply(names(x), function(z) 
>   data.frame(type=z, dat[[z]])))
> 
> 
> David L Carlson
> Department of Anthropology
> Texas A University
> College Station, TX 77843-4352
> 
> -Original Message-
> From: R-help <r-help-boun...@r-project.org> On Behalf Of William Dunlap via 
> R-help
> Sent: Wednesday, May 2, 2018 12:28 PM
> To: Kevin E. Thorpe <kevin.tho...@utoronto.ca>
> Cc: R Help Mailing List <r-help@r-project.org>
> Subject: Re: [R] Converting a list to a data frame
> 
> > x1 <- do.call(rbind, c(x, list(make.row.names=FALSE)))
> > x2 <- cbind(type=rep(names(x), vapply(x, nrow, 0)), x1)
> > str(x2)
> 'data.frame':   4 obs. of  3 variables:
>  $ type: Factor w/ 2 levels "A","B": 1 1 2 2
>  $ x   : int  1 2 5 6
>  $ y   : int  3 4 7 8
> 
> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
> 
> On Wed, May 2, 2018 at 10:11 AM, Kevin E. Thorpe <kevin.tho...@utoronto.ca>
> wrote:
> 
> > I suspect this is pretty easy, but I'm having trouble figuring it out.
> > Basically, I have a list of data frames such as the following example:
> >
> > list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
> >
> > I would like to turn this into  data frame where the list elements are 
> > essentially rbind'ed together and the element name becomes a new variable.
> > For example, I would like to turn the list above into a data frame 
> > that looks like this:
> >
> > data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
> >
> > Appreciate any pointers.
> >
> > Kevin
> >
> > --
> > Kevin E. Thorpe
> > Head of Biostatistics,  Applied Health Research Centre (AHRC) Li Ka 
> > Shing Knowledge Institute of St. Michael's Hospital Assistant 
> > Professor, Dalla Lana School of Public Health University of Toronto
> > email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
> >
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-03 Thread Tóth Dénes


On 05/03/2018 07:58 AM, Jeff Newmiller wrote:

This is very nice to learn about, Denis, but it seems only fair to point out 
that the result of rbindlist is not a data frame.  You can convert it to a data 
frame easily, but the copy and indexing semantics of data tables are quite 
different than data tables, which could be a real headache for someone not 
prepared for those differences. (To learn more, read the data tables vignette.)

Tibbles  (as produced by unnest in my previous response) are not data tables 
either, but they behave much more like data frames than data tables do.

On May 2, 2018 1:30:37 PM MDT, "Tóth Dénes"  wrote:



On 05/02/2018 07:11 PM, Kevin E. Thorpe wrote:

I suspect this is pretty easy, but I'm having trouble figuring it

out.

Basically, I have a list of data frames such as the following

example:


list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))

I would like to turn this into  data frame where the list elements

are

essentially rbind'ed together and the element name becomes a new
variable. For example, I would like to turn the list above into a

data

frame that looks like this:

data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))

Appreciate any pointers.


Hi Kevin,

data.table::rbindlist does exactly what you want in a very efficient
way:

library(data.table)
dat <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
rbindlist(dat, idcol = "type")


In response to Jeff's note, this a solution which results in a 
data.frame instead of a data.table:


library(data.table)
dat <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
dat <- rbindlist(dat, idcol = "type")

# traditional way
dat_df <- as.data.frame(dat)

# no copy (assignment not needed, memory-efficient)
setDF(dat)

Further amendments would be needed to transform the 'type' variable to a 
factor, if it is required.




Regards,
Denes




Kevin



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-02 Thread Jeff Newmiller
This is very nice to learn about, Denis, but it seems only fair to point out 
that the result of rbindlist is not a data frame.  You can convert it to a data 
frame easily, but the copy and indexing semantics of data tables are quite 
different than data tables, which could be a real headache for someone not 
prepared for those differences. (To learn more, read the data tables vignette.)

Tibbles  (as produced by unnest in my previous response) are not data tables 
either, but they behave much more like data frames than data tables do. 

On May 2, 2018 1:30:37 PM MDT, "Tóth Dénes"  wrote:
>
>
>On 05/02/2018 07:11 PM, Kevin E. Thorpe wrote:
>> I suspect this is pretty easy, but I'm having trouble figuring it
>out. 
>> Basically, I have a list of data frames such as the following
>example:
>> 
>> list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>> 
>> I would like to turn this into  data frame where the list elements
>are 
>> essentially rbind'ed together and the element name becomes a new 
>> variable. For example, I would like to turn the list above into a
>data 
>> frame that looks like this:
>> 
>> data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
>> 
>> Appreciate any pointers.
>
>Hi Kevin,
>
>data.table::rbindlist does exactly what you want in a very efficient
>way:
>
>library(data.table)
>dat <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>rbindlist(dat, idcol = "type")
>
>Regards,
>Denes
>
>
>> 
>> Kevin
>>
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-02 Thread David L Carlson
Typo: dat[[z]] should be x[[z]]:

x2 <- do.call(rbind, lapply(names(x), function(z) 
  data.frame(type=z, x[[z]])))
x2
  type x y
1A 1 3
2A 2 4
3B 5 7
4B 6 8

David C

-Original Message-
From: R-help <r-help-boun...@r-project.org> On Behalf Of David L Carlson
Sent: Wednesday, May 2, 2018 3:51 PM
To: William Dunlap <wdun...@tibco.com>; Kevin E. Thorpe 
<kevin.tho...@utoronto.ca>
Cc: r-help mailing list <r-help@r-project.org>
Subject: Re: [R] Converting a list to a data frame

Or add the type column first and then rbind:

x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
x2 <- do.call(rbind, lapply(names(x), function(z) 
  data.frame(type=z, dat[[z]])))


David L Carlson
Department of Anthropology
Texas A University
College Station, TX 77843-4352

-Original Message-
From: R-help <r-help-boun...@r-project.org> On Behalf Of William Dunlap via 
R-help
Sent: Wednesday, May 2, 2018 12:28 PM
To: Kevin E. Thorpe <kevin.tho...@utoronto.ca>
Cc: R Help Mailing List <r-help@r-project.org>
Subject: Re: [R] Converting a list to a data frame

> x1 <- do.call(rbind, c(x, list(make.row.names=FALSE)))
> x2 <- cbind(type=rep(names(x), vapply(x, nrow, 0)), x1)
> str(x2)
'data.frame':   4 obs. of  3 variables:
 $ type: Factor w/ 2 levels "A","B": 1 1 2 2
 $ x   : int  1 2 5 6
 $ y   : int  3 4 7 8


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, May 2, 2018 at 10:11 AM, Kevin E. Thorpe <kevin.tho...@utoronto.ca>
wrote:

> I suspect this is pretty easy, but I'm having trouble figuring it out.
> Basically, I have a list of data frames such as the following example:
>
> list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>
> I would like to turn this into  data frame where the list elements are 
> essentially rbind'ed together and the element name becomes a new variable.
> For example, I would like to turn the list above into a data frame 
> that looks like this:
>
> data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
>
> Appreciate any pointers.
>
> Kevin
>
> --
> Kevin E. Thorpe
> Head of Biostatistics,  Applied Health Research Centre (AHRC) Li Ka 
> Shing Knowledge Institute of St. Michael's Hospital Assistant 
> Professor, Dalla Lana School of Public Health University of Toronto
> email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti 
> ng-guide.html and provide commented, minimal, self-contained, 
> reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-02 Thread David L Carlson
Or add the type column first and then rbind:

x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
x2 <- do.call(rbind, lapply(names(x), function(z) 
  data.frame(type=z, dat[[z]])))


David L Carlson
Department of Anthropology
Texas A University
College Station, TX 77843-4352

-Original Message-
From: R-help <r-help-boun...@r-project.org> On Behalf Of William Dunlap via 
R-help
Sent: Wednesday, May 2, 2018 12:28 PM
To: Kevin E. Thorpe <kevin.tho...@utoronto.ca>
Cc: R Help Mailing List <r-help@r-project.org>
Subject: Re: [R] Converting a list to a data frame

> x1 <- do.call(rbind, c(x, list(make.row.names=FALSE)))
> x2 <- cbind(type=rep(names(x), vapply(x, nrow, 0)), x1)
> str(x2)
'data.frame':   4 obs. of  3 variables:
 $ type: Factor w/ 2 levels "A","B": 1 1 2 2
 $ x   : int  1 2 5 6
 $ y   : int  3 4 7 8


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, May 2, 2018 at 10:11 AM, Kevin E. Thorpe <kevin.tho...@utoronto.ca>
wrote:

> I suspect this is pretty easy, but I'm having trouble figuring it out.
> Basically, I have a list of data frames such as the following example:
>
> list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>
> I would like to turn this into  data frame where the list elements are 
> essentially rbind'ed together and the element name becomes a new variable.
> For example, I would like to turn the list above into a data frame 
> that looks like this:
>
> data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
>
> Appreciate any pointers.
>
> Kevin
>
> --
> Kevin E. Thorpe
> Head of Biostatistics,  Applied Health Research Centre (AHRC) Li Ka 
> Shing Knowledge Institute of St. Michael's Hospital Assistant 
> Professor, Dalla Lana School of Public Health University of Toronto
> email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti 
> ng-guide.html and provide commented, minimal, self-contained, 
> reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-02 Thread Tóth Dénes



On 05/02/2018 07:11 PM, Kevin E. Thorpe wrote:
I suspect this is pretty easy, but I'm having trouble figuring it out. 
Basically, I have a list of data frames such as the following example:


list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))

I would like to turn this into  data frame where the list elements are 
essentially rbind'ed together and the element name becomes a new 
variable. For example, I would like to turn the list above into a data 
frame that looks like this:


data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))

Appreciate any pointers.


Hi Kevin,

data.table::rbindlist does exactly what you want in a very efficient way:

library(data.table)
dat <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
rbindlist(dat, idcol = "type")

Regards,
Denes




Kevin



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-02 Thread Jeff Newmiller

Another approach:


library(tidyr)
L <- list( A = data.frame( x=1:2, y=3:4 )
 , B = data.frame( x=5:6, y=7:8 )
 )
D <- data.frame( Type = names( L )
   , stringsAsFactors = FALSE
   )
D$data <- L
unnest(D, data)
#>   Type x y
#> 1A 1 3
#> 2A 2 4
#> 3B 5 7
#> 4B 6 8


On Wed, 2 May 2018, Eivind K. Dovik wrote:


On Wed, 2 May 2018, Kevin E. Thorpe wrote:

I suspect this is pretty easy, but I'm having trouble figuring it out. 
Basically, I have a list of data frames such as the following example:


list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))

I would like to turn this into  data frame where the list elements are 
essentially rbind'ed together and the element name becomes a new variable. 
For example, I would like to turn the list above into a data frame that 
looks like this:


data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))

Appreciate any pointers.

Kevin


Hi, Kevin.

Here's code that will generate your desired data frame.

# List as provided
thelist <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
thelist

# Creating the type-vector
type <- c()
for(i in 1:length(thelist)){
 type <- c(type, rep(names(thelist)[i], sapply(thelist, nrow)[i]))
}

# Creating the data frame
df <- data.frame(type, do.call(rbind.data.frame, c(thelist, make.row.names = 
FALSE)))

df


Kind regards,
Eivind K. Dovik
Bergen, NO






--
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's Hospital
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html

and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-02 Thread Eivind K. Dovik

On Wed, 2 May 2018, Kevin E. Thorpe wrote:

I suspect this is pretty easy, but I'm having trouble figuring it out. 
Basically, I have a list of data frames such as the following example:


list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))

I would like to turn this into  data frame where the list elements are 
essentially rbind'ed together and the element name becomes a new variable. 
For example, I would like to turn the list above into a data frame that looks 
like this:


data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))

Appreciate any pointers.

Kevin


Hi, Kevin.

Here's code that will generate your desired data frame.

# List as provided
thelist <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
thelist

# Creating the type-vector
type <- c()
for(i in 1:length(thelist)){
  type <- c(type, rep(names(thelist)[i], sapply(thelist, nrow)[i]))
}

# Creating the data frame
df <- data.frame(type, do.call(rbind.data.frame, c(thelist, make.row.names 
= FALSE)))

df


Kind regards,
Eivind K. Dovik
Bergen, NO






--
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's Hospital
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-02 Thread William Dunlap via R-help
> x1 <- do.call(rbind, c(x, list(make.row.names=FALSE)))
> x2 <- cbind(type=rep(names(x), vapply(x, nrow, 0)), x1)
> str(x2)
'data.frame':   4 obs. of  3 variables:
 $ type: Factor w/ 2 levels "A","B": 1 1 2 2
 $ x   : int  1 2 5 6
 $ y   : int  3 4 7 8


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, May 2, 2018 at 10:11 AM, Kevin E. Thorpe 
wrote:

> I suspect this is pretty easy, but I'm having trouble figuring it out.
> Basically, I have a list of data frames such as the following example:
>
> list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>
> I would like to turn this into  data frame where the list elements are
> essentially rbind'ed together and the element name becomes a new variable.
> For example, I would like to turn the list above into a data frame that
> looks like this:
>
> data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
>
> Appreciate any pointers.
>
> Kevin
>
> --
> Kevin E. Thorpe
> Head of Biostatistics,  Applied Health Research Centre (AHRC)
> Li Ka Shing Knowledge Institute of St. Michael's Hospital
> Assistant Professor, Dalla Lana School of Public Health
> University of Toronto
> email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2018-05-02 Thread Huzefa Khalil
Hi Kevin,

There is probably a better way, but it can be done in two steps like this

temp <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))

temp <- lapply(names(temp), function(n, temp) {
  temp[[n]]$type <- n
  return(temp[[n]])
}, temp = temp)

do.call(rbind, temp)



On Wed, May 2, 2018 at 1:11 PM, Kevin E. Thorpe 
wrote:

> I suspect this is pretty easy, but I'm having trouble figuring it out.
> Basically, I have a list of data frames such as the following example:
>
> list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>
> I would like to turn this into  data frame where the list elements are
> essentially rbind'ed together and the element name becomes a new variable.
> For example, I would like to turn the list above into a data frame that
> looks like this:
>
> data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
>
> Appreciate any pointers.
>
> Kevin
>
> --
> Kevin E. Thorpe
> Head of Biostatistics,  Applied Health Research Centre (AHRC)
> Li Ka Shing Knowledge Institute of St. Michael's Hospital
> Assistant Professor, Dalla Lana School of Public Health
> University of Toronto
> email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Converting a list to a data frame

2018-05-02 Thread Kevin E. Thorpe
I suspect this is pretty easy, but I'm having trouble figuring it out. 
Basically, I have a list of data frames such as the following example:


list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))

I would like to turn this into  data frame where the list elements are 
essentially rbind'ed together and the element name becomes a new 
variable. For example, I would like to turn the list above into a data 
frame that looks like this:


data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))

Appreciate any pointers.

Kevin

--
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's Hospital
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2016-11-04 Thread Bert Gunter
I believe that a slightly more efficient way of doing this without
leaving base R is:

cbind(do.call(rbind,x), set = rep(seq_along(x), vapply(x,nrow,1)) )


Cheers,
Bert


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 Fri, Nov 4, 2016 at 5:50 AM, Charles Determan  wrote:
> Hi Kevin,
>
> There may be a more elegant way but the following do.call and lapply should
> solve your problem.
>
> do.call(rbind, lapply(seq(length(x)), function(i) data.frame(set=i,
> x[[i]])))
>
> Regards,
> Charles
>
> On Fri, Nov 4, 2016 at 7:37 AM, Kevin E. Thorpe 
> wrote:
>
>> There is probably a very simple elegant way to do this, but I have been
>> unable to find it. Here is a toy example. Suppose I have a list of data
>> frames like this.
>>
>>  print(x <- list('1'=data.frame(id=1:4,expand.grid(x1=0:1,x2=0:1)),'2'=
>> data.frame(id=5:8,expand.grid(x1=2:3,x2=2:3
>> $`1`
>>   id x1 x2
>> 1  1  0  0
>> 2  2  1  0
>> 3  3  0  1
>> 4  4  1  1
>>
>> $`2`
>>   id x1 x2
>> 1  5  2  2
>> 2  6  3  2
>> 3  7  2  3
>> 4  8  3  3
>>
>> The real application will have more than 2 elements so I'm looking for a
>> general approach. I basically want to rbind the data frames in each list
>> element and add a variable that adds the element name. In this example the
>> result would look something like this.
>>
>> rbind(data.frame(set='1',x[[1]]),data.frame(set='2',x[[2]]))
>>   set id x1 x2
>> 1   1  1  0  0
>> 2   1  2  1  0
>> 3   1  3  0  1
>> 4   1  4  1  1
>> 5   2  5  2  2
>> 6   2  6  3  2
>> 7   2  7  2  3
>> 8   2  8  3  3
>>
>> Obviously, for 2 elements the simple rbind works but I would like a
>> general solution for arbitrary length lists. Hopefully that is clear.
>>
>> Kevin
>>
>> --
>> Kevin E. Thorpe
>> Head of Biostatistics,  Applied Health Research Centre (AHRC)
>> Li Ka Shing Knowledge Institute of St. Michael's Hospital
>> Assistant Professor, Dalla Lana School of Public Health
>> University of Toronto
>> email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posti
>> ng-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2016-11-04 Thread Carlos Ortega
Hi,

You have also "rbindlist()" function in package "data.table" that does
exactly what you need.

Kind Regards,
Carlos Ortega
www.qualityexcellence.es



2016-11-04 13:37 GMT+01:00 Kevin E. Thorpe :

> There is probably a very simple elegant way to do this, but I have been
> unable to find it. Here is a toy example. Suppose I have a list of data
> frames like this.
>
>  print(x <- list('1'=data.frame(id=1:4,expand.grid(x1=0:1,x2=0:1)),'2'=
> data.frame(id=5:8,expand.grid(x1=2:3,x2=2:3
> $`1`
>   id x1 x2
> 1  1  0  0
> 2  2  1  0
> 3  3  0  1
> 4  4  1  1
>
> $`2`
>   id x1 x2
> 1  5  2  2
> 2  6  3  2
> 3  7  2  3
> 4  8  3  3
>
> The real application will have more than 2 elements so I'm looking for a
> general approach. I basically want to rbind the data frames in each list
> element and add a variable that adds the element name. In this example the
> result would look something like this.
>
> rbind(data.frame(set='1',x[[1]]),data.frame(set='2',x[[2]]))
>   set id x1 x2
> 1   1  1  0  0
> 2   1  2  1  0
> 3   1  3  0  1
> 4   1  4  1  1
> 5   2  5  2  2
> 6   2  6  3  2
> 7   2  7  2  3
> 8   2  8  3  3
>
> Obviously, for 2 elements the simple rbind works but I would like a
> general solution for arbitrary length lists. Hopefully that is clear.
>
> Kevin
>
> --
> Kevin E. Thorpe
> Head of Biostatistics,  Applied Health Research Centre (AHRC)
> Li Ka Shing Knowledge Institute of St. Michael's Hospital
> Assistant Professor, Dalla Lana School of Public Health
> University of Toronto
> email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Saludos,
Carlos Ortega
www.qualityexcellence.es

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Converting a list to a data frame

2016-11-04 Thread Charles Determan
Hi Kevin,

There may be a more elegant way but the following do.call and lapply should
solve your problem.

do.call(rbind, lapply(seq(length(x)), function(i) data.frame(set=i,
x[[i]])))

Regards,
Charles

On Fri, Nov 4, 2016 at 7:37 AM, Kevin E. Thorpe 
wrote:

> There is probably a very simple elegant way to do this, but I have been
> unable to find it. Here is a toy example. Suppose I have a list of data
> frames like this.
>
>  print(x <- list('1'=data.frame(id=1:4,expand.grid(x1=0:1,x2=0:1)),'2'=
> data.frame(id=5:8,expand.grid(x1=2:3,x2=2:3
> $`1`
>   id x1 x2
> 1  1  0  0
> 2  2  1  0
> 3  3  0  1
> 4  4  1  1
>
> $`2`
>   id x1 x2
> 1  5  2  2
> 2  6  3  2
> 3  7  2  3
> 4  8  3  3
>
> The real application will have more than 2 elements so I'm looking for a
> general approach. I basically want to rbind the data frames in each list
> element and add a variable that adds the element name. In this example the
> result would look something like this.
>
> rbind(data.frame(set='1',x[[1]]),data.frame(set='2',x[[2]]))
>   set id x1 x2
> 1   1  1  0  0
> 2   1  2  1  0
> 3   1  3  0  1
> 4   1  4  1  1
> 5   2  5  2  2
> 6   2  6  3  2
> 7   2  7  2  3
> 8   2  8  3  3
>
> Obviously, for 2 elements the simple rbind works but I would like a
> general solution for arbitrary length lists. Hopefully that is clear.
>
> Kevin
>
> --
> Kevin E. Thorpe
> Head of Biostatistics,  Applied Health Research Centre (AHRC)
> Li Ka Shing Knowledge Institute of St. Michael's Hospital
> Assistant Professor, Dalla Lana School of Public Health
> University of Toronto
> email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Converting a list to a data frame

2016-11-04 Thread Kevin E. Thorpe
There is probably a very simple elegant way to do this, but I have been 
unable to find it. Here is a toy example. Suppose I have a list of data 
frames like this.


 print(x <- 
list('1'=data.frame(id=1:4,expand.grid(x1=0:1,x2=0:1)),'2'=data.frame(id=5:8,expand.grid(x1=2:3,x2=2:3

$`1`
  id x1 x2
1  1  0  0
2  2  1  0
3  3  0  1
4  4  1  1

$`2`
  id x1 x2
1  5  2  2
2  6  3  2
3  7  2  3
4  8  3  3

The real application will have more than 2 elements so I'm looking for a 
general approach. I basically want to rbind the data frames in each list 
element and add a variable that adds the element name. In this example 
the result would look something like this.


rbind(data.frame(set='1',x[[1]]),data.frame(set='2',x[[2]]))
  set id x1 x2
1   1  1  0  0
2   1  2  1  0
3   1  3  0  1
4   1  4  1  1
5   2  5  2  2
6   2  6  3  2
7   2  7  2  3
8   2  8  3  3

Obviously, for 2 elements the simple rbind works but I would like a 
general solution for arbitrary length lists. Hopefully that is clear.


Kevin

--
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's Hospital
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Converting a list to a data frame or columns at the least

2009-05-26 Thread Farrel Buchinsky
I have a column in which dates and times are specified thus
m/d/ HH:MM:SS
Alas, some entries do not include the time and therefore are only
m/d/
so I used read.csv and specified that the relevant column should be read as
is and it remained as a character variable.
I then split the value on the space
split.dt.time -strsplit(teacher$Date.and.Time.of.Lesson, )
that gives me a list where each item on the list has two elements if the
time was specified and only 1 element if the time was not specified.
How do I take that list and make all the 1st elements go into one column and
all the second elements go into a second column; where there is no time I
would like the value to be missing (NA)
I tried playing around with do.call(rbind...

so I tried the following unsuccessfully
do.call(rbind,lapply(teacher$Date.and.Time.of.Lesson, function(i)
strsplit(i, )) )
rbind(strsplit (teacher$Date.and.Time.of.Lesson, ))
do.call(rbind(data.frame(strsplit (teacher$Date.and.Time.of.Lesson, 


Farrel Buchinsky

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.