The objective of this package is to facilitate producing reports using Rmarkdown, so the functions are of that type that you “you always have to use, but you always have to create”.
tblThis is the main function of fastrep, with her you can
make tables in HTML or latex format, the main
idea is to provide minimal parameters to create their own table, so you
just need to provide a data.frame.
df = Orange |>
dplyr::group_by(Tree) |>
dplyr::count()
fastrep::tbl(df)| Tree | n |
|---|---|
| 3 | 7 |
| 1 | 7 |
| 5 | 7 |
| 2 | 7 |
| 4 | 7 |
As you can see the only parameters is df, you can put a
title in the table using title
df |>
fastrep::tbl(title = "My Title!", full_page = F)| Tree | n |
|---|---|
| 3 | 7 |
| 1 | 7 |
| 5 | 7 |
| 2 | 7 |
| 4 | 7 |
The value of format will be automatically determined if the function
is called within a knitr document, but in your section, the default
format is html, if you want to change just use the argument
format which the possibles values are html or
latex. If you want the code that generates the table you
can use the argument code.
df |>
fastrep::tbl(title = "My Title!", code = T, format = "latex")You can choose if you want rows separating the cells in the table
(LaTeX version), using tabs = TRUE
freq_tblThis function creates a frequency table, you only need to supply a
data.frame and the variable to make the table.
infert |>
fastrep::freq_tbl(education)
#> # A tibble: 4 × 3
#> education absolute_frequency relative_frequency
#> <chr> <int> <dbl>
#> 1 0-5yrs 12 0.0484
#> 2 6-11yrs 120 0.484
#> 3 12+ yrs 116 0.468
#> 4 Total 248 1If you want to generate a table using the output, just combine it
with tbl
infert |>
fastrep::freq_tbl(education) |>
fastrep::tbl()| education | absolute_frequency | relative_frequency |
|---|---|---|
| 0-5yrs | 12 | 0.0484 |
| 6-11yrs | 120 | 0.4839 |
| 12+ yrs | 116 | 0.4677 |
| Total | 248 | 1.0000 |
You can order the output using sort_by, which has two
options: absolute_frequency and
relative_frequency.
chickwts |>
fastrep::freq_tbl(feed, sort_by = absolute_frequency)
#> # A tibble: 7 × 3
#> feed absolute_frequency relative_frequency
#> <chr> <int> <dbl>
#> 1 horsebean 10 0.141
#> 2 meatmeal 11 0.155
#> 3 casein 12 0.169
#> 4 linseed 12 0.169
#> 5 sunflower 12 0.169
#> 6 soybean 14 0.197
#> 7 Total 71 1.00And if you want decreasing order put desc = T
chickwts |>
fastrep::freq_tbl(feed, desc = T, sort_by = relative_frequency)
#> # A tibble: 7 × 3
#> feed absolute_frequency relative_frequency
#> <chr> <int> <dbl>
#> 1 soybean 14 0.197
#> 2 casein 12 0.169
#> 3 linseed 12 0.169
#> 4 sunflower 12 0.169
#> 5 meatmeal 11 0.155
#> 6 horsebean 10 0.141
#> 7 Total 71 1.00cross_tblThis function makes cross tables, like all functions from
fastrep you need to supply a data.frame, and
in this case two variables.
infert |>
fastrep::cross_tbl(education, spontaneous)|
spontaneous
|
||||
|---|---|---|---|---|
| education | 0 | 1 | 2 | Total |
| 0-5yrs | 9 | 1 | 2 | 12 |
| 6-11yrs | 71 | 33 | 16 | 120 |
| 12+ yrs | 61 | 37 | 18 | 116 |
| Total | 141 | 71 | 36 | 248 |
The second variable (var2) is on the top of the table,
if you want the marginal row table use marg = TRUE.
infert |>
fastrep::cross_tbl(education, spontaneous, marg = T)|
spontaneous
|
|||
|---|---|---|---|
| education | 0 | 1 | 2 |
| 0-5yrs | 0.7500000 | 0.0833333 | 0.1666667 |
| 6-11yrs | 0.5916667 | 0.2750000 | 0.1333333 |
| 12+ yrs | 0.5258621 | 0.3189655 | 0.1551724 |
describeIn the base R we have the function summary, but the output is no by
default a data.frame, so describe is an
enhancement of this function to summarize data frames.
mtcars |>
fastrep::describe()
#> # A tibble: 11 × 7
#> variable mean median sd min max na_count
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 mpg 20.1 19.2 6.03 10.4 33.9 0
#> 2 cyl 6.19 6 1.79 4 8 0
#> 3 disp 231. 196. 124. 71.1 472 0
#> 4 hp 147. 123 68.6 52 335 0
#> 5 drat 3.60 3.70 0.535 2.76 4.93 0
#> 6 wt 3.22 3.32 0.978 1.51 5.42 0
#> 7 qsec 17.8 17.7 1.79 14.5 22.9 0
#> 8 vs 0.438 0 0.504 0 1 0
#> 9 am 0.406 0 0.499 0 1 0
#> 10 gear 3.69 4 0.738 3 5 0
#> 11 carb 2.81 2 1.62 1 8 0As you’ve probably noticed, the output is a tibble, so
you can combine with tbl
mtcars |>
fastrep::describe() |>
fastrep::tbl("Summary of mtcars")| variable | mean | median | sd | min | max | na_count |
|---|---|---|---|---|---|---|
| mpg | 20.091 | 19.200 | 6.027 | 10.400 | 33.900 | 0 |
| cyl | 6.188 | 6.000 | 1.786 | 4.000 | 8.000 | 0 |
| disp | 230.722 | 196.300 | 123.939 | 71.100 | 472.000 | 0 |
| hp | 146.688 | 123.000 | 68.563 | 52.000 | 335.000 | 0 |
| drat | 3.597 | 3.695 | 0.535 | 2.760 | 4.930 | 0 |
| wt | 3.217 | 3.325 | 0.978 | 1.513 | 5.424 | 0 |
| qsec | 17.849 | 17.710 | 1.787 | 14.500 | 22.900 | 0 |
| vs | 0.438 | 0.000 | 0.504 | 0.000 | 1.000 | 0 |
| am | 0.406 | 0.000 | 0.499 | 0.000 | 1.000 | 0 |
| gear | 3.688 | 4.000 | 0.738 | 3.000 | 5.000 | 0 |
| carb | 2.812 | 2.000 | 1.615 | 1.000 | 8.000 | 0 |