Class from character to dat

hello, i have an issue with date as character, can not change class to date. Below i submite reprex. Thanks

# practice session module 6 -----------------------------------------------

# H7N9 data set
# date: 20241121
# fabio [email protected]


# load packages -----------------------------------------------------------

install.packages("pacman")
#> 
#> The downloaded binary packages are in
#>  /var/folders/vr/88w7r8d50rb7xbx_x8rx55640000gn/T//RtmpWJtijL/downloaded_packages
pacman::p_load(rio,
               here,
               janitor,
               reprex,
               datapasta,
               epitrix,
               flextable,
               gtsummary,
               scales,
               viridis,
               skimr,
               lubridate,
               tidyverse
)

# import data set ---------------------------------------------------------

inf_raw <- data.frame(
  stringsAsFactors = FALSE,
  date_of_symptoms = c("2/19/2013","2/27/2013",
                       "3/9/2013","3/19/2013","3/19/2013","3/21/2013","3/20/2013",
                       "3/7/2013","3/25/2013","3/28/2013")
)

# clean data --------------------------------------------------------------

infH7N9 <- inf_raw %>% 
  clean_names() %>%  # clean names automatically
  filter(province != "Anhui") %>% # remove Anhui from province
  mutate(day_of_symptoms = as.Date(date_of_symptoms))
#> Error in `filter()`:
#> β„Ή In argument: `province != "Anhui"`.
#> Caused by error:
#> ! object 'province' not found
#> Backtrace:
#>      β–†
#>   1. β”œβ”€... %>% mutate(day_of_symptoms = as.Date(date_of_symptoms))
#>   2. β”œβ”€dplyr::mutate(., day_of_symptoms = as.Date(date_of_symptoms))
#>   3. β”œβ”€dplyr::filter(., province != "Anhui")
#>   4. β”œβ”€dplyr:::filter.data.frame(., province != "Anhui")
#>   5. β”‚ └─dplyr:::filter_rows(.data, dots, by)
#>   6. β”‚   └─dplyr:::filter_eval(...)
#>   7. β”‚     β”œβ”€base::withCallingHandlers(...)
#>   8. β”‚     └─mask$eval_all_filter(dots, env_filter)
#>   9. β”‚       └─dplyr (local) eval()
#>  10. └─base::.handleSimpleError(...)
#>  11.   └─dplyr (local) h(simpleError(msg, call))
#>  12.     └─rlang::abort(message, class = error_class, parent = parent, call = error_call)

#minimal data set  

# inf_raw %>% 
#   head(10) %>% 
#   select(date_of_symptoms) %>% 
#   dpasta()
# 
# data.frame(
#   stringsAsFactors = FALSE,
#   date_of_symptoms = c("2/19/2013","2/27/2013",
#                        "3/9/2013","3/19/2013","3/19/2013","3/21/2013","3/20/2013",
#                        "3/7/2013","3/25/2013","3/28/2013")
# )

Created on 2024-11-21 with reprex v2.0.2

Session info


Hello,

There are two problems going on here, the first is that your reprex includes reference to province even though it’s not a variable in your raw data, inf_raw. Second, when you call as.Date you need to provide the proper format of the date in question. This should fix it:

# loading packages
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# loading data
inf_raw <- data.frame(
    stringsAsFactors = FALSE,
    date_of_symptoms = c(
        "2/19/2013",
        "2/27/2013",
        "3/9/2013",
        "3/19/2013",
        "3/19/2013",
        "3/21/2013",
        "3/20/2013",
        "3/7/2013",
        "3/25/2013",
        "3/28/2013"
    )
)

# formatting the date
infH7N9 <- inf_raw |>
    mutate(day_of_symptoms = as.Date(date_of_symptoms, "%m/%d/%Y"))

Created on 2024-11-22 with reprex v2.1.1

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.1 (2024-06-14)
#>  os       macOS 15.1
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Toronto
#>  date     2024-11-22
#>  pandoc   3.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/x86_64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.3   2024-06-21 [1] RSPM (R 4.4.0)
#>  digest        0.6.37  2024-08-19 [1] RSPM (R 4.4.0)
#>  dplyr       * 1.1.4   2023-11-17 [1] RSPM (R 4.4.0)
#>  evaluate      1.0.1   2024-10-10 [1] RSPM (R 4.4.0)
#>  fansi         1.0.6   2023-12-08 [1] RSPM (R 4.4.0)
#>  fastmap       1.2.0   2024-05-15 [1] RSPM (R 4.4.0)
#>  fs            1.6.5   2024-10-30 [1] RSPM (R 4.4.1)
#>  generics      0.1.3   2022-07-05 [1] RSPM (R 4.4.0)
#>  glue          1.8.0   2024-09-30 [1] RSPM (R 4.4.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] RSPM (R 4.4.0)
#>  knitr         1.48    2024-07-07 [1] RSPM (R 4.4.0)
#>  lifecycle     1.0.4   2023-11-07 [1] RSPM (R 4.4.0)
#>  magrittr      2.0.3   2022-03-30 [1] RSPM (R 4.4.0)
#>  pillar        1.9.0   2023-03-22 [1] RSPM (R 4.4.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] RSPM (R 4.4.0)
#>  R6            2.5.1   2021-08-19 [1] RSPM (R 4.4.0)
#>  reprex        2.1.1   2024-07-06 [1] RSPM (R 4.4.0)
#>  rlang         1.1.4   2024-06-04 [1] RSPM (R 4.4.0)
#>  rmarkdown     2.29    2024-11-04 [1] RSPM (R 4.4.1)
#>  rstudioapi    0.17.1  2024-10-22 [1] RSPM (R 4.4.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] RSPM (R 4.4.0)
#>  tibble        3.2.1   2023-03-20 [1] RSPM (R 4.4.0)
#>  tidyselect    1.2.1   2024-03-11 [1] RSPM (R 4.4.0)
#>  utf8          1.2.4   2023-10-22 [1] RSPM (R 4.4.0)
#>  vctrs         0.6.5   2023-12-01 [1] RSPM (R 4.4.0)
#>  withr         3.0.2   2024-10-28 [1] RSPM (R 4.4.0)
#>  xfun          0.49    2024-10-31 [1] RSPM (R 4.4.0)
#>  yaml          2.3.10  2024-07-26 [1] RSPM (R 4.4.0)
#> 
#>  [1] /Users/timothychisamore/Library/R/x86_64/4.4/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

All the best,

Tim

Thanks, problem solved

1 Like