how to change format of dates in a tibble

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

how to change format of dates in a tibble

Christopher W. Ryan
I'm not understanding how the tidyverse handles date formats.

output of sessionInfo() at the end of my message.

dateRanges <- structure(list(apptType = structure(1:2, .Label = c("initial
visit",
"start of treatment visit"), class = "factor"), minMadeRequestDates =
structure(c(18124,
18115), class = "Date"), maxMadeRequestDates = structure(c(18187,
18199), class = "Date"), minApptDate = structure(c(18129, 18129
), class = "Date"), maxApptDate = structure(c(18199, 18214), class =
"Date")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -2L))
str(dateRanges)

## produces desired result
format(dateRanges, format = "%d %b %Y")

## does not produce desired result
library(dplyr)
format(dateRanges, format = "%d %b %Y")

## rather cumbersome, and also does not produce the desired output
mutate(dateRanges, minMRD = as.Date(minMadeRequestDates, format = "%d %b
%Y"))

How does one change the format of a date variable inside a tibble when
dplyr is loaded?

Thanks

Chris Ryan

======== session info ===========
> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United
States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] dplyr_0.8.3

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2       fansi_0.4.0      zeallot_0.1.0    utf8_1.1.4
crayon_1.3.4     assertthat_0.2.1 R6_2.4.0
 [8] backports_1.1.5  magrittr_1.5     pillar_1.4.2     rlang_0.4.0
 cli_1.1.0        vctrs_0.2.0      glue_1.3.1
[15] purrr_0.3.3      compiler_3.6.1   pkgconfig_2.0.3  tidyselect_0.2.5
tibble_2.1.3
====================================

        [[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: how to change format of dates in a tibble

Rui Barradas
Hello,

You are passing a tbl_df to functions that expect a vector.
Here is a first, simpler example.


library(tidyverse)

df1 <- tibble(Date = Sys.Date() + 0:9)

## both produce expected result
format(df1$Date, format = "%d %b %Y")

df1 %>%
   mutate(d = format(Date, format = "%d %b %Y"))


Now your with data.

## does NOT produces desired result, dateRanges is an
## object of classes 'tbl_df', 'tbl' and 'data.frame'
format(dateRanges, format = "%d %b %Y")


## produces desired result
dateRanges %>%
   mutate(minMRD = format(minMadeRequestDates, format = "%d %b %Y")) %>%
   select(minMRD)

## rather cumbersome, and also produces the desired output
mutate(dateRanges, minMRD = format(minMadeRequestDates, format = "%d %b
%Y")) %>%
   select(minMRD)



Hope this helps,

Rui Barradas


Às 16:45 de 18/12/19, Christopher W Ryan escreveu:

> I'm not understanding how the tidyverse handles date formats.
>
> output of sessionInfo() at the end of my message.
>
> dateRanges <- structure(list(apptType = structure(1:2, .Label = c("initial
> visit",
> "start of treatment visit"), class = "factor"), minMadeRequestDates =
> structure(c(18124,
> 18115), class = "Date"), maxMadeRequestDates = structure(c(18187,
> 18199), class = "Date"), minApptDate = structure(c(18129, 18129
> ), class = "Date"), maxApptDate = structure(c(18199, 18214), class =
> "Date")), class = c("tbl_df",
> "tbl", "data.frame"), row.names = c(NA, -2L))
> str(dateRanges)
>
> ## produces desired result
> format(dateRanges, format = "%d %b %Y")
>
> ## does not produce desired result
> library(dplyr)
> format(dateRanges, format = "%d %b %Y")
>
> ## rather cumbersome, and also does not produce the desired output
> mutate(dateRanges, minMRD = as.Date(minMadeRequestDates, format = "%d %b
> %Y"))
>
> How does one change the format of a date variable inside a tibble when
> dplyr is loaded?
>
> Thanks
>
> Chris Ryan
>
> ======== session info ===========
>> sessionInfo()
> R version 3.6.1 (2019-07-05)
> Platform: i386-w64-mingw32/i386 (32-bit)
> Running under: Windows 7 (build 7601) Service Pack 1
>
> Matrix products: default
>
> locale:
> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
> States.1252    LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C                           LC_TIME=English_United
> States.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] dplyr_0.8.3
>
> loaded via a namespace (and not attached):
>   [1] Rcpp_1.0.2       fansi_0.4.0      zeallot_0.1.0    utf8_1.1.4
> crayon_1.3.4     assertthat_0.2.1 R6_2.4.0
>   [8] backports_1.1.5  magrittr_1.5     pillar_1.4.2     rlang_0.4.0
>   cli_1.1.0        vctrs_0.2.0      glue_1.3.1
> [15] purrr_0.3.3      compiler_3.6.1   pkgconfig_2.0.3  tidyselect_0.2.5
> tibble_2.1.3
> ====================================
>
> [[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.
>

______________________________________________
[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: how to change format of dates in a tibble

Duncan Murdoch-2
In reply to this post by Christopher W. Ryan
On 18/12/2019 11:45 a.m., Christopher W Ryan wrote:

> I'm not understanding how the tidyverse handles date formats.
>
> output of sessionInfo() at the end of my message.
>
> dateRanges <- structure(list(apptType = structure(1:2, .Label = c("initial
> visit",
> "start of treatment visit"), class = "factor"), minMadeRequestDates =
> structure(c(18124,
> 18115), class = "Date"), maxMadeRequestDates = structure(c(18187,
> 18199), class = "Date"), minApptDate = structure(c(18129, 18129
> ), class = "Date"), maxApptDate = structure(c(18199, 18214), class =
> "Date")), class = c("tbl_df",
> "tbl", "data.frame"), row.names = c(NA, -2L))
> str(dateRanges)
>
> ## produces desired result
> format(dateRanges, format = "%d %b %Y")
>
> ## does not produce desired result
> library(dplyr)
> format(dateRanges, format = "%d %b %Y")
>
> ## rather cumbersome, and also does not produce the desired output
> mutate(dateRanges, minMRD = as.Date(minMadeRequestDates, format = "%d %b
> %Y"))
>
> How does one change the format of a date variable inside a tibble when
> dplyr is loaded?
In the first example, the class of dateRanges is c("tbl_df", "tbl",
"data.frame").   Since you don't have any tbl methods loaded, you get
the format.data.frame method.

When you attach dplyr, it loads the tibble package, and it has a
"format.tbl" method that gives the output you don't like.

One simple way to work around this is to call the format.data.frame
method directly:

  format.data.frame(dateRanges, format = "%d %b %Y")

Similarly,

  format(as.data.frame(dateRanges), format = "%d %b %Y")

calls that method.

You can also convert the date columns to char using something like

   dateRanges %>% mutate_all(function(x) format(x, format = "%d %b %Y"))

and then auto-printing will show you the formatted output.  This is a
little risky, since it applies the formatting function to all columns;
in your example it works, but you might need to use mutate_if() instead:

   dateRanges %>% mutate_if(~ inherits(.x, "Date"), ~ format(.x, "%d %b
%Y"))

Duncan Murdoch

>
> Thanks
>
> Chris Ryan
>
> ======== session info ===========
>> sessionInfo()
> R version 3.6.1 (2019-07-05)
> Platform: i386-w64-mingw32/i386 (32-bit)
> Running under: Windows 7 (build 7601) Service Pack 1
>
> Matrix products: default
>
> locale:
> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
> States.1252    LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C                           LC_TIME=English_United
> States.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] dplyr_0.8.3
>
> loaded via a namespace (and not attached):
>   [1] Rcpp_1.0.2       fansi_0.4.0      zeallot_0.1.0    utf8_1.1.4
> crayon_1.3.4     assertthat_0.2.1 R6_2.4.0
>   [8] backports_1.1.5  magrittr_1.5     pillar_1.4.2     rlang_0.4.0
>   cli_1.1.0        vctrs_0.2.0      glue_1.3.1
> [15] purrr_0.3.3      compiler_3.6.1   pkgconfig_2.0.3  tidyselect_0.2.5
> tibble_2.1.3
> ====================================
>
> [[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.
>

______________________________________________
[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.