I one tried to write a function to do that, but it wasn't worth it / didn't
work

I found this to be a better solution:
mynames = names(sapply(mylist, names))
for(nm in mynames){
    print(mylist[nm])
    # or "do other stuff"
}

You can use "browser" to look inside sapply, and the objects available don't
seem to have the current index information, but I don't know how to look at
the ...

> sapply(mylist, browser)
Called from: lapply(X, FUN, ...)
Browse[1]> ls(all.names=TRUE)
[1] "..." "FUN" "X"
Browse[1]> FUN
function (text = "", condition = NULL, expr = TRUE, skipCalls = 0L)
.Primitive("browser")
Browse[1]> X
$a
[1] 1 2

$b
[1] 3 4

$c
[1] 5 6

Browse[1]>


On Sat, Jan 15, 2011 at 3:06 PM, Jonathan Flowers <
jonathanmflow...@gmail.com> wrote:

> Hi all,
>
> I would like to iterate through a list with named elements and access the
> names within an lapply / sapply call. One way to do this is iterate through
> the names and index the list with the name.  Is there a way to iterate
> through the list elements themselves and access the element names within in
> the function? For example,
>
> mylist <- list("a"=c(1,2),"b"=c(3,4),"c"=c(5,6))
> sapply(mylist,function(x){
>  #get name of list elements ("a", "b", "c")
>  #then do other stuff
> })
>
> Thanks for your suggestions.
>
> Jonathan
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to