[R] Convert Factor to Date

Spencer Graves spencer.graves at effectivedefense.org
Tue Aug 22 21:30:25 CEST 2017



On 2017-08-22 2:04 PM, Patrick Casimir wrote:
>
> This is large data set Spencer. What about when the dates change as below:
>


       Have you tried what I suggested?  What were the results? Spencer

>
> COL1 	COL2
> Jan-14 	1-Aug-16
> Feb-14 	1-Aug-16
> Mar-14 	1-Aug-16
> Apr-14 	1-Aug-16
> May-14 	1-Aug-16
> Jun-14 	1-Aug-16
> Jul-14 	1-Aug-16
> Aug-14 	1-Aug-16
> Sep-14 	1-Aug-16
> Oct-14 	1-Aug-16
> Nov-14 	1-Aug-16
> Dec-14 	1-Aug-16
> Jan-15 	1-Aug-16
> Feb-15 	1-Aug-16
> Mar-15 	1-Aug-16
> Apr-15 	1-Aug-16
> May-15 	1-Aug-16
> Jun-15 	1-Aug-16
> Jul-15 	1-Aug-16
> Aug-15 	1-Aug-16
> Sep-15 	1-Aug-16
> Oct-15 	1-Aug-16
> Nov-15 	1-Aug-16
> Dec-15 	1-Aug-16
> Jan-16 	1-Aug-16
> Feb-16 	1-Aug-16
> Mar-16 	1-Aug-16
> Apr-16 	1-Aug-16
> May-16 	1-Aug-16
> Jun-16 	1-Aug-16
> Jul-16 	1-Aug-16
> Aug-16 	1-Aug-16
> Sep-16 	1-Aug-16
> Oct-16 	1-Aug-16
>
>
>
>
>
>
>
> ------------------------------------------------------------------------
> *From:* R-help <r-help-bounces at r-project.org> on behalf of Spencer 
> Graves <spencer.graves at effectivedefense.org>
> *Sent:* Tuesday, August 22, 2017 2:49 PM
> *To:* r-help at r-project.org
> *Subject:* Re: [R] Convert Factor to Date
>
>
> On 2017-08-22 1:30 PM, Patrick Casimir wrote:
> > Dear R Fellows,
> >
> >
> > I Have a dataset( data1) with 2 columns of date showing a class of 
> factor. How to convert them to date? Then compare them, keep the 
> greater date only in a new column. Using as.Date to change the class 
> to Date but the data becomes NA.
>
>
>        When I specified a format with the second date, I got the desired
> behavior:
>
>
>  > as.Date(factor('1-Nov-16'), '%d-%b-%y')
> [1] "2016-11-01"
>  > as.Date('Nov-16', '%b-%y')
> [1] NA
>  > as.Date(factor('Nov-16'), '%b-%y')
> [1] NA
>  > as.Date('Nov-16', '%b-%y')
> [1] NA
>
>
>        To convert the first column, I pasted "1-" in front:
>
>
> as.Date(paste0('1-', factor('Nov-16')), '%d-%b-%y')
>
>
>        Hope this helps.  Spencer
>
> > Much Thanks
> >
> >
> > COL1    COL2
> > Apr-16  1-Nov-16
> > May-16  1-Nov-16
> > Jun-16  1-Nov-16
> > Jul-16  1-Nov-16
> > Aug-16  1-Nov-16
> > Sep-16  1-Nov-16
> > Oct-16  1-Nov-16
> > Nov-16  1-Nov-16
> > Dec-16  1-Nov-16
> > Jan-17  1-Nov-16
> > Feb-17  1-Nov-16
> > Mar-17  1-Nov-16
> > Apr-17  1-Nov-16
> > May-17  1-Nov-16
> > Jun-17  1-Nov-16
> > Jul-17  1-Nov-16
> > Aug-17  1-Nov-16
> > Sep-17  1-Nov-16
> >
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 
> https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=02%7C01%7Cpatrcasi%40nova.edu%7C6abf3517ab5f407427d308d4e98e9efd%7C2c2b2d312e3e4df1b571fb37c042ff1b%7C0%7C0%7C636390246143633480&sdata=jwTeb%2BvH0bbkXdckgzE6PJZ3gDl9d1%2F3t9K%2BxDtjyls%3D&reserved=0
> > PLEASE do read the posting guide 
> https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&data=02%7C01%7Cpatrcasi%40nova.edu%7C6abf3517ab5f407427d308d4e98e9efd%7C2c2b2d312e3e4df1b571fb37c042ff1b%7C0%7C0%7C636390246143633480&sdata=GUAR582xxtA88KLkQC1oPnvyNecfUyXjV9MrIziJicU%3D&reserved=0
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=02%7C01%7Cpatrcasi%40nova.edu%7C6abf3517ab5f407427d308d4e98e9efd%7C2c2b2d312e3e4df1b571fb37c042ff1b%7C0%7C0%7C636390246143633480&sdata=jwTeb%2BvH0bbkXdckgzE6PJZ3gDl9d1%2F3t9K%2BxDtjyls%3D&reserved=0
> PLEASE do read the posting guide 
> https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&data=02%7C01%7Cpatrcasi%40nova.edu%7C6abf3517ab5f407427d308d4e98e9efd%7C2c2b2d312e3e4df1b571fb37c042ff1b%7C0%7C0%7C636390246143633480&sdata=GUAR582xxtA88KLkQC1oPnvyNecfUyXjV9MrIziJicU%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.


	[[alternative HTML version deleted]]



More information about the R-help mailing list