Hello R-users, I have the following problem, that I want to solve efficiently: I have a named list, for example:> l <- list(a = 1, b = 3, c = 'asd') > l$a [1] 1 $b [1] 3 $c [1] "asd" I know that I can iterate through it using lapply() function, but I would also like to able to get the list names or some attributes of l in the lapply(). For example if I use names() function in the call of lapply() I get:> lapply(l, names)$a NULL $b NULL $c NULL My question is if I can get something like:> lapply(l, get_attr)$a [1] a $b [1] b $c [1] c I can do this very easy with a for() loop but my list is quite big and I would like to get a decent running time. I don't need only the attributes of the list(I can obtain them by using attributes() or attr() function), but for my list the names of the elements are given me information that I need. Also I must mention that the elements of the list can by of any type. Any solution is welcome. Many thanks, Adrian -- Adrian Alexa Max-Planck-Institut fuer Informatik Stuhlsatzenhausweg 85 Room 514 66123 Saarbruecken, Germany
Eric Lecoutre
2004-Nov-19 16:06 UTC
[R] accessing the attributes of a list inside lapply()
Hi, What about: > as.list(names(l)) [[1]] [1] "a" [[2]] [1] "b" [[3]] [1] "c" HTH, Eric At 15:59 19/11/2004, Adrian Alexa wrote:>Hello R-users, > > >I have the following problem, that I want to solve efficiently: > > >I have a named list, for example: > > > l <- list(a = 1, b = 3, c = 'asd') > > l >$a >[1] 1 > >$b >[1] 3 > >$c >[1] "asd" > > >I know that I can iterate through it using lapply() function, but I >would also like to able to get the list names or some attributes of l >in the lapply(). For example if I use names() function in the call of >lapply() I get: > > > > lapply(l, names) >$a >NULL > >$b >NULL > >$c >NULL > > >My question is if I can get something like: > > > > lapply(l, get_attr) >$a >[1] a > >$b >[1] b > >$c >[1] c > > >I can do this very easy with a for() loop but my list is quite big and >I would like to get a decent running time. I don't need only the >attributes of the list(I can obtain them by using attributes() or >attr() function), but for my list the names of the elements are given >me information that I need. Also I must mention that the elements of >the list can by of any type. > > >Any solution is welcome. > > > >Many thanks, > >Adrian > > > > >-- >Adrian Alexa >Max-Planck-Institut fuer Informatik >Stuhlsatzenhausweg 85 Room 514 >66123 Saarbruecken, Germany > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Spencer Graves
2004-Nov-19 16:16 UTC
[R] accessing the attributes of a list inside lapply()
Maybe I misunderstand your problem, but I wonder if you've considered "names": > l <- list(a = 1, b = 3, c = 'asd') > names(l) [1] "a" "b" "c" hope this helps. spencer graves p.s. I can't parse your "lapply(l, get_attr)". In R 1.9.1 and R 2.0.0 Patched, I get the following: > lapply(l, get_attr) Error in match.fun(FUN) : Object "get_attr" not found Adrian Alexa wrote:>Hello R-users, > > >I have the following problem, that I want to solve efficiently: > > >I have a named list, for example: > > > >>l <- list(a = 1, b = 3, c = 'asd') >>l >> >> >$a >[1] 1 > >$b >[1] 3 > >$c >[1] "asd" > > >I know that I can iterate through it using lapply() function, but I >would also like to able to get the list names or some attributes of l >in the lapply(). For example if I use names() function in the call of >lapply() I get: > > > > >>lapply(l, names) >> >> >$a >NULL > >$b >NULL > >$c >NULL > > >My question is if I can get something like: > > > > >>lapply(l, get_attr) >> >> >$a >[1] a > >$b >[1] b > >$c >[1] c > > >I can do this very easy with a for() loop but my list is quite big and >I would like to get a decent running time. I don't need only the >attributes of the list(I can obtain them by using attributes() or >attr() function), but for my list the names of the elements are given >me information that I need. Also I must mention that the elements of >the list can by of any type. > > >Any solution is welcome. > > > >Many thanks, > >Adrian > > > > > >-- Spencer Graves, PhD, Senior Development Engineer O: (408)938-4420; mobile: (408)655-4567