[R] date

Enrico Schumann e@ @end|ng |rom enr|co@chum@nn@net
Thu Dec 19 11:49:02 CET 2019


Quoting Eric Berger <ericjberger using gmail.com>:

> Martin  writes: "there's really no reason for going beyond base R"
>
> I disagree. Lubridate is a fantastic package. I use it all the time. It
> makes working with dates really easy, as evidenced by John Kane's
> suggestion. I strongly recommend learning to work with it.
>
> The bottom line: as is often the case, there are many different ways to
> accomplish a task in R.

I apologise beforehand if this sparks an unnecessary discussion ;-)

But the important point is:
If you know the structure of the data you want to
parse, then it is best to tell R (or any other language)
this structure explicitly.


> On Thu, Dec 19, 2019 at 10:31 AM Martin Maechler <maechler using stat.math.ethz.ch>
> wrote:
>
>> >>>>> John Kane
>> >>>>>     on Tue, 17 Dec 2019 20:28:17 -0500 writes:
>>
>>     > library(lubridate)
>>     > gs$dat1  <-  mdy(gs$date)
>>
>> there's really no reason for going beyond base R.
>>
>> Using the proper format as per Patrick and Peter's advice
>> (below) is perfectly clear and actually
>> more robust (for the next data set etc)
>> than going via "good guessing" in extra packages.
>>
>>     > On Tue, 17 Dec 2019 at 18:38, peter dalgaard <pdalgd using gmail.com>
>> wrote:
>>     >>
>>     >> ...and switch the order, and use %y for 2-digit years.
>>     >>
>>     >> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <
>> malone using malonequantitative.com> wrote:
>>     >> >
>>     >> > Try putting / instead of - in your format, to match the data.
>>     >> >
>>     >> > On Tue, Dec 17, 2019 at 5:52 PM Val <valkremk using gmail.com> wrote:
>>     >> >>
>>     >> >> Hi All,
>>     >> >>
>>     >> >> I wanted to to convert character date  mm/dd/yy  to YYYY-mm-dd
>>     >> >> The sample data and my attempt is shown below
>>     >> >>
>>     >> >> gs <-read.table(text="ID date
>>     >> >> A1   09/27/03
>>     >> >> A2   05/27/16
>>     >> >> A3   01/25/13
>>     >> >> A4   09/27/19",header=TRUE,stringsAsFactors=F)
>>     >> >>
>>     >> >> Desired output
>>     >> >>  ID     date      d1
>>     >> >> A1 09/27/03 2003-09-27
>>     >> >> A2 05/27/16 2016-05-27
>>     >> >> A3 01/25/13 2012-04-25
>>     >> >> A4 09/27/19 2019-09-27
>>     >> >>
>>     >> >> I used this
>>     >> >> gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d")
>>     >> >>
>>     >> >> but I got NA's.
>>     >> >>
>>     >> >> How do I get my desired result?
>>     >> >> Thank you.
>>     >> >>
>>     >>
>>     >> --
>>     >> Peter Dalgaard, Professor,
>>     >> Center for Statistics, Copenhagen Business School
>>     >> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>>     >> Phone: (+45)38153501
>>     >> Office: A 4.23
>>     >> Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com
>>     >>
>>
>>     > --
>>     > John Kane
>>     > Kingston ON Canada



-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net



More information about the R-help mailing list