简体   繁体   中英

Create a list from a dataframe in R

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 , ie 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.

I would appreciate any attempt of help! Thanks a lot!

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).

您可以使用以下代码:

sapply(union(test.df$a,NULL), function(y,x) x[x$a==y,], x=test.df, simplify=FALSE)

You could also use the dlply function in the plyr package:

> library(plyr)

> dlply(test.df, .(a))
$`1991-01-01`
           a          b         c
1 1991-01-01  1.3658775 0.9805356
2 1991-01-01 -0.2292211 2.2812914

$`1991-02-01`
           a          b         c
1 1991-02-01 -0.2678131 0.5323250
2 1991-02-01  0.3736910 0.4988308

Or the data.table package:

> library(data.table)

> setDT(test.df)
> dt <- test.df[, list(list(.SD)), by = a]$V1
> names(dt) <- unique(test.df$a)

> dt
$`1991-01-01`
            b         c
1:  1.3658775 0.9805356
2: -0.2292211 2.2812914

$`1991-02-01`
            b         c
1: -0.2678131 0.5323250
2:  0.3736910 0.4988308

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM