[R] determine the year of a date

carol white wht_crl at yahoo.com
Wed Feb 3 20:31:48 CET 2016


yes, some of them are like 09/01/15 and some others are 09/01/2015 and all of them range between 2015 and 2016. The goal was to convert 09/01/15 to 09/01/2015. I don't know if the fact that they range between 2015 and 2016 make the task easier.
Best wishes
Carol

    On Wednesday, February 3, 2016 7:34 PM, Boris Steipe <boris.steipe at utoronto.ca> wrote:
 

 Sorry - messed up example: corrected here...

d <- "7/27/77"
strptime(d, format="%m/%d/%y")  #  "1977-07-27 EDT"
x <- strptime(d, format="%m/%d/%y")

strftime(x, format="%m/%d/%Y")  # "07/27/1977"


On Feb 3, 2016, at 1:29 PM, Boris Steipe <boris.steipe at utoronto.ca> wrote:

> It seems your autocorrect is playing tricks on you but if I understand you correctly you have a two digit year and want to convert that to a four digit year? That's not uniquely possible of course; by convention, as the documentation to strptime() says:
> 
>  On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 – that
>  is the behaviour specified by the 2004 and 2008 POSIX standards...
> 
> If this is correct for you, you need to convert the string to a time object and the time object back to string. The format specifier %Y prints four-digit years:
> 
> 
> d <- "7/27/59"
> strptime(d, format="%m/%d/%y")  # "2059-07-27 EDT"
> x <- strptime(d, format="%m/%d/%y")
> 
> strftime(x, format="%m/%d/%Y")  # "07/27/1977"
> 
> 
> 
> B.
> 
> 
> 
> 
> 
> 
> 
> On Feb 3, 2016, at 11:03 AM, carol white via R-help <r-help at r-project.org> wrote:
> 
>> Hi,might be trivial but how to determine the year of a date which is in the %m/%d/%y format and those whose year is century should be modified to ISO so that all date will have with year in ISO?
>> Regards,
>> Carol
>> 
>>     [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.


  
	[[alternative HTML version deleted]]



More information about the R-help mailing list