Re: [R] List of lists to data frame?
I don't know if this is faster, but ... out - do.call(rbind, lapply(s, function(x)data.frame(x$category,x$name,as.vector(x$series ## You can then name the columns of out via names() Note: No fancy additional packages are required. -- Bert On Wed, Nov 16, 2011 at 6:39 PM, Kevin Burton rkevinbur...@charter.net wrote: Say I have the following data: s - list() s[[A]] - list(name=first, series=ts(rnorm(50), frequency=10, start=c(2000,1)), category=top) s[[B]] - list(name=second, series=ts(rnorm(60), frequency=10, start=c(2000,2)), category=next) If I use unlist since this is a list of lists I don't end up with a data frame. And the number of rows in the data frame should equal the number of time series entries. In the sample above it would be 110. I would expect that the name and category strings would be recycled for each row. My brute force code attempts to build the data frame by appending to the master data frame but like I said it is *very* slow. Kevin -Original Message- From: R. Michael Weylandt [mailto:michael.weyla...@gmail.com] Sent: Wednesday, November 16, 2011 5:26 PM To: rkevinbur...@charter.net Cc: r-help@r-project.org Subject: Re: [R] List of lists to data frame? unlist(..., recursive = F) Michael On Wed, Nov 16, 2011 at 6:20 PM, rkevinbur...@charter.net wrote: I would like to make the following faster: df - NULL for(i in 1:length(s)) { df - rbind(df, cbind(names(s[i]), time(s[[i]]$series), as.vector(s[[i]]$series), s[[i]]$category)) } names(df) - c(name, time, value, category) return(df) The s object is a list of lists. It is constructed like: s[[object]] - list(. . . . . .) where object would be the name associated with this list s[[i]]$series is a 'ts' object and s[[i]]$category is a name. Constructing this list is reasonably fast but to do some more processing on the data it would be easier if it were converted to a data frame. Right now the above code is unacceptably slow at converting this list of lists to a data frame. Any suggestions on how to optimize this are welcome. Thank you. Kevin [[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. __ 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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm __ 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.
[R] List of lists to data frame?
I would like to make the following faster: df - NULL for(i in 1:length(s)) { df - rbind(df, cbind(names(s[i]), time(s[[i]]$series), as.vector(s[[i]]$series), s[[i]]$category)) } names(df) - c(name, time, value, category) return(df) The s object is a list of lists. It is constructed like: s[[object]] - list(. . . . . .) where object would be the name associated with this list s[[i]]$series is a 'ts' object and s[[i]]$category is a name. Constructing this list is reasonably fast but to do some more processing on the data it would be easier if it were converted to a data frame. Right now the above code is unacceptably slow at converting this list of lists to a data frame. Any suggestions on how to optimize this are welcome. Thank you. Kevin [[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.
Re: [R] List of lists to data frame?
unlist(..., recursive = F) Michael On Wed, Nov 16, 2011 at 6:20 PM, rkevinbur...@charter.net wrote: I would like to make the following faster: df - NULL for(i in 1:length(s)) { df - rbind(df, cbind(names(s[i]), time(s[[i]]$series), as.vector(s[[i]]$series), s[[i]]$category)) } names(df) - c(name, time, value, category) return(df) The s object is a list of lists. It is constructed like: s[[object]] - list(. . . . . .) where object would be the name associated with this list s[[i]]$series is a 'ts' object and s[[i]]$category is a name. Constructing this list is reasonably fast but to do some more processing on the data it would be easier if it were converted to a data frame. Right now the above code is unacceptably slow at converting this list of lists to a data frame. Any suggestions on how to optimize this are welcome. Thank you. Kevin [[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. __ 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.
Re: [R] List of lists to data frame?
Say I have the following data: s - list() s[[A]] - list(name=first, series=ts(rnorm(50), frequency=10, start=c(2000,1)), category=top) s[[B]] - list(name=second, series=ts(rnorm(60), frequency=10, start=c(2000,2)), category=next) If I use unlist since this is a list of lists I don't end up with a data frame. And the number of rows in the data frame should equal the number of time series entries. In the sample above it would be 110. I would expect that the name and category strings would be recycled for each row. My brute force code attempts to build the data frame by appending to the master data frame but like I said it is *very* slow. Kevin -Original Message- From: R. Michael Weylandt [mailto:michael.weyla...@gmail.com] Sent: Wednesday, November 16, 2011 5:26 PM To: rkevinbur...@charter.net Cc: r-help@r-project.org Subject: Re: [R] List of lists to data frame? unlist(..., recursive = F) Michael On Wed, Nov 16, 2011 at 6:20 PM, rkevinbur...@charter.net wrote: I would like to make the following faster: df - NULL for(i in 1:length(s)) { df - rbind(df, cbind(names(s[i]), time(s[[i]]$series), as.vector(s[[i]]$series), s[[i]]$category)) } names(df) - c(name, time, value, category) return(df) The s object is a list of lists. It is constructed like: s[[object]] - list(. . . . . .) where object would be the name associated with this list s[[i]]$series is a 'ts' object and s[[i]]$category is a name. Constructing this list is reasonably fast but to do some more processing on the data it would be easier if it were converted to a data frame. Right now the above code is unacceptably slow at converting this list of lists to a data frame. Any suggestions on how to optimize this are welcome. Thank you. Kevin [[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. __ 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.
Re: [R] List of lists to data frame?
On Wed, Nov 16, 2011 at 9:39 PM, Kevin Burton rkevinbur...@charter.net wrote: Say I have the following data: s - list() s[[A]] - list(name=first, series=ts(rnorm(50), frequency=10, start=c(2000,1)), category=top) s[[B]] - list(name=second, series=ts(rnorm(60), frequency=10, start=c(2000,2)), category=next) If I use unlist since this is a list of lists I don't end up with a data frame. And the number of rows in the data frame should equal the number of time series entries. In the sample above it would be 110. I would expect that the name and category strings would be recycled for each row. My brute force code attempts to build the data frame by appending to the master data frame but like I said it is *very* slow. Appending is very slow, and should be avoided. Instead, create a data frame of the correct size before starting the loop, and add each new bit into the appropriate place. There may well be a more efficient solution (I don't quite understand what your objective is), but simply getting rid of the rbind() within a loop will help. Kevin -Original Message- From: R. Michael Weylandt [mailto:michael.weyla...@gmail.com] Sent: Wednesday, November 16, 2011 5:26 PM To: rkevinbur...@charter.net Cc: r-help@r-project.org Subject: Re: [R] List of lists to data frame? unlist(..., recursive = F) Michael On Wed, Nov 16, 2011 at 6:20 PM, rkevinbur...@charter.net wrote: I would like to make the following faster: df - NULL for(i in 1:length(s)) { df - rbind(df, cbind(names(s[i]), time(s[[i]]$series), as.vector(s[[i]]$series), s[[i]]$category)) } names(df) - c(name, time, value, category) return(df) The s object is a list of lists. It is constructed like: s[[object]] - list(. . . . . .) where object would be the name associated with this list s[[i]]$series is a 'ts' object and s[[i]]$category is a name. Constructing this list is reasonably fast but to do some more processing on the data it would be easier if it were converted to a data frame. Right now the above code is unacceptably slow at converting this list of lists to a data frame. Any suggestions on how to optimize this are welcome. Thank you. Kevin -- Sarah Goslee http://www.functionaldiversity.org __ 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.
Re: [R] List of lists to data frame?
On the face of it this looks like a job for ldply() in the plyr package which specialises in taking things apart and putting them back together. ldply() applies a function for each element of a list and then combine results into a data frame On 17 November 2011 04:53, Sarah Goslee sarah.gos...@gmail.com wrote: On Wed, Nov 16, 2011 at 9:39 PM, Kevin Burton rkevinbur...@charter.net wrote: Say I have the following data: s - list() s[[A]] - list(name=first, series=ts(rnorm(50), frequency=10, start=c(2000,1)), category=top) s[[B]] - list(name=second, series=ts(rnorm(60), frequency=10, start=c(2000,2)), category=next) If I use unlist since this is a list of lists I don't end up with a data frame. And the number of rows in the data frame should equal the number of time series entries. In the sample above it would be 110. I would expect that the name and category strings would be recycled for each row. My brute force code attempts to build the data frame by appending to the master data frame but like I said it is *very* slow. Appending is very slow, and should be avoided. Instead, create a data frame of the correct size before starting the loop, and add each new bit into the appropriate place. There may well be a more efficient solution (I don't quite understand what your objective is), but simply getting rid of the rbind() within a loop will help. [[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.