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

Adrian Alexa adrian.alexa at gmail.com
Fri Nov 19 15:59:15 CET 2004


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




More information about the R-help mailing list