I'm trying to create separate data.frame
objects based on levels of a factor. So if I have:
df <- data.frame(
x=rnorm(25),
y=rnorm(25),
g=rep(factor(LETTERS[1:5]), 5)
)
how can I split df
into separate data.frame
s for each level of g
containing the corresponding x
and y
values? I can get most of the way there using split(df, df$g)
, but I'd like the each level of the factor to have its own data.frame
. What's the best way to do this?
Thanks.
I think that
split
does exactly what you want.Notice that X is a list of data frames, as seen by
str
:If you want individual object with the group g names you could assign the elements of X from
split
to objects of those names, though this seems like extra work when you can just index the data frames from the listsplit
creates.Edit Or even better than using
lapply
to assign to the global environment uselist2env
:Since
dplyr 0.8.0
, we can also usegroup_split
which has similar behavior asbase::split
It also comes with argument
keep
(which isTRUE
by default) to specify whether or not the grouped column should be kept or not.The difference between
base::split
anddplyr::group_split
is thatgroup_split
does not name the elements of the list based on grouping. Sowhereas
data