简体   繁体   中英

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

>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:

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

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