Skip to content
Prev 165397 / 398506 Next

Using transform to add a date column to a dataframe

On Tue, 2008-12-23 at 05:24 -0800, Tom La Bone wrote:
It says that the two arguments have different numbers of observations.
The reason for which should now be pretty obvious as you provided a
single Date whereas airquality has 153 observations.

You did read ?transform , which points out this "problem"? ;-)

Anyway, don't assume R recycles everything if it is not of sufficient
length to match other arguments. In this case, repeat the date as many
times as there are rows in airquality:
nrow(airquality)))
Ozone Solar.R Wind Temp Month Day       Date
1    41     190  7.4   67     5   1 1950-01-01
2    36     118  8.0   72     5   2 1950-01-01
3    12     149 12.6   74     5   3 1950-01-01
4    18     313 11.5   62     5   4 1950-01-01
5    NA      NA 14.3   56     5   5 1950-01-01
6    28      NA 14.9   66     5   6 1950-01-01

Also, the attach(airquality) call in your example doesn't do anything
that affects your example so is redundant.

HTH

G