[R] determine the year of a date
Boris Steipe
boris.steipe at utoronto.ca
Wed Feb 3 21:55:26 CET 2016
Who said this was Excel? What did I miss?
B.
On Feb 3, 2016, at 3:52 PM, Ista Zahn <istazahn at gmail.com> wrote:
> On Wed, Feb 3, 2016 at 3:43 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
>> Not exactly.
>>
>> If you have been "Excel"ed and have both formats in one column then you
>> really have a data cleanup task to do.
>
> No need to reinvent the wheel. Just do
>
> library(lubridate)
> mdy(c("09/01/15", "09/01/2015"))
>
> Best,
> ista
> The best route to resolution is to
>> fix the source of the data (e.g. go back into Excel and reformat the column
>> with consistent formatting).
>>
>> If you need to do this on an ongoing basis and cannot fix the data source,
>> then you might need to choose the correct format on the fly... something
>> like:
>>
>> yYdates <- function( ds ) {
>> fmt <- ifelse( grepl( "^\\d{1,2}/\\d{1,2}/\\d{2}$", ds )
>> , "%m/%d/%y"
>> , "%m/%d/%Y"
>> )
>> as.Date( ds, format=fmt )
>> }
>>
>> yYdates( c( "02/18/2015", "03/21/16" ) )
>> [1] "2015-02-18" "2016-03-21"
>>
>> but this would have to be tailored to the formatting problems in your data
>> and is not really a general solution.
>>
>> On Wed, 3 Feb 2016, Boris Steipe wrote:
>>
>>> As the documentation says, that's exactly what the expansion will do by
>>> default.
>>>
>>> B.
>>>
>>>
>>> On Feb 3, 2016, at 2:31 PM, carol white <wht_crl at yahoo.com> wrote:
>>>
>>>> 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.
>>>>
>>>>
>>>>
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>> ---------------------------------------------------------------------------
>> Jeff Newmiller The ..... ..... Go Live...
>> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
>> Live: OO#.. Dead: OO#.. Playing
>> Research Engineer (Solar/Batteries O.O#. #.O#. with
>> /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
>>
>>
>> ______________________________________________
>> 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.
More information about the R-help
mailing list