Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R中的数据帧创建列表_R_List_Dataframe - Fatal编程技术网

从R中的数据帧创建列表

从R中的数据帧创建列表,r,list,dataframe,R,List,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)) 请注意,子集数据帧的行数可能不同 在我的实际示例中,列a是一个具有更多值的日期列 我将感激任何帮助的尝试!非常感谢 您可以使用split lst <- split(test.df, test.df$a) lst您可以使用以下代码: sapply(

考虑以下数据帧:

test.df <- data.frame(a = c("1991-01-01","1991-01-01","1991-02-01","1991-02-01"), b = rnorm(4), c = rnorm(4))
请注意,子集数据帧的行数可能不同

在我的实际示例中,列
a
是一个具有更多值的日期列


我将感激任何帮助的尝试!非常感谢

您可以使用
split

lst <- split(test.df, test.df$a)

lst您可以使用以下代码:

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

您还可以使用
plyr
软件包中的
dlply
功能:

> 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
> 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
数据表
包:

> 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
> 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
>库(data.table)
>setDT(test.df)
>dt名称(dt)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

您可以使用
split(test.df[,-1],test.df$a)
谢谢大家,这太棒了
lst
是输出的示例。