Displaying 2 results from an estimated 2 matches for "do_both".
Did you mean:
d1_both
2010 May 04
3
Idiomatic looping over list name, value pairs in R
Considering the python code:
for k, v in d.items(): do_something(k); do_something_else(v)
I have the following for R:
for (i in c(1:length(d))) { do_something(names(d[i]));
do_something_else(d[[i]]) }
This does not seem seems idiomatic. What is the best way of doing the
same with R?
Thanks.
Luis
2010 May 05
0
R-help Digest, Vol 87, Issue 5
...t;
> This does not seem seems idiomatic. What is the best way of doing the
> same with R?
>
You could do it as
for (name in names(d)) {
do_something(name)
do_something(d[[name]])
}
or
sapply(names(d), function(name) {
do_something(name)
do_something_else(d[[name]])
})
or
do_both <- function(name) {
do_something(name)
do_something_else(d[[name]])
}
sapply(names(d), do_both)
My choice would be the first version, but yours might differ.
Duncan Murdoch
------------------------------
Message: 41
Date: Tue, 4 May 2010 10:12:29 -0400
From: Abiel X Reinhart <abiel....