简体   繁体   English

更改列表中列表的名称

[英]Change names of lists in list

I have list with elements "a" and "b" which are xts objects with length from 2010-01-01 to 2020-03-03我有元素“a”和“b”的列表,它们是长度从 2010-01-01 到 2020-03-03 的 xts 对象

>List
 $a         col1 col2
 2020-01-01 7.50 50000     
 2020-01-02 7.55 40000 
 $b         col1 col2
 2020-01-01 4.50 50000     
 2020-01-02 4.70 35000

Then i use split() function to split them by week and the output is this:然后我使用 split() function 按周拆分它们,output 是这样的:

>List
 $a[[1]]    col1  col2
 2020-01-01 7.50 50000
 $a[[2]]    col1  col2 
 2020-01-02 7.55 40000 
 $b[[1]]    col1  col2
 2020-01-01 4.50 50000
 $b[[2]]    col1  col2
 2020-01-02 4.70 35000

How can change lists names, because from this split function i get number to my elements name.如何更改列表名称,因为从这个拆分 function 我得到了我的元素名称的编号。 I am looking for this:我正在寻找这个:

 >List
  $a          col1  col2
  2020-01-01  7.50 50000 
  $a          col1  col2 
  2020-01-02  7.55 40000
  $b          col1  col2
  2020-01-01  4.50 50000 
  $b          col1  col2
  2020-01-02  4.70 35000

Maybe you can try the code below也许你可以试试下面的代码

Listout<- setNames(unlist(lapply(List, function(x) split(x,seq(nrow(x)))),recursive = FALSE),
                   rep(names(List),sapply(List,nrow)))

such that这样

> Listout
$a
           col1  col2
2020-01-01  7.5 50000

$a
           col1  col2
2020-01-02 7.55 40000

$b
           col1  col2
2020-01-01  4.5 50000

$b
           col1  col2
2020-01-02  4.7 35000

DATA数据

List <- list(a = data.frame(col1=c(7.5,7.55),col2 = c(5e4,4e4),row.names = c("2020-01-01","2020-01-02")),
             b = data.frame(col1=c(4.5,4.7),col2 = c(5e4,3.5e4),row.names = c("2020-01-01","2020-01-02")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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