[R] how to change format of dates in a tibble
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Dec 18 20:32:52 CET 2019
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]]
>
> ______________________________________________
> 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