Skip to content

Using transform to add a date column to a dataframe

7 messages · Gavin Simpson, Gabor Grothendieck, Philipp Pagel +3 more

#
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:
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?
#
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
#
or not using transform at all:

data1 <- airquality
data1$Date <- as.Date("1950-01-01")

# or in just one line:

data1 <- replace(airquality, "Date", as.Date("1950-01-01"))
On Tue, Dec 23, 2008 at 9:06 AM, Gavin Simpson <gavin.simpson at ucl.ac.uk> wrote:
#
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')

cu
	Philipp
#
Gavin Simpson wrote:
Thanks. I did look at ?transform but I was a bit confused because this
worked

   data1 <- transform(airquality,LTMDA=T)

whereas this did not

  data1 <- transform(airquality,Date=as.Date("1950-01-01"))

Why does the first one work with one argument but the second one does not?
#
On Tue, 23 Dec 2008, Tom La Bone wrote:

            
What is "T"?  (If you mean 'TRUE', please say so, as "T" is a regular 
variable.)
A Date object is not a vector.  In released versions of R, data.frame() 
only knows how to replicate vectors and factors.

As Gavin pointed out, you were warned so why are you disregarding the 
warning?  That a subclass works does not mean that you are entitled to 
leap to conclusions about another subclass, does it?  Abductive inference 
does not apply (it would be like useRs considering you to be always in 
error based on your email address).
#
Prof Brian Ripley wrote:

            
That's a rather oblique way of saying that it actually does work with 
the unreleased version:

 > head(transform(airquality,Date=as.Date("1950-01-01")))
   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
 > R.version.string
[1] "R version 2.9.0 Under development (unstable) (2008-12-23 r47310)"