Dr Eberhard W Lisse
2022-Jul-13 16:14 UTC
[R] How to parse a really silly date with lubridate
Hi, while all of the below work in a character vector, none works in the tibble. The following DDATA %>% add_column(as.tibble(lubridate::dmy_hm(DDATA$Date)), .before = "Period") %>% rename(NewDate=value) %>% select(Date,NewDate) %>% filter(between(as.Date(NewDate),as.Date('2022-07-09'), as.Date('2022-07-10'))) does work # A tibble: 3 ? 2 Date NewDate <chr> <dttm> 1 9. Jul 2022 at 11:39 2022-07-09 11:39:00 2 10. Jul 2022 at 01:58 2022-07-10 01:58:00 3 10. Jul 2022 at 11:26 2022-07-10 11:26:00 but I wonder if that can not be done more elegantly, ie by direct replacements in the column. greetings, el On 2022-07-13 16:48 , Rui Barradas wrote: [...] > d <- c('9. Jul 2022 at 11:39', '10. Jul 2022 at 01:58') > lubridate::dmy_hm(d) > #> [1] "2022-07-09 11:39:00 UTC" "2022-07-10 01:58:00 UTC" [...] On 2022-07-13 16:03 , Ben Tupper wrote: [...] > s = c("9. Jul 2022 at 11:39", "10. Jul 2022 at 01:58") > as.POSIXct(s, format = "%d. %b %Y at %H:%M") > as.POSIXct(s, format = "%d. %b %Y at %H:%M", tz = "UTC") [...] On 2022-07-13 15:52 , Ivan Krylov wrote: [...] > Sys.setlocale('LC_TIME', 'C') > strptime( > c('9. Jul 2022 at 11:39', '10. Jul 2022 at 01:58'), > '%d. %b %Y at %H:%M' > ) [...] > ?s 14:40 de 13/07/2022, Dr Eberhard Lisse escreveu: >> >> Hi, >> >> I have data file which generated by an otherwise very nice (diabetes >> log) app, but exports dates really silly. >> >> After reading the enclosed mwe.csv into R like so >> >> MWE <- read_delim('mwe.csv', delim = ';') %>% >> select(Date) %>% >> print() >> >> >> this comes out as: >> >> # A tibble: 2 ? 1 >> Date >> <chr> >> 1 9. Jul 2022 at 11:39 >> 2 10. Jul 2022 at 01:58 >> >> >> No matter what I try I am not able to parse this inside R to get at >> proper dates (I have loaded tidyverse and lubridate). >> >> I can easily do somethig >> >> csvq -d ';' -t '%e. %b %Y at %H:%i' \ >> 'SELECT Date as oridate, >> DATETIME_FORMAT(Date, "%Y-%m-%d %H:%m") AS date >> FROM mwe' >> >> +-----------------------+------------------+ >> | oridate | date | >> +-----------------------+------------------+ >> | 9. Jul 2022 at 11:39 | 2022-07-09 11:07 | >> | 10. Jul 2022 at 01:58 | 2022-07-10 01:07 | >> +-----------------------+------------------+ >> >> and hence could easily do something like >> >> csvq -d ';' -t '%e. %b %Y at %H:%i' \ >> 'ALTER mwe >> SET Date = DATETIME_FORMAT(Date, "%Y-%m-%d %H:%m")' >> >> but would rather like to be able to do it inside R and would therefor >> appreciate any advice in this regard. >> >> >> greetings, el >> >
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 ?s 17:14 de 13/07/2022, Dr Eberhard W Lisse escreveu:> > Hi, > > while all of the below work in a character vector, none works in > the tibble. > > The following > > ????DDATA %>% > ??????? add_column(as.tibble(lubridate::dmy_hm(DDATA$Date)), > ??????????? .before = "Period") %>% > ??????? rename(NewDate=value) %>% > ??????? select(Date,NewDate) %>% > ??????? filter(between(as.Date(NewDate),as.Date('2022-07-09'), > ??????????? as.Date('2022-07-10'))) > > does work > > ????# A tibble: 3 ? 2 > ????? Date????????????????? NewDate > ????? <chr>???????????????? <dttm> > ????1 9. Jul 2022 at 11:39? 2022-07-09 11:39:00 > ????2 10. Jul 2022 at 01:58 2022-07-10 01:58:00 > ????3 10. Jul 2022 at 11:26 2022-07-10 11:26:00 > > but I wonder if that can not be done more elegantly, ie by direct > replacements in the column. > > greetings, el > > On 2022-07-13 16:48 , Rui Barradas wrote: > [...] > > d <- c('9. Jul 2022 at 11:39', '10. Jul 2022 at 01:58') > > lubridate::dmy_hm(d) > > #> [1] "2022-07-09 11:39:00 UTC" "2022-07-10 01:58:00 UTC" > [...] > > On 2022-07-13 16:03 , Ben Tupper wrote: > [...] > > s = c("9. Jul 2022 at 11:39", "10. Jul 2022 at 01:58") > > as.POSIXct(s, format = "%d. %b %Y at %H:%M") > > as.POSIXct(s, format = "%d. %b %Y at %H:%M", tz = "UTC") > [...] > > On 2022-07-13 15:52 , Ivan Krylov wrote: > [...] > > Sys.setlocale('LC_TIME', 'C') > > strptime( > >?? c('9. Jul 2022 at 11:39', '10. Jul 2022 at 01:58'), > >?? '%d. %b %Y at %H:%M' > > ) > [...] > > > > ?s 14:40 de 13/07/2022, Dr Eberhard Lisse escreveu: > >> > >> Hi, > >> > >> I have data file which generated by an otherwise very nice (diabetes > >> log) app, but exports dates really silly. > >> > >> After reading the enclosed mwe.csv into R like so > >> > >>?????? MWE <- read_delim('mwe.csv', delim = ';') %>% > >>????????? select(Date) %>% > >>????????? print() > >> > >> > >> this comes out as: > >> > >>?????? # A tibble: 2 ? 1 > >>????? Date > >>????? <chr> > >>?????? 1 9. Jul 2022 at 11:39 > >>?????? 2 10. Jul 2022 at 01:58 > >> > >> > >> No matter what I try I am not able to parse this inside R to get at > >> proper dates (I have loaded tidyverse and lubridate). > >> > >> I can easily do somethig > >> > >>?????? csvq? -d ';' -t '%e. %b %Y at %H:%i' \ > >>????????? 'SELECT Date as oridate, > >>????????????? DATETIME_FORMAT(Date, "%Y-%m-%d %H:%m") AS date > >>????????? FROM mwe' > >> > >>?????? +-----------------------+------------------+ > >>?????? |??????? oridate??????? |?????? date?????? | > >>?????? +-----------------------+------------------+ > >>?????? | 9. Jul 2022 at 11:39? | 2022-07-09 11:07 | > >>?????? | 10. Jul 2022 at 01:58 | 2022-07-10 01:07 | > >>?????? +-----------------------+------------------+ > >> > >> and hence could easily do something like > >> > >>?????? csvq? -d ';' -t '%e. %b %Y at %H:%i' \ > >>??????? 'ALTER mwe > >>??????? SET Date = DATETIME_FORMAT(Date, "%Y-%m-%d %H:%m")' > >> > >> but would rather like to be able to do it inside R and would therefor > >> appreciate any advice in this regard. > >> > >> > >> greetings, el > >> > > > > ______________________________________________ > 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.