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-boun...@r-project.org> On Behalf Of Dr Eberhard W Lisse
Sent: Wednesday, July 13, 2022 5:49 PM
To: r-help@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@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@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.

Reply via email to