[R] Changing Date Variables as Continuous Variables
Rolf Turner
rolf.turner at xtra.co.nz
Sun Nov 4 03:47:53 CET 2012
There is a phenomenon that occurs here which, it seems to me,
merits some emphasis. The glm() function appears to be perfectly
willing to take a variable of class "Date" and treat it as a continuous
variable.
Apparently what it does (on the basis of one little experiment that I
did) is convert the vector of dates to a vector of "Julian date" values,
taking the origin to be the minimum value of the Date vector.
I would have thought that the user would have to effect this conversion
her/himself. But not so; the software is so cleverly written that it's all
handled for the user.
The designers of R have thought of just about *everything*!
We think it 'mazing! :-)
cheers,
Rolf Turner
On 04/11/12 14:58, jim holtman wrote:
> Here is how to convert your column of factors into Dates:
>
>> x <- read.table(text = '2/10/2011
> + 2/20/2011
> + 3/4/2011')
>> # read in as factors
>> str(x)
> 'data.frame': 3 obs. of 1 variable:
> $ V1: Factor w/ 3 levels "2/10/2011","2/20/2011",..: 1 2 3
>> # convert to Date
>> x$date <- as.Date(as.character(x$V1), format = "%m/%d/%Y")
>> x
> V1 date
> 1 2/10/2011 2011-02-10
> 2 2/20/2011 2011-02-20
> 3 3/4/2011 2011-03-04
>> str(x)
> 'data.frame': 3 obs. of 2 variables:
> $ V1 : Factor w/ 3 levels "2/10/2011","2/20/2011",..: 1 2 3
> $ date: Date, format: "2011-02-10" "2011-02-20" "2011-03-04"
>
>
> On Sat, Nov 3, 2012 at 8:09 PM, hoguejm <hoguejm at gmail.com> wrote:
>> I am very new to R, so I apologize if this question is trivial.
>>
>> I have a row in my data of dates in the format mm/dd/yyyy; about 3500 rows.
>>
>> I am using this variable in a logistic regression model, and need to treat
>> it as continuous, not a factor as r has decided it is.
>>
>> I tried the as.numeric function but it resulted in all NA's and the message:
>> "NAs introduced by coercion"
>>
>> If anyone knows a solution, I would greatly appreciate it.
>>
More information about the R-help
mailing list