[R] Changing Date Variables as Continuous Variables

jim holtman jholtman at gmail.com
Sun Nov 4 02:58:52 CET 2012


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.
>
> Cheers,
> Jake
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Changing-Date-Variables-as-Continuous-Variables-tp4648354.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.




More information about the R-help mailing list