[R] Changing Date Variables as Continuous Variables
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/; 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@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.
Re: [R] Changing Date Variables as Continuous Variables
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 hogu...@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/; 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@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. __ R-help@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.
Re: [R] Changing Date Variables as Continuous Variables
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 hogu...@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/; 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. __ R-help@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.