9/11/2023 0 Comments Rstudio summary statistics![]() To the best of my knowledge, gtsummary is the only package that can export tables as Excel files: df %>%Īs_hux_xlsx("descriptives_gtsummary.xlsx")Īrsenal also exports tables as Word files but requires a bit more coding. # this changes the table to a different format that can be saved as Wordįlextable::save_as_docx(path = "descriptives_gtsummary.docx", pr_section = sect_properties) The “by” command can be used to summarize the data by group. However, for interval scale variables such as age I’d rather have the mean and standard deviation than the median and the interquartile range. The information for the categorical / ordinal variables looks fine (frequencies and percentages). There is also a very quick way to get a descriptive table with the package gtsummary: df %>% # use flextable package to save table as wordįlextable::save_as_docx(path = "descriptives_crosstable.docx", pr_section = sect_properties) # Specify properties for the target documentĬrosstable::crosstable(by = "condition") %>% With the following code, it is also possible to save the table as a Word file (see here and here). # optimize formatting for web display (table looks better without next line) Grouping the data by a column (here the experimental condition) is also easy. A nicer output (e.g., for websites or R Studio’s viewer) can be obtained via knitr::kable(). In its simplest form, the code for this package couldn’t be shorter: crosstable(df) # A tibble: 13 × 4īy default, crosstable displays the table in the console. Let’s start with crosstable, the package that requires the least amount of code and has good default settings. df %ĭplyr::mutate(df, across(c(condition,gender,student), as.factor)) In this dataset, some of the categorical variables need updating from type numeric to factor. For this to work properly, it is essential that the “type” of the variables is correctly defined. Most of the packages introduced below automatically check the scale level of variables and then compute the appropriate statistic (e.g., frequencies for ordinal data). Select("age","gender","student","condition") To be able to replicate the code and create the table, you can download the sample data from a previous study directly from OSF: url % "officer", # for exporting tables to word "flextable", # for formatting and exporting tables to word ![]() "openxlsx", # to save table as Excel file "labelled", # for with labelled variables ![]() "sjlabelled", # for working with item labels (retrieving their content) Make sure all required packages are available and loaded: if (!require("pacman")) install.packages("pacman") To see how the Word files look, just click on the package’s name: In what follows, I present a couple of packages that require only little coding, automatically prepare the appropriate statistics, and offer great portability (i.e., save tables as Word or Excel files while keeping as much of the formatting as possible). Another advantage is that the process of creating the tables is automated, transparent, and replicable. Producing tables as Word or Excel files is very convenient when collaborating with others. still others can save tables directly as Word or Excel files others are intended for creating PDF files some only show the results in the console The packages differ in terms of portability: Preparing statistics for different subgroups (e.g., experimental conditions or administrative regions) may entail additional work (see here and here for some tips on how to prepare descriptive tables manually useful functions to prepare multiple summary statistics in one step include rstatix::get_summary_stats, psych::describe, Hmisc::describe, and DescrTab2::descr for ratio and interval data, and janitor::tabyl for nominal and ordinal data).įortunately, there are R packages that facilitate creating such tables. To illustrate, different types of variables require different statistics: Ratio and interval scale variables (e.g., age, test score) are best summarized by their mean and standard deviation, while nominal and ordinal data (e.g., gender, level of education) are better summarized by frequencies and percentages. Gathering and aggregating the relevant information can be tedious. Most reports of empirical work require a table that describes the study sample (e.g., people, animals, organizations).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |