如何在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。请包含您为生成答案而编写的代码,以便提问者可以看到您是如何回答的。