- Notifications
You must be signed in to change notification settings - Fork8
Easy and thorough description of datasets
DanChaltiel/crosstable
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Crosstable is a package centered on a single function,crosstable,which easily computes descriptive statistics on datasets. It can use thetidyverse syntax and is interfaced with the packageofficer tocreate automatized reports.
# Install last version available on CRANinstall.packages("crosstable")# Install development version on Githubremotes::install_github("DanChaltiel/crosstable@v0.8.2.9002")
Note that, for reproducibility purpose, an even better solution would beto userenv.
Here are 2 examples to try and show you the main features ofcrosstable. See thedocumentationwebsite for more.
Dear crosstable, using the
mtcars2dataset, please describe columnsdispandvsdepending on the levels of columnam, with totals inboth rows and columns, and with proportions formatted with group size,percent on row and percent on column, with no decimals.
library(crosstable)ct1= crosstable(mtcars2, c(disp,vs),by=am,total="both",percent_pattern="{n} ({p_row}/{p_col})",percent_digits=0) %>% as_flextable()ct1
With only a few arguments, we did select which column to describe(c(disp, vs)), define a grouping variable (by=am), set thepercentage calculation in row/column (percent_pattern=), and ask fortotals (total=).
Sincemtcars2 is a dataset with labels, they are displayed instead ofthe variable name (seeherefor how to add some).
Ascrosstable() is returning adata.frame, we useas_flextable()to output a beautiful HTML table. This one can even be exported to MSWord with a few more lines of code (seehereto learn how).
Here is a more advanced example.
Dear crosstable, using the
mtcars2dataset again, please describeall columns whose name starts with "cy" and those whose name ends with"at", depending on the levels of both columnsamandvs, withoutconsidering labels, applyingmean()andquantile()as summaryfunction, withprobs25% and 75% defined for this latter function,and with 3 decimals for numeric variables:
ct2= crosstable(mtcars2, c(starts_with("cy"), ends_with("at")),by=c(am,vs),label=FALSE,num_digits=3,funs=c(mean,quantile),funs_arg=list(probs=c(.25,.75))) %>% as_flextable(compact=TRUE,header_show_n=1:2)ct2
Here, the variables were selected usingtidyselect helpers and thesummary functionsmean andquantile were specified, along withargumentprobs for the latter. Usinglabel=FALSE allowed to seewhich variables were selected but it is best to keep the labels in thefinal table.
Inas_flextable(), thecompact=TRUE option yields a longer output,which may be more suited in some contexts (for instance forpublication), andheader_show_n=1:2 adds the group sizes for both rowsof the header.
You can find the whole documentation on thededicatedwebsite:
vignette("crosstable")for a first step-by-step guide on how to usecrosstable(link)vignette("crosstable-report")for more on creating MS Word reportsusing either{officer}orRmarkdown(link)vignette("pertent_pattern")for more on how to usepercent_pattern(link)vignette("crosstable-selection")for more on variable selection(link),although you should better readhttps://tidyselect.r-lib.org/articles/syntax.html.
There are lots of other features you can learn about there, for instance(non-exhaustive list):
- description of correlation, dates, and survival data(link)
- variable selection with functions, e.g.
is.numeric(link) - formula interface, allowing to describe more mutated columns,e.g.
sqrt(mpg)orSurv(time, event)(link) - automatic computation of statistical tests(link)and of effect sizes(link)
- global options to avoid repeating arguments(link)
If you have a question about how to usecrosstable, please ask onStackOverflow with the tagcrosstable.You can@DanChaltiel in a comment if you are struggling to getanswers. Don't forget to add a minimalreproducibleexample toyour question, ideally using thereprexpackage.
If you miss any feature that you think would belong incrosstable,please fill aFeatureRequestissue.
If you encounter an unexpected error while usingcrosstable, pleasefill aBugReportissue. In case of any installation problem, try the solutions proposedinthisarticlefirst.
In its earliest development phase,crosstable was based on the awesomepackagebiostat2 written byDavid Hajage. Thanks David!
About
Easy and thorough description of datasets
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors8
Uh oh!
There was an error while loading.Please reload this page.


