Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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 - Fatal编程技术网

如何在r中的列表中收集相似的列表?

如何在r中的列表中收集相似的列表?,r,R,我有一份清单,里面有几个月的时间。我想把同样的月份列在一张清单上。列表中的一月元素、列表中的二月元素等 这是我的数据 $`01-2011` date V1 1 01-01-2011 1.48 2 01-02-2011 2.53 $`01-2012` date V1 366 01-01-2012 0.14 367 01-02-2012 1.18 $`02-2015` date V1 1493 02-01-2015 10.0

我有一份清单,里面有几个月的时间。我想把同样的月份列在一张清单上。列表中的一月元素、列表中的二月元素等

这是我的数据

$`01-2011`
        date   V1
1 01-01-2011 1.48
2 01-02-2011 2.53

$`01-2012`
          date   V1
366 01-01-2012 0.14
367 01-02-2012 1.18

$`02-2015`
           date    V1
1493 02-01-2015 10.06
1494 02-02-2015  0.51

$`02-2016`
           date   V1
1858 02-01-2016 0.00
1859 02-02-2016 2.28
和数据结构

mix<-list(`01-2011` = structure(list(date = c("01-01-2011", "01-02-2011"
), V1 = c(1.48, 2.53)), row.names = 1:2, class = "data.frame"), 
    `01-2012` = structure(list(date = c("01-01-2012", "01-02-2012"
    ), V1 = c(0.14, 1.18)), row.names = 366:367, class = "data.frame"), 
    `02-2015` = structure(list(date = c("02-01-2015", "02-02-2015"
    ), V1 = c(10.06, 0.51)), row.names = 1493:1494, class = "data.frame"), 
    `02-2016` = structure(list(date = c("02-01-2016", "02-02-2016"
    ), V1 = c(0, 2.28)), row.names = 1858:1859, class = "data.frame"))
out<-list(jan = list(`01-2011` = structure(list(date = c("01-01-2011", 
"01-02-2011"), V1 = c(1.48, 2.53)), row.names = 1:2, class = "data.frame"), 
    `01-2012` = structure(list(date = c("01-01-2012", "01-02-2012"
    ), V1 = c(0.14, 1.18)), row.names = 366:367, class = "data.frame")), 
    feb = list(`02-2015` = structure(list(date = c("02-01-2015", 
    "02-02-2015"), V1 = c(10.06, 0.51)), row.names = 1493:1494, class = "data.frame"), 
        `02-2016` = structure(list(date = c("02-01-2016", "02-02-2016"
        ), V1 = c(0, 2.28)), row.names = 1858:1859, class = "data.frame")))
期望输出结构

mix<-list(`01-2011` = structure(list(date = c("01-01-2011", "01-02-2011"
), V1 = c(1.48, 2.53)), row.names = 1:2, class = "data.frame"), 
    `01-2012` = structure(list(date = c("01-01-2012", "01-02-2012"
    ), V1 = c(0.14, 1.18)), row.names = 366:367, class = "data.frame"), 
    `02-2015` = structure(list(date = c("02-01-2015", "02-02-2015"
    ), V1 = c(10.06, 0.51)), row.names = 1493:1494, class = "data.frame"), 
    `02-2016` = structure(list(date = c("02-01-2016", "02-02-2016"
    ), V1 = c(0, 2.28)), row.names = 1858:1859, class = "data.frame"))
out<-list(jan = list(`01-2011` = structure(list(date = c("01-01-2011", 
"01-02-2011"), V1 = c(1.48, 2.53)), row.names = 1:2, class = "data.frame"), 
    `01-2012` = structure(list(date = c("01-01-2012", "01-02-2012"
    ), V1 = c(0.14, 1.18)), row.names = 366:367, class = "data.frame")), 
    feb = list(`02-2015` = structure(list(date = c("02-01-2015", 
    "02-02-2015"), V1 = c(10.06, 0.51)), row.names = 1493:1494, class = "data.frame"), 
        `02-2016` = structure(list(date = c("02-01-2016", "02-02-2016"
        ), V1 = c(0, 2.28)), row.names = 1858:1859, class = "data.frame")))

out使用
split

split(mix, month.abb[as.integer(sub('-.*', '', names(mix)))])

#$Feb
#$Feb$`02-2015`
#           date    V1
#1493 02-01-2015 10.06
#1494 02-02-2015  0.51

#$Feb$`02-2016`
#           date   V1
#1858 02-01-2016 0.00
#1859 02-02-2016 2.28


#$Jan
#$Jan$`01-2011`
#        date   V1
#1 01-01-2011 1.48
#2 01-02-2011 2.53

#$Jan$`01-2012`
#          date   V1
#366 01-01-2012 0.14
#367 01-02-2012 1.18

使用
sub
我们仅从列表(01,02)的名称中提取月份部分。我们将其转换为整数,使用
month。abb
获得相应的月份名称,并放入
拆分
,将月份组合在一起。

我们可以将
名称
zoo
转换为
yearmon
,然后通过获得前3个字符的
子字符串来执行
拆分

library(zoo)
split(mix, substr(as.yearmon(names(mix), '%m-%Y'), 1, 3))
#$Feb
#$Feb$`02-2015`
#           date    V1
#1493 02-01-2015 10.06
#1494 02-02-2015  0.51

#$Feb$`02-2016`
#           date   V1
#1858 02-01-2016 0.00
#1859 02-02-2016 2.28


#$Jan
#$Jan$`01-2011`
#        date   V1
#1 01-01-2011 1.48
#2 01-02-2011 2.53

#$Jan$`01-2012`
#          date   V1
#366 01-01-2012 0.14
#367 01-02-2012 1.18
$jan $jan$
01-2011
日期V1 1 01-01-2011 1.48 2 01-02-2011 2.53

$jan$
01-2012
日期V1 366 01-01-2012 0.14 367 01-02-2012 1.18

二月美元 $feb$
02-2015
日期V1 1493 02-01-2015 10.06 1494 02-02-2015 0.51

$feb$
02-2016
日期V1 1858 02-01-2016 0.00
1859 02-02-2016 2.28

你好,Benyahdou。请包含您为生成答案而编写的代码,以便提问者可以看到您是如何回答的。