I am new to gtsummary package, but when I try to change summary statistics in the tbl_summary function, it gives me error messages, I tried to use the code with different data frames, but the same problem exists.
Here is my code:
> data %>%
+ tbl_summary(statistic = list(
+ all_continuous() ~ "{mean} ({sd})",
+ all_categorical() ~ "{n} ({P}%)"
+ ))
Error in `mutate()`:
ℹ In argument: `tbl_stats = pmap(...)`.
Caused by error in `pmap()`:
ℹ In index: 1.
Caused by error in `value[[3L]]()`:
! There was an error assembling the summary statistics for 'age'
with summary type 'categorical'.
There are 2 common sources for this error.
1. You have requested summary statistics meant for continuous
variables for a variable being as summarized as categorical.
To change the summary type to continuous, add the argument
`type = list(age ~ 'continuous')`
2. One of the functions or statistics from the `statistic=` argument is not valid.
Run `rlang::last_trace()` to see where the error occurred.
> rlang::last_trace()
<error/dplyr:::mutate_error>
Error in `mutate()`:
ℹ In argument: `tbl_stats = pmap(...)`.
Caused by error in `pmap()`:
ℹ In index: 1.
Caused by error in `value[[3L]]()`:
! There was an error assembling the summary statistics for 'age'
with summary type 'categorical'.
There are 2 common sources for this error.
1. You have requested summary statistics meant for continuous
variables for a variable being as summarized as categorical.
To change the summary type to continuous, add the argument
`type = list(age ~ 'continuous')`
2. One of the functions or statistics from the `statistic=` argument is not valid.
---
Backtrace:
▆
1. ├─data %>% ...
2. ├─gtsummary::tbl_summary(...)
3. │ └─... %>% ...
4. ├─dplyr::select(., "variable", "var_type", "var_label", everything())
5. ├─tidyr::unnest(., "tbl_stats")
6. ├─dplyr::select(., var_type = "summary_type", "var_label", "tbl_stats")
7. ├─dplyr::mutate(...)
8. ├─dplyr:::mutate.data.frame(...)
9. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
10. │ ├─base::withCallingHandlers(...)
11. │ └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
12. │ └─mask$eval_all_mutate(quo)
13. │ └─dplyr (local) eval()
14. └─purrr::pmap(...)
15. └─purrr:::pmap_("list", .l, .f, ..., .progress = .progress)
16. ├─purrr:::with_indexed_errors(...)
17. │ └─base::withCallingHandlers(...)
18. ├─purrr:::call_with_cleanup(...)
19. └─gtsummary (local) .f(...)
20. └─gtsummary:::df_stats_to_tbl(...)
21. └─base::tryCatch(...)
22. └─base (local) tryCatchList(expr, classes, parentenv, handlers)
23. └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
24. └─value[[3L]](cond)
25. └─base::stop(...)
> summary(data)
age year gender residence
Min. :17.00 Length:373 Length:373 Length:373
1st Qu.:18.00 Class :character Class :character Class :character
Median :18.00 Mode :character Mode :character Mode :character
Mean :18.67
3rd Qu.:19.00
Max. :30.00
> data
# A tibble: 373 × 4
age year gender residence
<dbl> <chr> <chr> <chr>
1 18 1st year Male Rural (Countryside)
2 19 1st year Female Rural (Countryside)
3 18 1st year Male Urban (City)
4 18 1st year Female Urban (City)
5 18 1st year Female Urban (City)
6 18 1st year Female Urban (City)
7 17 1st year Female Rural (Countryside)
8 19 2nd year Female Urban (City)
9 21 2nd year Male Urban (City)
10 18 1st year Male Rural (Countryside)
# ℹ 363 more rows
# ℹ Use `print(n = ...)` to see more rows
It tells me that age is a categorical variable, but it is a continuous variable, even after removing age, it will give me similar errors with other variables in my data.
I will be happy if someone know how I can fix this, thanks!