date

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

date

Val-17
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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date

Patrick (Malone Quantitative)
Try putting / instead of - in your format, to match the data.

On Tue, Dec 17, 2019 at 5:52 PM Val <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date

Peter Dalgaard-2
...and switch the order, and use %y for 2-digit years.

> On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <[hidden email]> wrote:
>
> Try putting / instead of - in your format, to match the data.
>
> On Tue, Dec 17, 2019 at 5:52 PM Val <[hidden email]> 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.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date

John Kane-3
library(lubridate)
gs$dat1  <-  mdy(gs$date)

On Tue, 17 Dec 2019 at 18:38, peter dalgaard <[hidden email]> wrote:

>
> ...and switch the order, and use %y for 2-digit years.
>
> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <[hidden email]> wrote:
> >
> > Try putting / instead of - in your format, to match the data.
> >
> > On Tue, Dec 17, 2019 at 5:52 PM Val <[hidden email]> 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.
> >>
> >> ______________________________________________
> >> [hidden email] 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.
> >
> > ______________________________________________
> > [hidden email] 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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: [hidden email]  Priv: [hidden email]
>
> ______________________________________________
> [hidden email] 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.



--
John Kane
Kingston ON Canada

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date

Martin Maechler
>>>>> 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 <[hidden email]> wrote:
    >>
    >> ...and switch the order, and use %y for 2-digit years.
    >>
    >> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <[hidden email]> wrote:
    >> >
    >> > Try putting / instead of - in your format, to match the data.
    >> >
    >> > On Tue, Dec 17, 2019 at 5:52 PM Val <[hidden email]> 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.
    >> >>
    >> >> ______________________________________________
    >> >> [hidden email] 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.
    >> >
    >> > ______________________________________________
    >> > [hidden email] 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.
    >>
    >> --
    >> Peter Dalgaard, Professor,
    >> Center for Statistics, Copenhagen Business School
    >> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
    >> Phone: (+45)38153501
    >> Office: A 4.23
    >> Email: [hidden email]  Priv: [hidden email]
    >>
    >> ______________________________________________
    >> [hidden email] 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.



    > --
    > John Kane
    > Kingston ON Canada

    > ______________________________________________
    > [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date

Eric Berger
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.


On Thu, Dec 19, 2019 at 10:31 AM Martin Maechler <[hidden email]>
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 <[hidden email]>
> wrote:
>     >>
>     >> ...and switch the order, and use %y for 2-digit years.
>     >>
>     >> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <
> [hidden email]> wrote:
>     >> >
>     >> > Try putting / instead of - in your format, to match the data.
>     >> >
>     >> > On Tue, Dec 17, 2019 at 5:52 PM Val <[hidden email]> 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.
>     >> >>
>     >> >> ______________________________________________
>     >> >> [hidden email] 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.
>     >> >
>     >> > ______________________________________________
>     >> > [hidden email] 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.
>     >>
>     >> --
>     >> Peter Dalgaard, Professor,
>     >> Center for Statistics, Copenhagen Business School
>     >> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>     >> Phone: (+45)38153501
>     >> Office: A 4.23
>     >> Email: [hidden email]  Priv: [hidden email]
>     >>
>     >> ______________________________________________
>     >> [hidden email] 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.
>
>
>
>     > --
>     > John Kane
>     > Kingston ON Canada
>
>     > ______________________________________________
>     > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date

Enrico Schumann-2

Quoting Eric Berger <[hidden email]>:

> 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 <[hidden email]>
> 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 <[hidden email]>
>> wrote:
>>     >>
>>     >> ...and switch the order, and use %y for 2-digit years.
>>     >>
>>     >> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <
>> [hidden email]> wrote:
>>     >> >
>>     >> > Try putting / instead of - in your format, to match the data.
>>     >> >
>>     >> > On Tue, Dec 17, 2019 at 5:52 PM Val <[hidden email]> 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: [hidden email]  Priv: [hidden email]
>>     >>
>>
>>     > --
>>     > John Kane
>>     > Kingston ON Canada



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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date

Bert Gunter-2
"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. "

Fortune nomination!

-- Bert






Thu, Dec 19, 2019, 2:49 AM Enrico Schumann <[hidden email]> wrote:

>
> Quoting Eric Berger <[hidden email]>:
>
> > 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 <
> [hidden email]>
> > 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 <[hidden email]>
> >> wrote:
> >>     >>
> >>     >> ...and switch the order, and use %y for 2-digit years.
> >>     >>
> >>     >> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <
> >> [hidden email]> wrote:
> >>     >> >
> >>     >> > Try putting / instead of - in your format, to match the data.
> >>     >> >
> >>     >> > On Tue, Dec 17, 2019 at 5:52 PM Val <[hidden email]>
> 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: [hidden email]  Priv: [hidden email]
> >>     >>
> >>
> >>     > --
> >>     > John Kane
> >>     > Kingston ON Canada
>
>
>
> --
> Enrico Schumann
> Lucerne, Switzerland
> http://enricoschumann.net
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date

Eric Berger
[ ... taking the bait regarding the "unnecessary discussion" ... ]

The "Fortune nomination" that Bert sent includes the phrase

"...then it is best to tell R ..."

What metric is being used to do the ranking to get the "best"? If the
metric is related to "providing the most unambiguous information to R"
then I agree that providing the structure explicitly is best.
However, often what is "best" is to minimize programmer time. With
lubridate, I know that providing the clue 'ymd' is enough to have it
perform the date conversion correctly. That is minimal effort on my part,
which gives it a top ranking from my point of view.

Also, to broaden this "unnecessary discussion" I would argue that the
lubridate package may even be more "in the spirit of R" than what is being
proposed with the explicit structural information. Clearly R is far from
being a strongly typed language. If you really want to provide explicit
structural information maybe you would be better off with a language such
as C++. :-)





On Thu, Dec 19, 2019 at 2:30 PM Bert Gunter <[hidden email]> wrote:

>
>
> "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. "
>
> Fortune nomination!
>
> -- Bert
>
>
>
>
>
>
> Thu, Dec 19, 2019, 2:49 AM Enrico Schumann <[hidden email]> wrote:
>
>>
>> Quoting Eric Berger <[hidden email]>:
>>
>> > 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 <
>> [hidden email]>
>> > 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 <[hidden email]>
>> >> wrote:
>> >>     >>
>> >>     >> ...and switch the order, and use %y for 2-digit years.
>> >>     >>
>> >>     >> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <
>> >> [hidden email]> wrote:
>> >>     >> >
>> >>     >> > Try putting / instead of - in your format, to match the data.
>> >>     >> >
>> >>     >> > On Tue, Dec 17, 2019 at 5:52 PM Val <[hidden email]>
>> 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: [hidden email]  Priv: [hidden email]
>> >>     >>
>> >>
>> >>     > --
>> >>     > John Kane
>> >>     > Kingston ON Canada
>>
>>
>>
>> --
>> Enrico Schumann
>> Lucerne, Switzerland
>> http://enricoschumann.net
>>
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date

Jeff Newmiller
I agree that having convenience functions can be in the spirit of R, but I find that lubridate puts the cart before the horse so I avoid it. Specifically, the conceptual sequence

- convert character to timestamp in GMT
- "fix" erroneous timestamps to correct time zone

more inefficient and error-prone than

- convert character to timestamp specifying correct timezone

or

- specify default timezone
- convert character to timestamp using default timezone

but apparently others disagree so we have a whole sub-culture working with GMT by default.

On December 19, 2019 4:45:41 AM PST, Eric Berger <[hidden email]> wrote:

>[ ... taking the bait regarding the "unnecessary discussion" ... ]
>
>The "Fortune nomination" that Bert sent includes the phrase
>
>"...then it is best to tell R ..."
>
>What metric is being used to do the ranking to get the "best"? If the
>metric is related to "providing the most unambiguous information to R"
>then I agree that providing the structure explicitly is best.
>However, often what is "best" is to minimize programmer time. With
>lubridate, I know that providing the clue 'ymd' is enough to have it
>perform the date conversion correctly. That is minimal effort on my
>part,
>which gives it a top ranking from my point of view.
>
>Also, to broaden this "unnecessary discussion" I would argue that the
>lubridate package may even be more "in the spirit of R" than what is
>being
>proposed with the explicit structural information. Clearly R is far
>from
>being a strongly typed language. If you really want to provide explicit
>structural information maybe you would be better off with a language
>such
>as C++. :-)
>
>
>
>
>
>On Thu, Dec 19, 2019 at 2:30 PM Bert Gunter <[hidden email]>
>wrote:
>
>>
>>
>> "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. "
>>
>> Fortune nomination!
>>
>> -- Bert
>>
>>
>>
>>
>>
>>
>> Thu, Dec 19, 2019, 2:49 AM Enrico Schumann <[hidden email]>
>wrote:
>>
>>>
>>> Quoting Eric Berger <[hidden email]>:
>>>
>>> > 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 <
>>> [hidden email]>
>>> > 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
><[hidden email]>
>>> >> wrote:
>>> >>     >>
>>> >>     >> ...and switch the order, and use %y for 2-digit years.
>>> >>     >>
>>> >>     >> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative)
><
>>> >> [hidden email]> wrote:
>>> >>     >> >
>>> >>     >> > Try putting / instead of - in your format, to match the
>data.
>>> >>     >> >
>>> >>     >> > On Tue, Dec 17, 2019 at 5:52 PM Val <[hidden email]>
>>> 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: [hidden email]  Priv: [hidden email]
>>> >>     >>
>>> >>
>>> >>     > --
>>> >>     > John Kane
>>> >>     > Kingston ON Canada
>>>
>>>
>>>
>>> --
>>> Enrico Schumann
>>> Lucerne, Switzerland
>>> http://enricoschumann.net
>>>
>>> ______________________________________________
>>> [hidden email] 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]]
>
>______________________________________________
>[hidden email] 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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date

dkStevens
In reply to this post by Val-17
Val

Another all-base R solution:

as.Date(strptime(gs$date,format="%m/%d/%y"))

or if you want to add a time field later

as.POSIXct(strptime(gs$date,format="%m/%d/%y")))

since strptime produces a list version of the date: class is POSIXt and the subclass is POSIXlt, that can be convenient for extracting elements like month, day of the year, minute, etc. The actual date/time information is returned as the number of seconds since 1970-01-01 00:00:00 UTC but the representation is as in your gs.d1 below.

It can be a bit tricky when daylight savings time comes into play but that's for another post.

David


On 12/17/2019 3:51 PM, Val 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.

______________________________________________
[hidden email]<mailto:[hidden email]> 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.


--
David K Stevens, PhD, PE
Environmental Engineering Division
Civil and Environmental Engineering
Utah State University
8200 Old Main Hill
Logan, UT 83200-8200
(435) 797-3229
[hidden email]<mailto:[hidden email]>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: date

Rolf Turner
In reply to this post by Bert Gunter-2
On 20/12/19 1:30 am, Bert Gunter wrote:
> "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. "
>
> Fortune nomination!

Second the nomination!

cheers,

Rolf Turner

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.