[R] assign object to list

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jan 20 13:58:34 CET 2006


On Fri, 20 Jan 2006, Prof Brian Ripley wrote:

> On Fri, 20 Jan 2006, Christian Bieli wrote:
>
>> Dear all
>>
>> I want to generate a list like this:
>>
>> a <- data.frame(1:10)
>> attr(a,'myattribute') <- 'something'
>> b <- data.frame(11:20)
>> attr(b,'myattribute') <- 'anything'
>> mylist <- list(a,b)
>>
>> Is there a way to place the dataframes into the list giving them the
>> attribute at the same time?
>
> Yes, the above worked.
>
>> lapply(mylist, attributes)
>> lapply(mylist, function(x) attr(x, "myattribute"))
>
> show you they are there.

Or perhaps you were looking for

list(structure(a, myattribute='something'),
      structure(b, myattribute='anything'))

?

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list