Hello,
I am an Epidemiologist who has just now completed my Intro training to R course. As part of the reprex code exercise, I have created an error in my code and copying the error below to request guidance in troubleshooting this code. Any help will be greatly appreciated. Reprex is below.
pacman::p_load(
rio, # for importing data
here, # for locating files
skimr, # for reviewing the data
janitor, # for data cleaning
epikit, # creating age categories
gtsummary, # creating tables
RColorBrewer, # for colour palettes
viridis, # for more colour palettes
scales, # percents in tables
flextable, # for making pretty tables
gghighlight, # highlighting plot parts
ggExtra, # special plotting functions
datapasta,
reprex,
styler,
naniar, # replace values with NA
tidyverse # for data management and visualization
)
# Import data -------------------------------------------------------------
# importing the file not from a project folder using here() as coded below, followed by using read.csv()
file_path <- here ("C:/Users/vxe9/Desktop/intro_course/learning_materials/extra_datasets/H7N9_china_2013_EN.csv")
h1n1 <- data.frame(
stringsAsFactors = FALSE,
case_id = c(1L, 2L, 3L, 4L, 5L),
date_of_symptoms = c("2/19/2013",
"2/27/2013","3/9/2013","3/19/2013","3/19/2013"),
date_of_hospitalisation = c(NA,"3/3/2013",
"3/19/2013","3/27/2013","3/30/2013"),
date_of_result = c("3/4/2013",
"3/10/2013","4/9/2013",NA,"5/15/2013"),
sex = c("m", "m", "f", "f", "f"),
age = c("87", "27", "35", "45", "48"),
province = c("Shanghai",
"Shanghai","Anhui","Jiangsu","Jiangsu")
)
# clean the imported data
h1n1_cl <- h1n1 %>%
clean_names() %>%
distinct () %>%
# rename variables
# new name = old name
rename(
symp_date = date_of_symptoms,
hosp_date = date_of_hospitalisation,
result_date = date_of_result
) %>%
# transform variables
mutate (
symp_date = mdy (symp_date),
hosp_date = mdy (hosp_date),
result_date = ymd (result_date),
sex = recode (sex,
"m" = "male",
"f" = "female"),
age_cat = age_categories (
age,
breakers = c(0, 10, 20, 30, 40, 50, 60, 70))) %>%
replace_with_na(replace = list(result = c("", "D"))) %>% #replace_with_na() of naniar package replaces specific values with na
filter (province != "Anhui") %>%
drop_na(age_cat)
#> Warning: There was 1 warning in `mutate()`.
#> βΉ In argument: `result_date = ymd(result_date)`.
#> Caused by warning:
#> ! All formats failed to parse. No formats found.
#> Warning: Missing from data: `result`
Created on 2024-03-09 with reprex v2.0.2
Session info
sessionInfo()
#> R version 4.2.2 (2022-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=English_United States.utf8
#> [2] LC_CTYPE=English_United States.utf8
#> [3] LC_MONETARY=English_United States.utf8
#> [4] LC_NUMERIC=C
#> [5] LC_TIME=English_United States.utf8
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] lubridate_1.9.3 forcats_1.0.0 stringr_1.5.0 dplyr_1.1.3
#> [5] purrr_1.0.2 readr_2.1.4 tidyr_1.3.0 tibble_3.2.1
#> [9] tidyverse_2.0.0 naniar_1.1.0 styler_1.10.2 reprex_2.0.2
#> [13] datapasta_3.1.0 ggExtra_0.10.1 gghighlight_0.4.1 ggplot2_3.4.4
#> [17] flextable_0.9.4 scales_1.2.1 viridis_0.6.5 viridisLite_0.4.2
#> [21] RColorBrewer_1.1-3 gtsummary_1.7.2 epikit_0.1.6 janitor_2.2.0
#> [25] skimr_2.1.5 here_1.0.1 rio_1.0.1
#>
#> loaded via a namespace (and not attached):
#> [1] colorspace_2.1-0 ellipsis_0.3.2 class_7.3-20
#> [4] visdat_0.6.0 rprojroot_2.0.4 snakecase_0.11.1
#> [7] base64enc_0.1-3 fs_1.6.3 httpcode_0.3.0
#> [10] rstudioapi_0.15.0 proxy_0.4-27 fansi_1.0.4
#> [13] xml2_1.3.5 R.methodsS3_1.8.2 knitr_1.44
#> [16] jsonlite_1.8.7 gt_0.10.1 R.oo_1.25.0
#> [19] shiny_1.8.0 compiler_4.2.2 fastmap_1.1.1
#> [22] cli_3.6.1 later_1.3.2 htmltools_0.5.6.1
#> [25] tools_4.2.2 gtable_0.3.4 glue_1.6.2
#> [28] Rcpp_1.0.11 fontquiver_0.2.1 vctrs_0.6.3
#> [31] crul_1.4.0 broom.helpers_1.14.0 xfun_0.40
#> [34] timechange_0.2.0 mime_0.12 miniUI_0.1.1.1
#> [37] lifecycle_1.0.3 pacman_0.5.1 ragg_1.2.6
#> [40] hms_1.1.3 promises_1.2.1 fontLiberation_0.1.0
#> [43] yaml_2.3.7 curl_5.1.0 gridExtra_2.3
#> [46] gdtools_0.3.5 stringi_1.7.12 fontBitstreamVera_0.1.1
#> [49] e1071_1.7-14 zip_2.3.0 repr_1.1.6
#> [52] rlang_1.1.1 pkgconfig_2.0.3 systemfonts_1.0.5
#> [55] evaluate_0.22 sf_1.0-15 tidyselect_1.2.0
#> [58] magrittr_2.0.3 R6_2.5.1 generics_0.1.3
#> [61] DBI_1.1.3 pillar_1.9.0 withr_2.5.0
#> [64] units_0.8-5 crayon_1.5.2 gfonts_0.2.0
#> [67] uuid_1.1-1 KernSmooth_2.23-20 utf8_1.2.3
#> [70] tzdb_0.4.0 rmarkdown_2.25 officer_0.6.3
#> [73] grid_4.2.2 data.table_1.14.8 digest_0.6.33
#> [76] classInt_0.4-10 xtable_1.8-4 R.cache_0.16.0
#> [79] httpuv_1.6.14 R.utils_2.12.3 textshaping_0.3.7
#> [82] openssl_2.1.1 munsell_0.5.0 askpass_1.2.0
Describe your issue
- What specifically do you need help with
- Relevant context (e.g. public health trends or associated data infrastructure)
- Timeline/urgency
- (No sensitive or identifiable information)
What steps have you already taken to find an answer?
- Ensure you have searched the Epi R Handbook and internet forums like stackoverflow: https://stackoverflow.com/
Provide an example of your R code
-
Watch this video: https://www.youtube.com/watch?v=XIc-VHFeUl8
This document is also helpful: Reprex do's and don'ts β’ reprex -
If pasting your code, put it in backticks so that others can easy copy/paste:
- Use single backticks to make text appear as code, like
mutate()
andfilter()
- Use 3 backticks on lines above and below a large block of text to make it appear as code
male_cases <- linelist %>% filter(gender == "male")
- Use single backticks to make text appear as code, like
Follow-up
- Thank the volunteers who try to help you
- Mark one reply as the βSolutionβ if appropriate