2015-07-27 8 views
6

Ich möchte alle n Elemente der Liste, cbind die Scheibe und dann rbind die Scheiben schneiden.Split-Liste alle n-Elemente und cbind, dann rbind Scheiben

Ich kann dies mit dem folgenden Code tun (n = 10 Elemente, Liste ist 30 Elemente lang). Ich 'manuell' wählen Sie alle 10 Elemente der Liste und dann cbind diese 10 Element Slices. Ich dann rbind diejenigen cbind Ed-Scheiben.

Aber ich denke, dass es einen Weg dazu mit l*ply in plyr oder dplyr oder zumindest etwas davon geben könnte. Für den Anfang, ich weiß jetzt nicht, wie Sie alle n Elemente der Liste auswählen, und scheinen nicht den richtigen Suchbegriff zu wissen, um diese Antwort zu finden.

dl <- list(c(2L, 1L, 3L, 2L, 1L, 1L, 3L), c(1L, 1L, 2L, 1L, 1L, 2L, 
1L), c(1L, 1L, 2L, 2L, 3L, 3L, 3L), c(1L, 1L, 2L, 2L, 3L, 3L, 
3L), c(1L, 1L, 2L, 2L, 3L, 3L, 3L), c(1L, 1L, 2L, 2L, 3L, 3L, 
1L), c(1L, 1L, 2L, 2L, 3L, 3L, 3L), c(1L, 3L, 2L, 1L, 3L, 2L, 
1L), c(3L, 1L, 2L, 3L, 3L, 1L, 3L), c(3L, 2L, 1L, 1L, 3L, 3L, 
1L), c(1L, 1L, 2L, 2L, 2L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, 
NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, 
NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, 
NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, 
NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(2L, 1L, 2L, 1L, 1L, NA, 
NA), c(2L, 3L, 1L, 2L, 1L, 2L, NA), c(1L, 1L, 2L, 2L, 1L, 3L, 
NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, 
NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, 
NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, 
NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, 
NA)) 

# slice list 'manually' cbind those slices 
dl1 <- dl[1:10] 
dl1.c <- do.call("cbind", dl1) 
dl2 <- dl[11:20] 
dl2.c <- do.call("cbind", dl2) 
dl3 <- dl[21:30] 
dl3.c <- do.call("cbind", dl3) 

# rbind the cbind slices for result 
ans <- as.data.frame(rbind(dl1.c, dl2.c, dl3.c)) # ans as df 
# ans <- rbind(dl1.c, dl2.c, dl3.c) 
+2

Vielleicht 'do.call Versuchen (mapply, c (cbind, Split (dl, Schnitt (seq_along (dl), 3))))' –

+1

auch 'do.call (rbind, lapply (split (dl, rep (1: (Länge (dl)% /% 10), jeweils = 10)), simplicate2array)) ' – jenesaisquoi

+1

@ StevenBeaupré diese Codezeile ergibt die gewünschte Ausgabe. Für mein Verständnis, dies wird 3 gleiche Teile (dh "Levels: (0.971.10.7) (10.7.20.3) (20.3,30)" basierend auf der Anzahl der Elemente in der Liste, Split-Liste mit durch diese Schnitte und Cbind sie in eine Liste von Listen, dann Mapply, 'rbinds' diese Listen. – nofunsally

Antwort

5

do.call(mapply, c(cbind, split(dl, cut(seq_along(dl), length(dl)/10, labels = FALSE)))) 
+2

schöne lösung –