[R] accessing the attributes of a list inside lapply()

Eric Lecoutre lecoutre at stat.ucl.ac.be
Fri Nov 19 17:06:57 CET 2004


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




More information about the R-help mailing list