Consider the following dataframe:
test.df <- data.frame(a = c("1991-01-01","1991-01-01","1991-02-01","1991-02-01"), b = rnorm(4), c = rnorm(4))
I would like to create a list from test.df
. Each element of the list would be a subset dataframe of test.df
corresponding to a specific value of column a
, i.e. each date. In other words, in this case, column a
takes unique values 1991-01-01
and 1991-02-01
. Therefore, the resulting list would be comprised of two elements: the subset of test.df
when a = 1991-01-01
(excluding column a
), and the other element of the list would be the subset of test.df
when 1991-02-01 = 2
(excluding column a
).
Here is the output I am looking for:
lst <- list(test.df[1:2,2:3], test.df[3:4,2:3])
Note that the subset dataframes may not have the same number of rows.
In my real practical example, column a
is a date column with many more values.
What can I try next?
You can use split
lst <- split(test.df, test.df$a)
If you want to get rid of column a
, use split(test.df[-1], test.df$a)
(thanks to @akrun for comment).