You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide rbind solution that can add list element names as a variable in the output #22
Problem: you have a list of data.frames and the element names convey information. You want to row bind them together and, in the new data.frame, you want a variable for the list element each observation originated in.
Demo: fragment subset of iris into separate data.frames, stored as list.
Note: Species info carried only via list names
No documentation yet, but unnest() should do now what you want.
I'm still struggling if unnest() should work on both lists and columns of data frames that are lists. That might be confusing, but it's basically the same behaviour albeit with slightly different output.
As a happy user of the grammar that runs through your packages, the words this task evokes for me are rbind and/or gather. Maybe unnest will seem more natural when I've worked with it more.
Morally, this operation seems like gathering variables into key-value pairs, with really different mechanics. Instead of different levels of a factor represented as separate variables in a data.frame, we've got them represented as separate data.frames.
I think it'll be more obvious why it's called unnest() once I sketch out the other pieces, and when you see what nest() does - they're fundamentally about lists of data frames and vectors, where spread() and gather() deal with columns.
This is what I would imagine to be the outcome of do.call( rbind.fill, df_list) but that's not what the code actually uses. It's an S3 method that seems to be first an rbind operation for the unmatched columns followed by (v)applying append column-wise matched on the basis of names. I wasn't really sure how the append_df would succeed, since it looked just like base::append with some attribute management (to handle Dates, datetimes, and factors presumably) but there is no append.data.frame. I was expecting some lapply(list, append_df), but it appears to be succeeding nonetheless, so probably it's just my confusion.
FWIW, I've removed this experimental method because I'm now pretty sure it's a bad idea (and dplyr::bind_rows() should do the equivalent in the next version)
So ... in case anyone else finds this in their search for adding a new column with bind_rows (or rbind_list or do.call(rbind, ...) and return a new column without inelegant convulsions... I'll finish out the example to demonstrate:
By
dplyr::bind_rows() should do the equivalent in the next version
What I gather is that (using dplyr 0.5.0) the argument .id can be used to specify the new column name, so following from the example #22 (comment)
P.S. thanks again for all of the great work you and your team are doing!
Reacted by linlin-cheng, Sean Davis, Obryan Poyser, Dan Skelly, map2085, Gerhard Burger, Clemens Schmid, Taylor Reiter, Andrew Breza, Tamas Andrei Foldes and 11 more
Reacted by Agile Bean, Jamie M. Kass, Karen Ro Ma and XiaonanLiu-TEU
Reacted by Obryan Poyser, Andrew Breza, Athanasia Monika Mowinckel and Karen Ro Ma
Reacted by map2085, Alison Presmanes Hill, stelsemeyer, Taylor Reiter, Marzia Antonella Scelsi, Karen Ro Ma, PatriciaQPan and Pietari Pöykkö
Is there a solution for the cases when rbind_rows will not accomplish this task if the constituent lists are of different length? In these cases I usually appeal to do.call(rbind, df) but there is obviously no .id argument to go along with that solution...
On Sep 28, 2017, at 10:14 AM, Jeff ***@***.***> wrote:
Is there a solution for the cases when rbind_rows will not accomplish this task if the constituent lists are of different length? In these cases I usually appeal to do.call(rbind, df) but there is obviously no .id argument to go along with that solution...
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
The dplyr package has a `bind_rows` function. Missing items are filled with NA.
Also see: rbind.fill in plyr-pkg.
Activity
hadley commentedon Aug 22, 2014
No documentation yet, but
unnest()should do now what you want.I'm still struggling if
unnest()should work on both lists and columns of data frames that are lists. That might be confusing, but it's basically the same behaviour albeit with slightly different output.jennybc commentedon Aug 23, 2014
Thank -- yes
unnest()does it!As a happy user of the grammar that runs through your packages, the words this task evokes for me are
rbindand/orgather. Maybeunnestwill seem more natural when I've worked with it more.Morally, this operation seems like
gathering variables into key-value pairs, with really different mechanics. Instead of different levels of a factor represented as separate variables in a data.frame, we've got them represented as separate data.frames.hadley commentedon Aug 23, 2014
I think it'll be more obvious why it's called
unnest()once I sketch out the other pieces, and when you see whatnest()does - they're fundamentally about lists of data frames and vectors, wherespread()andgather()deal with columns.jennybc commentedon Aug 23, 2014
I have every faith that it will be the most natural thing when you're done. :)
dwinsemius commentedon Sep 20, 2014
This is what I would imagine to be the outcome of
do.call( rbind.fill, df_list)but that's not what the code actually uses. It's an S3 method that seems to be first an rbind operation for the unmatched columns followed by (v)applyingappendcolumn-wise matched on the basis of names. I wasn't really sure how theappend_dfwould succeed, since it looked just likebase::appendwith some attribute management (to handle Dates, datetimes, and factors presumably) but there is noappend.data.frame. I was expecting somelapply(list, append_df), but it appears to be succeeding nonetheless, so probably it's just my confusion.hadley commentedon May 18, 2015
FWIW, I've removed this experimental method because I'm now pretty sure it's a bad idea (and
dplyr::bind_rows()should do the equivalent in the next version)hadley commentedon May 18, 2015
Hmmm, but maybe
unnest()needs to handle the case where a column of the data frame is a list of data frames:(and those data frames could contain lists-columns themselves, but I think you'd need a second unnest to handle that)
I'm not certain whether or not this is useful (it might crop up as an alternative way of handling relational data), but it is interesting.
hadley commentedon May 18, 2015
Oh that's #58
dlebauer commentedon Jan 6, 2017
So ... in case anyone else finds this in their search for adding a new column with
bind_rows(orrbind_listordo.call(rbind, ...)and return a new column without inelegant convulsions... I'll finish out the example to demonstrate:By
What I gather is that (using dplyr 0.5.0) the argument
.idcan be used to specify the new column name, so following from the example #22 (comment)returns
P.S. thanks again for all of the great work you and your team are doing!
d8aninja commentedon Sep 28, 2017
Is there a solution for the cases when
rbind_rowswill not accomplish this task if the constituent lists are of different length? In these cases I usually appeal todo.call(rbind, df)but there is obviously no.idargument to go along with that solution...jennybc commentedon Sep 28, 2017
@d8aninja Do you want to make a little example that shows exactly what you mean (I'm not entirely sure) and ask it over in the tidyverse section of community.rstudio.com? This question is a good fit.
d8aninja commentedon Oct 3, 2017
@jennybc will do, thanks!
dwinsemius commentedon Oct 12, 2017
agilebean commentedon Jun 1, 2019
@jennybc can you show in code how this works?
I tried this with throws an error:
gilbertmosr commentedon Mar 19, 2020
The built in method that works for me was to loop through the list and add the item name as a constant column.
After that you can apply the usual do.call as following:
mylist<- do.call(rbind,mylist)