Error in coercing numeric column

Epi R Training exercise - posting reprex

When cleaning the following example dataset I encounter the following error message when converting the age column from a character to numeric class and was hoping for assistance.

Error: Error in age_categories(., age, lower = 0, upper = 70, breakers = 10): ‘list’ object cannot be coerced to type ‘double’

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

pacman::p_load(rio,
               janitor,
               here,
               epikit,
               tidyverse)


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

demo_data <- data.frame(
            case_id = c(1L,2L,10L,12L,13L,14L,
                        17L,18L,19L,21L,24L,25L,26L,29L,30L,34L,35L,
                        36L,42L,43L,45L,56L,57L,58L,72L,73L,74L,75L,76L,
                        77L,78L,85L,97L),
   date_of_symptoms = c("2013-02-19","2013-02-27",
                        "2013-03-28","2013-03-27","2013-03-22","2013-03-31",
                        "2013-03-28","2013-03-29","2013-03-25","2013-03-29",
                        "2013-04-01","2013-04-01","2013-04-03","2013-04-01",
                        "2013-04-04","2013-03-31","2013-04-02","2013-04-04",
                        "2013-04-03","2013-04-03","2013-04-01","2013-04-05",
                        "2013-04-08","2013-04-04","2013-04-10",NA,NA,NA,NA,
                        "2013-03-17","2013-04-12","2013-04-09","2013-04-13"),
                age = c(87,27,48,52,67,4,74,66,
                        59,67,64,62,77,76,81,74,83,68,53,86,56,73,
                        54,78,47,69,NA,NA,55,2,89,80,75)
 ) 

flu <- demo_data %>%
  mutate(
    age = as.numeric(age)) %>%
      age_categories(age, lower = 0, upper = 70, breakers = 10)
#> Error in age_categories(., age, lower = 0, upper = 70, breakers = 10): 'list' object cannot be coerced to type 'double'

Created on 2024-03-04 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.2 (2023-10-31 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United Kingdom.utf8
#>  ctype    English_United Kingdom.utf8
#>  tz       Europe/London
#>  date     2024-03-04
#>  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  class         7.3-22  2023-05-03 [2] CRAN (R 4.3.2)
#>  classInt      0.4-10  2023-09-05 [1] CRAN (R 4.3.2)
#>  cli           3.6.2   2023-12-11 [1] CRAN (R 4.3.2)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.2)
#>  DBI           1.2.1   2024-01-12 [1] CRAN (R 4.3.2)
#>  digest        0.6.34  2024-01-11 [1] CRAN (R 4.3.2)
#>  dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.3.2)
#>  e1071         1.7-14  2023-12-06 [1] CRAN (R 4.3.2)
#>  epikit      * 0.1.6   2024-01-23 [1] CRAN (R 4.3.2)
#>  evaluate      0.23    2023-11-01 [1] CRAN (R 4.3.2)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.3.2)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.2)
#>  forcats     * 1.0.0   2023-01-29 [1] CRAN (R 4.3.2)
#>  fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.2)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.2)
#>  ggplot2     * 3.4.4   2023-10-12 [1] CRAN (R 4.3.2)
#>  glue          1.7.0   2024-01-09 [1] CRAN (R 4.3.2)
#>  gtable        0.3.4   2023-08-21 [1] CRAN (R 4.3.2)
#>  here        * 1.0.1   2020-12-13 [1] CRAN (R 4.3.2)
#>  hms           1.1.3   2023-03-21 [1] CRAN (R 4.3.2)
#>  htmltools     0.5.7   2023-11-03 [1] CRAN (R 4.3.2)
#>  janitor     * 2.2.0   2023-02-02 [1] CRAN (R 4.3.2)
#>  KernSmooth    2.23-22 2023-07-10 [2] CRAN (R 4.3.2)
#>  knitr         1.45    2023-10-30 [1] CRAN (R 4.3.2)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.3.2)
#>  lubridate   * 1.9.3   2023-09-27 [1] CRAN (R 4.3.2)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.2)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.2)
#>  pacman        0.5.1   2019-03-11 [1] CRAN (R 4.3.2)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.2)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.2)
#>  proxy         0.4-27  2022-06-09 [1] CRAN (R 4.3.2)
#>  purrr       * 1.0.2   2023-08-10 [1] CRAN (R 4.3.2)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.2)
#>  Rcpp          1.0.12  2024-01-09 [1] CRAN (R 4.3.2)
#>  readr       * 2.1.5   2024-01-10 [1] CRAN (R 4.3.2)
#>  reprex        2.1.0   2024-01-11 [1] CRAN (R 4.3.2)
#>  rio         * 1.0.1   2023-09-19 [1] CRAN (R 4.3.2)
#>  rlang         1.1.3   2024-01-10 [1] CRAN (R 4.3.2)
#>  rmarkdown     2.25    2023-09-18 [1] CRAN (R 4.3.2)
#>  rprojroot     2.0.4   2023-11-05 [1] CRAN (R 4.3.2)
#>  rstudioapi    0.15.0  2023-07-07 [1] CRAN (R 4.3.2)
#>  scales        1.3.0   2023-11-28 [1] CRAN (R 4.3.2)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.2)
#>  sf            1.0-15  2023-12-18 [1] CRAN (R 4.3.2)
#>  snakecase     0.11.1  2023-08-27 [1] CRAN (R 4.3.2)
#>  stringi       1.8.3   2023-12-11 [1] CRAN (R 4.3.2)
#>  stringr     * 1.5.1   2023-11-14 [1] CRAN (R 4.3.2)
#>  tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.3.2)
#>  tidyr       * 1.3.1   2024-01-24 [1] CRAN (R 4.3.2)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.3.2)
#>  tidyverse   * 2.0.0   2023-02-22 [1] CRAN (R 4.3.2)
#>  timechange    0.3.0   2024-01-18 [1] CRAN (R 4.3.2)
#>  tzdb          0.4.0   2023-05-12 [1] CRAN (R 4.3.2)
#>  units         0.8-5   2023-11-28 [1] CRAN (R 4.3.2)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.3.2)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.3.2)
#>  withr         3.0.0   2024-01-16 [1] CRAN (R 4.3.2)
#>  xfun          0.42    2024-02-08 [1] CRAN (R 4.3.2)
#>  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.3.2)
#> 
#>  [1] C:/Users/nicholas.gardner/AppData/Local/R/win-library/4.3
#>  [2] C:/Program Files/R/R-4.3.2/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
1 Like

hi @nicholas.gardner and welcome to the forum!

I think the error is because R is not recognizing the age_categories() function. This is from the epikit package, which I don’t see loaded in your pacman::p_load() command.

Try adding epikit and does that solve the error?

2 Likes

Hi Neale,

Thanks for your response. Apologies I think in the initial post I was referring to a different error which I have now amended. I have updated the original post with epikit in the p_load() command.

The error I initially referred to should now occur: Error: Error in age_categories(., age, lower = 0, upper = 70, breakers = 10): ‘list’ object cannot be coerced to type ‘double

Thanks,
Nick

1 Like

Hi @nicholas.gardner
We have two issues there. First, the argument name you probably is looking for is by not breakers and we should assign the age_categories function output to a new column name inside mutate.

Something like that:

flu <- demo_data %>%
        mutate(age = as.numeric(age),
                age_cat = age_categories(age, lower = 0, upper = 70, by = 10))
2 Likes

Thanks @lnielsen that works!

2 Likes