Skip to contents

Summarize data into a data.frame with row(s). Includes optional stratification and null hypothesis testing using a factor or logical variable.

Usage

build_row(x, ...)

# S3 method for data.frame
build_row(
  x,
  y = NA_real_,
  label = NULL,
  label.stat = TRUE,
  stat.pct.sign = FALSE,
  col.overall = TRUE,
  col.missing = FALSE,
  col.test = FALSE,
  digits = 1,
  ...
)

# S3 method for numeric
build_row(
  x,
  y = NA_real_,
  label = NULL,
  label.stat = TRUE,
  stat = c("mean", "median"),
  stat.pct.sign = FALSE,
  col.overall = TRUE,
  col.missing = FALSE,
  test = c("anova", "kruskal", "wilcoxon"),
  col.test = FALSE,
  digits = 1,
  p.digits = 4,
  ...
)

# S3 method for logical
build_row(
  x,
  y = NA_real_,
  label = NULL,
  label.stat = TRUE,
  inverse = FALSE,
  stat.pct.sign = FALSE,
  col.overall = TRUE,
  col.missing = FALSE,
  test = c("chisq", "fisher"),
  test.simulate.p = FALSE,
  col.test = FALSE,
  digits = 1,
  p.digits = 4,
  ...
)

# S3 method for factor
build_row(
  x,
  y = NA_real_,
  label = NULL,
  label.stat = TRUE,
  stat.pct.sign = FALSE,
  col.overall = TRUE,
  col.missing = FALSE,
  test = c("chisq", "fisher"),
  test.simulate.p = FALSE,
  col.test = FALSE,
  digits = 1,
  p.digits = 4,
  ...
)

Arguments

x

A data.frame, numeric, factor, or logical. Data to summarize.

...

Arguments passed to the appropriate S3 method.

y

A factor or logical. Data to optionally stratify x by.

label

A character. A label for the summarized data.

label.stat

A logical. Append the summary statistic used to the label.

stat.pct.sign

A logical. Paste a percentage symbol with each frequency. frequency.

col.overall

A logical. Append a column with the statistic for all data. If y is not specified, this parameter is ignored.

col.missing

A logical. Append a column with counts of missing data.

col.test

A logical. Append a column with the name of the statistical test used.

digits

An integer. Number of digits to round to.

stat

A character. Name of the summary statistic to use. Supported options include the mean ('mean') and median ('median') for continuous data.

test

A character. Name of statistical test to compare groups. Supported options: [continuous data] ANOVA linear model ('anova'), Kruskal-Wallis ('kruskal'), and Wilcoxon rank sum ('wilcoxon') tests; [nominal data] Pearson's Chi-squared Test ('chisq') and Fisher's Exact Test ('fisher').

p.digits

An integer. Number of p-value digits to report.

inverse

A logical. For logical data, report frequencies of the FALSE values instead.

test.simulate.p

A logical. Whether to use Monte Carlo simulation of the p-value when testing nominal data.

Value

An object of class tbl_df (tibble) summarizing the provided data.

Examples

strata <- as.factor(datasets::mtcars$cyl)

# Create a "count" row from a data.frame for a factor
build_row(x = datasets::mtcars, y = strata)
#> # A tibble: 1 × 6
#>   Variable Overall `4`       `6`      `8`       p    
#>   <chr>    <chr>   <chr>     <chr>    <chr>     <chr>
#> 1 n(%)     32      11 (34.4) 7 (21.9) 14 (43.8) ""   

# Create a row summarizing a numeric by a factor
build_row(label = 'MPG', x = as.numeric(datasets::mtcars$mpg), y = strata)
#> # A tibble: 1 × 6
#>   Variable     Overall `4`       `6`       `8`       p      
#>   <chr>        <chr>   <chr>     <chr>     <chr>     <chr>  
#> 1 MPG, mean±SD 20.1 ±6 26.7 ±4.5 19.7 ±1.5 15.1 ±2.6 <0.0001

# Create a row summarizing a logical by a factor
build_row(label = 'VS', x = as.logical(datasets::mtcars$vs), y = strata)
#> # A tibble: 1 × 6
#>   Variable Overall   `4`       `6`      `8`   p      
#>   <chr>    <chr>     <chr>     <chr>    <chr> <chr>  
#> 1 VS, n(%) 14 (43.8) 10 (90.9) 4 (57.1) 0 (0) <0.0001

# Create a row summarizing a factor by a factor
build_row(label = 'Carb', x = as.factor(datasets::mtcars$carb), y = strata)
#> # A tibble: 7 × 6
#>   Variable     Overall     `4`        `6`        `8`        p       
#>   <chr>        <chr>       <chr>      <chr>      <chr>      <chr>   
#> 1 "Carb, n(%)" ""          ""         ""         ""         "0.0066"
#> 2 "  1"        "7 (21.9)"  "5 (45.5)" "2 (28.6)" "0 (0)"    ""      
#> 3 "  2"        "10 (31.2)" "6 (54.5)" "0 (0)"    "4 (28.6)" ""      
#> 4 "  3"        "3 (9.4)"   "0 (0)"    "0 (0)"    "3 (21.4)" ""      
#> 5 "  4"        "10 (31.2)" "0 (0)"    "4 (57.1)" "6 (42.9)" ""      
#> 6 "  6"        "1 (3.1)"   "0 (0)"    "1 (14.3)" "0 (0)"    ""      
#> 7 "  8"        "1 (3.1)"   "0 (0)"    "0 (0)"    "1 (7.1)"  ""