Search the web
Sign In
New User? Sign Up
sportsci_rtutorial · Sportsci.org R tutorial
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want to share photos of your group with the world? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
New file uploaded to sportsci_rtutorial   Message List  
Reply | Forward Message #37 of 61 |
RE: [sportsci_rtutorial] Re: New file uploaded to sportsci_rtutorial

Rob

Thanks for the help. This works well enough.

For the audience, I think those who use multi-relational databases might
find my problem and solution helpful.

First, I want to highlight a silly error. When trying to create a dataframe
of the date vector, I was using "as.data.frame" and this wasn't working.
Using cbind creates the matrix and coerces the date vector to a number, so
even when I converted the "numerical" date to a "date" date, I couldn't get
it into the dataframe. I then checked previous code I wrote to convert to
dataframes and the correct code is simply "data.frame" instead of
"as.data.frame".

Here is my story in more detail.

I have one table with information about injuries, including injury date, but
I don't have the enddate of the injury in that file. I have another table
with treatment dates for every subject for every injury. If I can get the
last treatment date for each injury, I can the merge this with the Injury
file and I would have all the information in one file and be able to work
from there.

So, I split the file by injuryID. This creates a list of tables. Each
component of the list is a table that has all the fields in the original
injury file but each table is limited to only one injuryId.

One can then use "lapply" to apply a function to each item in the list (i.e.
to each table). In my case, I took the last treatment in each table by using
the "tail" function.

However, using this method, the function returns a list where the name of
each component is the InjuryId and the value of each component is the last
treatment date (the class of the components is indeed date). But to merge
the information to the Injury file, I need a vector of dates associated with
the InjuryId, not a "list" of dates. So, first, I created a vector of the
InjuryId from the list using "as.numeric(names(listname))". This effectively
converts the listnames (in my case, the InjuryId) to a numeric vector. Now
came the problem I asked about.

Now, I had to get the components of the list (i.e. the actual dates) into a
vector. I then learned about "unlist", which actually worked well and
converts the components in a "list" to a vector. However, it converts the
dates to numerical vectors, just as "cbind" does. Using Rob's trick, I
simply added the number to the date 1970-01-01 and now the vector is in date
class. To link it to the InjuryId, you simply have to use
"data.frame(InjuryId,Datefield)" and it works, keeping the date in date
format (this is where I had used "as.data.frame" or "cbind" and it just kept
converting everything back to a number.

Now, I have a data.frame with InjuryId and the Lasttreatment date, and
another file with the InjuryId and all the information. The last step is to
merge the files, and the help file was pretty easy to use for that feature.

Ian


>-----Original Message-----
>From: sportsci_rtutorial@yahoogroups.com
>[mailto:sportsci_rtutorial@yahoogroups.com]On Behalf Of gerrobrein
>Sent: 19-Aug-2007 4:45 PM
>To: sportsci_rtutorial@yahoogroups.com
>Subject: [sportsci_rtutorial] Re: New file uploaded to
>sportsci_rtutorial
>
>
>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
>>
>
>
>
>
>
>Yahoo! Groups Links
>
>
>




Mon Aug 20, 2007 1:49 am

ianshrier
Offline Offline
Send Email Send Email

Forward
Message #37 of 61 |
Expand Messages Author Sort by Date

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
May 17, 2007
3:29 am

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
May 20, 2007
3:46 am

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
May 21, 2007
9:09 pm

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
May 23, 2007
10:13 pm

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
May 27, 2007
4:27 am

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
May 28, 2007
11:07 pm

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
May 31, 2007
4:26 am

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Jun 3, 2007
5:37 am

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Jun 5, 2007
6:06 am

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Jun 8, 2007
1:17 am

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Jun 23, 2007
5:49 pm

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Jul 11, 2007
11:50 am

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Jul 15, 2007
9:12 pm

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Jul 18, 2007
8:59 pm

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Jul 22, 2007
9:13 pm

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Aug 2, 2007
1:12 pm

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Aug 9, 2007
5:50 pm

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Aug 16, 2007
7:36 pm

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...
Ian Shrier
ianshrier
Offline Send Email
Aug 18, 2007
9:18 pm

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...
gerrobrein
Offline Send Email
Aug 19, 2007
9:24 pm

Rob Thanks for the help. This works well enough. For the audience, I think those who use multi-relational databases might find my problem and solution helpful....
Ian Shrier
ianshrier
Offline Send Email
Aug 20, 2007
6:24 am

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Nov 19, 2007
7:38 am

Rob, forgot to email you that the item about your tutorials is now up at http://sportsci.org. Will...
Will Hopkins
willhopkinsnz
Offline Send Email
Nov 20, 2007
8:27 am

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Nov 28, 2007
5:44 pm

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Feb 12, 2008
4:23 pm

Hello, This email message is a notification to let you know that a file has been uploaded to the Files area of the sportsci_rtutorial group. File :...
sportsci_rtutorial@ya...
Send Email
Apr 13, 2009
8:22 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help