R tip iterating over list

To iterate over a list by key, value pair, do this:

for (name in names(myList)) {
    print(name)<br />
    print(myList[[name]])<br />
}

Note the double [[]] so you get just the value, not the pair.

2 comments

  1. Thanks for that snippet!

    I was able to write a script that allows me to list the levels associated with each of the named variables.

    Ian

  2. Very helpful. Better than the usual “LOOPS ARE EVIL!!111111″ instructions. Thanks!

Leave a comment