Reputation: 333
In R, once the following code is ran:
temp <- split(mtcars, mtcars$cyl)
If I send only "temp" to someone else ...
What code can he use to put back slices of "temp" together? He does not need to use "cyl" as column name; he can use whatever he wants. Thanks!
Upvotes: 2
Views: 1984
Reputation: 388962
You can use dplyr
's bind_rows
or data.table
's rbindlist
. To identify which rows come from which element of the list we can use .id
/idcol
parameter.
dplyr::bind_rows(temp, .id = 'id')
data.table::rbindlist(temp, idcol = 'id')
By default it assigns name of the list as id column, if you want them as numbers you can remove the names from the list using unname
.
dplyr::bind_rows(unname(temp), .id = 'id')
data.table::rbindlist(unname(temp), idcol = 'id')
Upvotes: 0
Reputation: 887068
We can use do.call
with rbind
, but the order or rows may be different
do.call(rbind, temp)
If the column info is known, then unsplit
can be useful as it will keep the same order as before the split
unsplit(temp, mtcars$cyl)
Upvotes: 7