Hi Ian,
I have to confess that I'm not too familiar with the Date class.
Anyway, from what I've picked up from the help files, Date objects are
indeed internally represented as integers which mark the days since
1970-01-01.
Sys.Date()
unclass(Sys.Date())
Sys.Date() - unclass(Sys.Date())
Now, concerning cbind(), the help file reads:
The type of a matrix result determined from the highest type of
any of the inputs in the hierarchy:
raw < logical < integer < real < complex < character < list
Hence, there's no cbind() function for Date objects and the objects
are coerced to numeric. That's why you end up with numbers in your
matrix.
I guess I didn't understand the problem properly, but can't you
include the dates straight away into your date.frame()
sDate <- seq.Date(as.Date("2007/08/01"),as.Date("2007/08/30"),"4
days")
sDate
injuries <- 1:8
DF <- data.frame(date=sDate, injuries=injuries)
DF
If you have to go with cbind() what you could do, although it's not
all too elegant and more an ad-hoc solution, you could generate a
Date() object representing the "first" day.
origin <- as.Date("1970-01-01")
Generate the date matrix with cbind()
dMat <- cbind(sDate,sDate)
dMat
When you need you dates back, you extract them normally from dMat and
add them to the origin object.
origin + tab[3,2]
This way you end up with a Date object again.
I suppose you're running some functions over your matrix? You could
include this operation into this function then.
Hope that helps. If not maybe include some generated data which looks
like the one you're working with.
Cheers
Rob
--- In sportsci_rtutorial@yahoogroups.com, "Ian
Shrier" <ian.shrier@...> wrote:
>
> Rob
>
> I have run into a problem with manipulating a date vector. When I
try to
> cbind a date vector, it converts the date to a numerical vector. I
think
> this is because cbind creates a matrix, and matrix has to have
numbers if I
> remember correctly.
>
> I have a friend who is really good with R and he showed me that this
> represents the number of seconds since Jan 1, 1970. He was trying
to convert
> it with ISOdatetime and adding the number generated by cbind. This
converts
> it back to a date but we couldn't get it into a dataframe.
>
> To take a step back so this in context, i'm working with a multi-
relational
> database. One table has all the injuries of athletes with the date
of
> injury. The other table has all the treatment dates for each
injury. I'm
> using a function to look at the treatment table and pull out the
last
> treatment date for each injury and I want to put this date into the
> dataframe that has the list of injuries. This would give me one
table for
> injuries that includes the last treatment date as part of the
table. The
> date is then used to calculate number of days for the injury to
heal, and
> also will be used as the end-date when searching through a third
table that
> has exposures so I can count how many exposures from the injury
start date
> to end date.
>
> Any ideas?
>
> Ian
>