You can also use sum() to return the number of rows that meet a logical criteria. na.rm = TRUE for most mathematical functions). Within the statistical function, give the column(s) to be operated on and any relevant arguments (e.g. The syntax is the same - within the summarise() parentheses you provide the names of each new summary column followed by an equals sign and a statistical function to apply. As above, these outputs can be produced for the whole data frame set, or by group. You can also use sum() to return the number of rows that meet certain logical criteria. One major advantage of dplyr and summarise() is the ability to return more advanced statistical summaries like median(), mean(), max(), min(), sd() (standard deviation), and percentiles. Thus, the final step of calculating proportions (denominator sum(n)) is still grouped by outcome. Importantly - as it finishes its process, count() also ungroups the age_cat grouping, so the only remaining data grouping is the original grouping by outcome. This function further groups the data by age_cat and returns counts for each outcome- age-cat combination. First, the data are grouped on outcome via group_by(). It relies on different levels of data grouping being selectively applied and removed. Use table() from base R if you do not have access to the above packagesĪge_summary % count ( age_cat ) %>% # group and count by gender (produces "n" column) mutate ( # create percent of column - note the denominator percent = scales :: percent ( n / sum ( n ) ) ) # print age_summary # age_cat n percentīelow is a method to calculate proportions within groups.Use tbl_summary() from gtsummary to produce detailed publication-ready tables.Use summarise() and count() from dplyr for more complex statistics, tidy data frame outputs, or preparing data for ggplot().Use get_summary_stats() from rstatix to easily generate data frames of numeric summary statistics for multiple columns and/or groups.Use tabyl() from janitor to produce and “adorn” tabulations and cross-tabulations.Consider the points below as you choose the tool for your situation. png/.jpeg/.html image), and ease of post-processing. Some of the factors to consider include code simplicity, customizeability, the desired output (printed to R console, as data frame, or as “pretty”. You have several choices when producing tabulation and cross-tabulation summary tables. Use this page to decide which approach works for your scenario. This page covers how to create* the underlying tables, whereas the Tables for presentation page covers how to nicely format and print them.*Įach of these packages has advantages and disadvantages in the areas of code simplicity, accessibility of outputs, quality of printed outputs. This page demonstrates the use of janitor, dplyr, gtsummary, rstatix, and base R to summarise data and create tables with descriptive statistics. 46 Version control and collaboration with Git and Github.33 Demographic pyramids and Likert-scales.19 Univariate and multivariable regression.Obviously, rendering of the table can be tweaked further by changing/adding arguments of the rendering functions in the automatically generated code. You can make them work on PDF and Word by adding always_allow_html: yes in the yaml header of the Rmd, and installing phantomjs using: webshot::install_phantomjs() (results are not that good, though).Ī useful feature is that, for larger tables, you can also cut and paste content from a spreadsheet : kable works everywhere, while DT and rhandsontable work out of the box only if knitting to html. IMPORTANT NOTE: Not all output formats play well with knitting to PDF or Word!. After clicking Done the Addin will add in the file the code needed to generate the table in a nice tribble format (thanks to Miles McBain’s datapasta package!) to allow easier additional editing, and also the code needed to render it with the selected output format using some default options, as can be seen below: In this case, a GUI will open allowing you to select the desired output format ( kable, kableExtra, DT and rhandsontable are currently implemented), and to edit the content of the table. Launch the addin with the cursor on a empty line
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |