[R] How to parse a really silly date with lubridate
@vi@e@gross m@iii@g oii gm@ii@com
@vi@e@gross m@iii@g oii gm@ii@com
Thu Jul 14 21:27:34 CEST 2022
Let me retract and modify my earlier comment based on getting more complete code.
This line of code remains silly if used stand-alone,
mutate(Date = lubridate::dmy_hm(Date))
But it was used in a pipeline as in
Read_delim(...) %>%
mutate(Date = lubridate::dmy_hm(Date)) %>%
...
As such, it makes perfect sense ONCE GIVEN A CONTEXT.
The contents of a file are read into a data.frame which is the implicit first argument to mutate. There is this a second argument that does an appropriate conversion to the column named Date within the data.frame.
Date = lubridate::dmy_hm(Date)
So it is n appropriate use and highlights one reason some do not like pipelines. When using one, you need to KNOW it so you are aware any arguments are after a first argument not shown.
-----Original Message-----
From: avi.e.gross using gmail.com <avi.e.gross using gmail.com>
Sent: Thursday, July 14, 2022 12:43 PM
To: 'r-help using r-project.org' <r-help using r-project.org>
Subject: RE: [R] How to parse a really silly date with lubridate
To be clear, I take no credit for the rather extraordinary function cll shown below:
mutate(Date = lubridate::dmy_hm(Date))
I would pretty much never have constructed such an interesting and rather unnecessary line of code.
ALL the work is done within the parentheses:
Date = lubridate::dmy_hm(Date))
The above creates a tibble and assigns it to the name of Date. but first it takes a vector called Date containing text and uses a function to parse it and return a form more suitable to use as a date.
So what does mutate() do when it is called with a tibble and asked to do nothing? I mean it sees something more like this:
Mutate(.data=Date)
What is missing are the usual assignment statements to modify existing columns or make new ones. So it does nothing and exists without complaint.
My suggestion was more like the following which uses mutate. For illustration, I will not use the name "Date" repeatedly and make new columns and uses an original_date as the source but note I am not saying the lubridate used will work as I do not see how it handles the initial part of the string containing an index number.
old_tibble <- tibble(useless=original_date) new_tibble <- mutate(old_tibble, useful = lubridate::dmy_hm(Date))
The above can be more compact by making the tibble directly in the first argument, and it can be done using old or new pipelines.
The reason the suggested way worked is because it used the vectorized methods of base-R and I mentioned there was no reason you must use dplyr for this or many other things especially when it is simple.
Now if you wanted to make multiple new columns containing character or integer versions of the month and other parts of the date or even calculate what day of the week that full date was in that year, then mutate can be very useful as you can keep adding requests to make a new column using all old and new columns already specified.
Sometimes when code works, we don't look to see if it works inadvertently, LOL!
-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Dr Eberhard W Lisse
Sent: Wednesday, July 13, 2022 5:49 PM
To: r-help using r-project.org
Subject: Re: [R] How to parse a really silly date with lubridate
Bui,
thanks, this what Avi suggested in an email to me as well and works.
It's so easy if you know it :-)-O
el
On 2022-07-13 23:40 , Rui Barradas wrote:
> Hello,
>
> Are you looking for mutate? In the example below I haven't included
> the filter, since the tibble only has 2 rows. But the date column is
> coerced to an actual datetime class in place, without the need for NewDate.
>
> suppressPackageStartupMessages({
> library(tibble)
> library(dplyr)
> })
>
> DDATA <- tibble(Date = c('9. Jul 2022 at 11:39', '10. Jul 2022 at
> 01:58'))
>
> DDATA %>%
> mutate(Date = lubridate::dmy_hm(Date)) #> # A tibble: 2 × 1 #> Date
> #> <dttm> #> 1 2022-07-09 11:39:00 #> 2 2022-07-10 01:58:00
>
>
> Hope this helps,
>
> Rui Barradas
[...]
______________________________________________
R-help using 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