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? I don't want to create all the dataframes in my workspace first. I tried it with parentheses {}, but it obviously did not work. Thanks in advance. Christian -- Christian Bieli, project assistant Institute of Social and Preventive Medicine University of Basel, Switzerland Steinengraben 49 CH-4051 Basel Tel.: +41 61 270 22 12 Fax: +41 61 270 22 25 christian.bieli at unibas.ch www.unibas.ch/ispmbs
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. What may have confused you is that the print method for data frames does not show arbitrary attributes.> I don't want to create all the dataframes in my workspace first. I tried > it with parentheses {}, but it obviously did not work. > > Thanks in advance. > Christian-- 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
if you have a list of data frames without the attribute and a vector/list of the values for this attributes: mapply(function(x,y) {attr(x,"names") <- y ; x}, list(data.frame(1:10), data.frame(11:20)), c("something", "anything"), SIMPLIFY=F) Christian Bieli a ??crit :>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? >I don't want to create all the dataframes in my workspace first. I tried >it with parentheses {}, but it obviously did not work. > >Thanks in advance. >Christian > > >