2016-04-15 22 views
1

ich so,die Hierarchie einer Liste in R

haben Liste ändern
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) 

Die Struktur ist,

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

Ich möchte dies nach myList mit so restrukturieren, dass jeder color Willen kommt vor . Ich kann den Code, der myList erstellt, nicht ändern. Ich muss es tun, nachdem myList generiert wird. Einige helfen plz.

+0

ich die Lösung aus einer ähnlichen Frage bei http://stackoverflow.com gefunden/questions/34643746/how-can-i-reshape-a-listen-of-list-from-wide-to-long, vielen Dank, das wird mir jetzt helfen – TheRimalaya

Antwort

0

Es ist vielleicht nicht ideal, aber solange die Listenstruktur regelmäßig ist, können Sie eine verschachtelte for-Schleife verwenden:

# 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]) 
    } 
}