更改R中列表的层次结构 [英] change the hierarchy of a list in R

查看:96
本文介绍了更改R中列表的层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的清单,

myList <- lapply(unique(diamonds$cut), function(x){
    lst <- lapply(unique(diamonds$color), function(y){
        dta <- diamonds[diamonds$cut == x & diamonds$color == y, ]
        lm(price ~ carat, data = dta)
    })
    names(lst) <- unique(diamonds$color)
    return(lst)
})
names(myList) <- unique(diamonds$cut)

结构是

> str(myList, max.level=2)
List of 5
 $ Ideal    :List of 7
  ..$ E:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ I:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ J:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ H:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ F:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ G:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ D:List of 12
  .. ..- attr(*, "class")= chr "lm"
 $ Premium  :List of 7
  ..$ E:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ I:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ J:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ H:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ F:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ G:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ D:List of 12
  .. ..- attr(*, "class")= chr "lm"
 $ Good     :List of 7
  ..$ E:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ I:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ J:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ H:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ F:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ G:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ D:List of 12
  .. ..- attr(*, "class")= chr "lm"
 $ Very Good:List of 7
  ..$ E:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ I:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ J:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ H:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ F:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ G:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ D:List of 12
  .. ..- attr(*, "class")= chr "lm"
 $ Fair     :List of 7
  ..$ E:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ I:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ J:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ H:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ F:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ G:List of 12
  .. ..- attr(*, "class")= chr "lm"
  ..$ D:List of 12
  .. ..- attr(*, "class")= chr "lm"

我想在具有myList之后对其进行重组,以使每个color都位于cut之前.我无法更改创建myList的代码. myList生成后,我必须这样做.一些帮助.

I want to restructure this after having myList so that Each color will comes before cut. I can not change the code that creates myList. I have to do it after myList is generated. Some Help plz.

推荐答案

这可能并不理想,但是只要列表结构是常规的,就可以使用嵌套的for循环:

It might not be ideal, but as long as the list structure is regular, you could use a nested for loop:

# get names of inner and outer lists
innerNames <- names(myList[[1]])
outerNames <- names(myList)

# creat new blank list
myList2 <- list()

# restructure
for(i in innerNames) {
  for(j in outerNames)
    if(j == outerNames[1]) myList2[[i]] <- myList[[j]][i]
    else myList2[[i]] <- c(myList2[[i]], myList[[j]][i])
  }
}

这篇关于更改R中列表的层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆