[R] Using transform to add a date column to a dataframe

Philipp Pagel p.pagel at wzw.tum.de
Tue Dec 23 15:31:25 CET 2008

> I would like to add a column to the airquality dataset that contains the date
> 1950-01-01 in each row. This method does not appear to work:
> > attach(airquality)
> > data1 <- transform(airquality,Date=as.Date("1950-01-01"))
> Error in data.frame(list(Ozone = c(41L, 36L, 12L, 18L, NA, 28L, 23L, 19L,  : 
>   arguments imply differing number of rows: 153, 1
> I can't decipher what the error message is trying to tell me. Any
> suggestions on how to do this?

You already got an answer solving your problem using transform
and rep. I would like to add that the automatic recycling would
have worked in this case:

airquality$Date <- as.Date('1950-01-01')


Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany

More information about the R-help mailing list