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
Christos Argyropoulos
2010-May-04 14:34 UTC
[R] Idiomatic looping over list name, value pairs in R
Can you give an example of what the python code is supposed to do? Some of us are not familiar with python, and the R code is not particularly informative. You seem to encode information on both the values and the names of the elements of the vector "d". If this is the case, why don't you create a data frame (or a matrix) and call apply on the columns? Christos> Date: Tue, 4 May 2010 23:24:11 +0900 > From: globophobe@gmail.com > To: r-help@r-project.org > Subject: [R] 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 > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code._________________________________________________________________ Hotmail: Powerful Free email with security by Microsoft. [[alternative HTML version deleted]]
Duncan Murdoch
2010-May-04 14:38 UTC
[R] Idiomatic looping over list name, value pairs in R
On 04/05/2010 10:24 AM, Luis N wrote:> 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? >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
Thank you. Your response was enlightening. On Tue, May 4, 2010 at 11:38 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> On 04/05/2010 10:24 AM, Luis N wrote: >> >> 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? >> > > 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
Seemingly Similar Threads
- InductiveRangeCheckElimination and BranchProbabilityInfo
- [LLVMdev] llvm.meta (was Rotated loop identification)
- InductiveRangeCheckElimination and BranchProbabilityInfo
- Enumerations (again): Comments please
- [LLVMdev] llvm.meta (was Rotated loop identification)