Jonathan Flowers
2011-Jan-15 21:06 UTC
[R] get list element names within lapply / sapply call
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]]
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 < jonathanmflowers@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]]
David Winsemius
2011-Jan-15 22:37 UTC
[R] get list element names within lapply / sapply call
On Jan 15, 2011, at 4:06 PM, Jonathan Flowers 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 > })You can pass this result to "[[": > names(sapply(mylist, names)) [1] "a" "b" "c" > sapply( names(sapply(mylist, names)) , function(x) mylist[[x]]^2) a b c [1,] 1 9 25 [2,] 4 16 36> > Thanks for your suggestions. > > Jonathan > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.David Winsemius, MD West Hartford, CT
David Winsemius
2011-Jan-15 22:42 UTC
[R] get list element names within lapply / sapply call
On Jan 15, 2011, at 5:37 PM, David Winsemius wrote:> > On Jan 15, 2011, at 4:06 PM, Jonathan Flowers 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 >> }) > > You can pass this result to "[[": > > names(sapply(mylist, names)) > [1] "a" "b" "c" > > > sapply( names(sapply(mylist, names)) , function(x) mylist[[x]]^2) > a b c > [1,] 1 9 25 > [2,] 4 16 36Does seem the long way around, though: > sapply( mylist, function(x) x^2) a b c [1,] 1 9 25 [2,] 4 16 36> > >> >> Thanks for your suggestions. >> >> Jonathan >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at 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. > > David Winsemius, MD > West Hartford, CT > > ______________________________________________ > R-help at 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.David Winsemius, MD West Hartford, CT