--- John Kane <jrkrideau at yahoo.ca> wrote:
> Date: Wed, 31 Jan 2007 08:32:03 -0500 (EST)
> From: John Kane <jrkrideau at yahoo.ca>
> Subject: Re: [R] Simple Date problems with cbind
> To: marc_schwartz at comcast.net
>
> My thanks to Marc, Tony Plate, Phil Spector & Mark
> Leeds.
>
> I totally missed the significance of the Y not y
> issue
> even after reading ?strftime.
>
> Just after posting I realised why my date was
> getting
> coerced to a numeric. Thanks for the various
> alternatives for creating the data.frame that gets
> around this.
>
> john
>
>
> --- Marc Schwartz <marc_schwartz at comcast.net> wrote:
>
> > On Tue, 2007-01-30 at 15:27 -0500, John Kane
> wrote:
> > > I am clearly misunderstanding something about
> > dates
> > > and my reading of the help and RSiteSearch have
> > not
> > > turned up anything.
> > >
> > > I have a variable of class "Date" and I want to
> > add
> > > include it in a data.frame. However when do a
> > cbind
> > > the date var is coerced into a numeric.
> > >
> > > However when I tried to create a example I also
> > seem
> > > to be doing something wrong as I cannot seem
> even
> > to
> > > create a date class var even when I try to copy
> an
> > > example from the Help
> > >
> > > Example from Help
> > > dates <- c("02/27/92", "02/27/92",
"01/14/92",
> > > "02/28/92", "02/01/92")
> > > max <-as.Date(dates, "%m/%d/%y")
> > > max
> > > class(max)
> > >
> > > Results
> > > > dates <- c("02/27/92", "02/27/92",
"01/14/92",
> > > "02/28/92", "02/01/92")
> > > > max <-as.Date(dates, "%m/%d/%y")
> > > > max
> > > [1] "1992-02-27" "1992-02-27"
"1992-01-14"
> > > "1992-02-28" "1992-02-01"
> > > > class(max)
> > > [1] "Date"
> > >
> > >
> > > My example
> > >
> > > cc <- c("2005/01/24" ,"2006/01/23"
> ,"2006/01/23",
> > > "2006/01/23")
> > > xx <- as.Date(cc, "%y/%m/%d")
> > > xx
> > > class(xx)
> >
> >
> > You need to use a capital "Y" for a four digit
> > year...
> >
> > See ?strftime for more information on date
> formats.
> >
> >
> > > Results
> > > > cc <- c("2005/01/24" ,"2006/01/23"
> > ,"2006/01/23",
> > > "2006/01/23")
> > > > xx <- as.Date(cc, "%y/%m/%d")
> > > > xx
> > > [1] NA NA NA NA
> > > > class(xx)
> > > [1] "Date"
> >
> >
> > > cc <- c("2005/01/24" ,"2006/01/23"
> ,"2006/01/23",
> > "2006/01/23")
> >
> > xx <- as.Date(cc, "%Y/%m/%d")
> >
> > > xx
> > [1] "2005-01-24" "2006-01-23"
"2006-01-23"
> > "2006-01-23"
> >
> > > class(xx)
> > [1] "Date"
> >
> >
> >
> > > And on to the cbind problem
> > >
> > > jj <- 1:5
> > > cbind(jj,max)
> > >
> > > jj max
> > > [1,] 1 8092
> > > [2,] 2 8092
> > > [3,] 3 8048
> > > [4,] 4 8093
> > > [5,] 5 8066
> > >
> > > I have tried various as.Date etc approcaes
> > >
> > > It is probably something blindingly simple but
> can
> > > anyone suggest something?
> > >
> > > Thanks
> >
> > In this case, you are trying to cbind() a numeric
> > vector and a Date
> > vector into a matrix. Since a matrix may only
> have
> > one data type, the
> > Date vector will be coerced to numeric.
> >
> > If you want mixed data types, you need to create a
> > data frame:
> >
> > jj <- 1:4
> > DF <- data.frame(jj, xx)
> >
> > > DF
> > jj xx
> > 1 1 2005-01-24
> > 2 2 2006-01-23
> > 3 3 2006-01-23
> > 4 4 2006-01-23
> >
> > > str(DF)
> > 'data.frame': 4 obs. of 2 variables:
> > $ jj: int 1 2 3 4
> > $ xx:Class 'Date' num [1:4] 12807 13171 13171
> > 13171
> >
> >
> > Alternatively, create an initial data frame with
> > 'jj' and then cbind()
> > 'xx':
> >
> > JJ <- data.frame(jj)
> >
> > > str(JJ)
> > 'data.frame': 4 obs. of 1 variable:
> > $ jj: int 1 2 3 4
> >
> > DF <- cbind(JJ, xx)
> >
> > > str(DF)
> > 'data.frame': 4 obs. of 2 variables:
> > $ jj: int 1 2 3 4
> > $ xx:Class 'Date' num [1:4] 12807 13171 13171
> > 13171
> >
> >
> > Once you create the initial data frame, cbind()
> will
> > then use the
> > appropriate approach based upon the first argument
> > already being a data
> > frame.
> >
> > HTH,
> >
> > Marc Schwartz
> >
> >
> >
>
>
> __________________________________________________
> Do You Yahoo!?
> protection around
> http://mail.yahoo.com
>