2017-01-23 1 views
0

Ok das ist sehr einfach, aber ich versuche, eine 'verschachtelte' (2-stufig) Liste in R. zu erstellenR - schaffen Multi-Level-Liste

Ich habe vier Dateien verwiesen wird dies mögen:

files=c('path-to-file1', 
     'path-to-file2', 
     'path-to-file3', 
     'path-to-file4') 

auf der anderen Seite, ich habe vier verschiedene Operationen, die ich für jede Datei ausführen müssen:

ops=c('operation1, 
     operation2, 
     operation3, 
     operation4') 

ich zwei tue „für“ Schleifen (eine über Dateien und über Operationen) und Daher muss ich eine zweistufige Liste füllen, die oder sein muss nisiert wie folgt aus:

- the 1st element of the first level is "file1"; 
    - the 1st element of the second level is "operation1" 
    - the 2nd element of the second level is "operation2" 
    - the 3nd element of the second level is "operation3" 
    - the 4nd element of the second level is "operation4" 

    - the 2nd element of the first level is "file2"; 
    - the 1st element of the second level is "operation1" 
    - the 2nd element of the second level is "operation2" 
    - the 3nd element of the second level is "operation3" 
    - the 4nd element of the second level is "operation4" 

    - and so on... 

Was ist der beste Weg, um eine Multi-Level-Liste wie folgt zu erstellen?

EDIT: Dies ist eine Art, was ich suche:

files=c('path-to-file1', 
     'path-to-file2', 
     'path-to-file3', 
     'path-to-file4') 

ops=c('operation1, 
     operation2, 
     operation3, 
     operation4') 

# Create empty list to hold "operations" objects 
list.ops = vector('list', length(ops)) 

# Create multi-level list lo hold both "files" and "operations" objects 
multi.list = ? (how to create it? see loops below) 

for (i in 1:length(files)){ 

    for (j in 1:length(ops)){ 

    (do some stuff) 

    list.ops[[j]] = result of some operations 

    } 
    multi.list[[i]][[j]] = list.ops[[j]] 
} 
+2

Was ist Betrieb? Ist es eine Funktion? Basierend auf Ihrem Beispiel ist es eine einzelne Zeichenfolge. Bitte zeigen Sie ein kleines reproduzierbares Beispiel für andere an Ihrem Problem zu arbeiten – akrun

+0

@akrun die Operation ist wirklich eine Reihe von mathematischen Berechnungen. Ziemlich schwierig, mit einem aussagekräftigen Datensatz zu reproduzieren, weil wir über Satellitenbilder sprechen, die riesig sind ... – thiagoveloso

+0

@akrun, Ich habe die Frage bearbeitet, indem ich ähnliche Schleifen wie in meinem Skript hinzugefügt habe. Hoffentlich wird es leichter zu verstehen ... – thiagoveloso

Antwort

1

Nach einigen Tests habe ich, was ich suchte.

Wie ich vermutete, war es ziemlich einfach. Ich brauchte nur zwei Listen mit den gewünschten Größen zu erstellen und sie wie in diesem Beispiel zu füllen:

# Define files 
files=c('path-to-file1','path-to-file2','path-to-file3','path-to-file4') 

# Create list to hold "files" objects 
f.list = vector('list', length(files)) 

# Define operations 
ops=c('operation1','operation2','operation3','operation4') 

# Create list to hold "ops" objects 
o.list = vector('list', length(ops)) 

# Now, iterate over files and operations  
for (i in 1:length(files)){ 

    for (j in 1:length(ops)){ 

    (do some stuff) 

    o.list[[j]] = result of some operations 

    } 
    f.list[[i]] = o.list 
}