foreach: Keep names
foreach, r
Solution
I was using your solution until I needed to use a nested foreach (with the `%:%` operator). I came up with this:
foo <- list(a = 1, b = 2)
bar <- foreach (x = foo, .final = function(x) setNames(x, names(foo))) %do% {
x * 2
}
The trick is to use the `.final` argument (which is a function applied once at the final result) to set the names. This is nicer because it doesn't use a temporary variable and is overall cleaner. It works with nested lists so that you can preserve the names accross several layers of the structure.
Note that this only works correctly if foreach has the argument `.inorder=T` (which is the default).
Problem
Is there a way to make foreach() return a named list/data.frame. E.g. ``` foo <- list(a = 1, b = 2) bar <- foreach (x = foo) %do% { x * 2 } ``` returns `list(2, 4)`. I would want it to return `list(a = 2, b = 4)`. Plus, is there a way to access the name from within the loop body? (I'm not interested in a solution which assigns the names after the foreach loop.) Regards