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-bounces at r-project.org] On Behalf Of Huzefa Khalil Sent: Wednesday, May 02, 2018 1:24 PM To: Kevin E. Thorpe <kevin.thorpe at utoronto.ca> Cc: R Help Mailing List <r-help at 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.thorpe at utoronto.ca<mailto:kevin.thorpe at 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.thorpe at utoronto.ca<mailto:kevin.thorpe at utoronto.ca> Tel: 416.864.5776 Fax: 416.864.3016 > > ______________________________________________ > R-help at r-project.org<mailto:R-help at 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 at r-project.org<mailto:R-help at 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}}
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-bounces at r-project.org] *On Behalf Of > *Huzefa Khalil > *Sent:* Wednesday, May 02, 2018 1:24 PM > *To:* Kevin E. Thorpe <kevin.thorpe at utoronto.ca> > *Cc:* R Help Mailing List <r-help at 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.thorpe at utoronto.ca <mailto:kevin.thorpe at 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.thorpe at utoronto.ca <mailto:kevin.thorpe at 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.thorpe at utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016
Oh, how funny, hence the term Novice usR. UGH! Thank you Sir. WHP From: Kevin E. Thorpe [mailto:kevin.thorpe at utoronto.ca] Sent: Friday, May 04, 2018 9:08 AM To: Bill Poling <Bill.Poling at zelis.com>; Huzefa Khalil <huzefa.khalil at umich.edu> Cc: R Help Mailing List <r-help at 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-bounces at r-project.org] *On Behalf Of > *Huzefa Khalil > *Sent:* Wednesday, May 02, 2018 1:24 PM > *To:* Kevin E. Thorpe <kevin.thorpe at utoronto.ca<mailto:kevin.thorpe at utoronto.ca>> > *Cc:* R Help Mailing List <r-help at r-project.org<mailto:r-help at 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.thorpe at utoronto.ca <mailto:kevin.thorpe at utoronto.ca<mailto:kevin.thorpe at utoronto.ca%20%3cmailto:kevin.thorpe at 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.thorpe at utoronto.ca<mailto:kevin.thorpe at utoronto.ca> <mailto:kevin.thorpe at 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.thorpe at utoronto.ca<mailto:kevin.thorpe at utoronto.ca> Tel: 416.864.5776 Fax: 416.864.3016 Confidentiality Notice This message is sent from Zelis. ...{{dropped:15}}