if (FALSE) {
# The left-hand side of the formula describes rows, and the right-hand side
# describes columns. This table uses the "mpg" variable as a row and the "mean"
# function as a column:
datasummary(mpg ~ mean, data = mtcars)
# This table uses the "mean" function as a row and the "mpg" variable as a column:
datasummary(mean ~ mpg, data = mtcars)
# Display several variables or functions of the data using the "+"
# concatenation operator. This table has 2 rows and 2 columns:
datasummary(hp + mpg ~ mean + sd, data = mtcars)
# Nest variables or statistics inside a "factor" variable using the "*" nesting
# operator. This table shows the mean of "hp" and "mpg" for each value of
# "cyl":
mtcars$cyl <- as.factor(mtcars$cyl)
datasummary(hp + mpg ~ cyl * mean, data = mtcars)
# If you don't want to convert your original data
# to factors, you can use the 'Factor()'
# function inside 'datasummary' to obtain an identical result:
datasummary(hp + mpg ~ Factor(cyl) * mean, data = mtcars)
# You can nest several variables or statistics inside a factor by using
# parentheses. This table shows the mean and the standard deviation for each
# subset of "cyl":
datasummary(hp + mpg ~ cyl * (mean + sd), data = mtcars)
# Summarize all numeric variables with 'All()'
datasummary(All(mtcars) ~ mean + sd, data = mtcars)
# Define custom summary statistics. Your custom function should accept a vector
# of numeric values and return a single numeric or string value:
minmax <- function(x) sprintf("[%.2f, %.2f]", min(x), max(x))
mean_na <- function(x) mean(x, na.rm = TRUE)
datasummary(hp + mpg ~ minmax + mean_na, data = mtcars)
# To handle missing values, you can pass arguments to your functions using
# '*Arguments()'
datasummary(hp + mpg ~ mean * Arguments(na.rm = TRUE), data = mtcars)
# For convenience, 'modelsummary' supplies several convenience functions
# with the argument `na.rm=TRUE` by default: Mean, Median, Min, Max, SD, Var,
# P0, P25, P50, P75, P100, NUnique, Histogram
datasummary(hp + mpg ~ Mean + SD + Histogram, data = mtcars)
# These functions also accept a 'fmt' argument which allows you to
# round/format the results
datasummary(hp + mpg ~ Mean * Arguments(fmt = "%.3f") + SD * Arguments(fmt = "%.1f"), data = mtcars)
# Save your tables to a variety of output formats:
f <- hp + mpg ~ Mean + SD
datasummary(f, data = mtcars, output = 'table.html')
datasummary(f, data = mtcars, output = 'table.tex')
datasummary(f, data = mtcars, output = 'table.md')
datasummary(f, data = mtcars, output = 'table.docx')
datasummary(f, data = mtcars, output = 'table.pptx')
datasummary(f, data = mtcars, output = 'table.jpg')
datasummary(f, data = mtcars, output = 'table.png')
# Display human-readable code
datasummary(f, data = mtcars, output = 'html')
datasummary(f, data = mtcars, output = 'markdown')
datasummary(f, data = mtcars, output = 'latex')
# Return a table object to customize using a table-making package
datasummary(f, data = mtcars, output = 'gt')
datasummary(f, data = mtcars, output = 'kableExtra')
datasummary(f, data = mtcars, output = 'flextable')
datasummary(f, data = mtcars, output = 'huxtable')
# add_rows
new_rows <- data.frame(a = 1:2, b = 2:3, c = 4:5)
attr(new_rows, 'position') <- c(1, 3)
datasummary(mpg + hp ~ mean + sd, data = mtcars, add_rows = new_rows)
}
Run the code above in your browser using DataLab