Learn R Programming

mcradds (version 1.1.1)

descvar: Summarize Descriptive Statistics

Description

[Experimental]

Create a summary table with a set of descriptive statistics for one or more variables by one group, as well as a total column if necessary.

Usage

descvar(
  data,
  var,
  bygroup,
  stats = getOption("mcradds.stats.default"),
  autodecimal = TRUE,
  decimal = 1,
  addtot = FALSE,
  .perctype = 2
)

Value

A object Desc contains an intermediate data with long form for post-processing and final data with wide form for presentation.

Arguments

data

(data.frame)
a data frame that contains the variables to be summarized and grouped.

var

(vector)
a character vector of variables to be summarized within data.

bygroup

(string)
a character variable for grouping within data.

stats

(vector)
a statistics character vector must be chosen from c("N", "MEAN", "SD", "MEDIAN", "MAX", "MIN", "Q1", "Q3", "MEANSD", "RANGE", "IQR", "MEDRANGE", "MEDIQR"), and the default are top six items.

autodecimal

(logical)
whether to capture the variable's maximum decimal, and the final decimal precision is equal to the variable decimal plus the definition of each statistic from getOption("mcradds.precision.default").

decimal

(integer)
a integer number to define the decimal precision for each variable.

addtot

(logical)
whether to add total column in the output or not.

.perctype

(integer)
an integer between 1 and 9 selecting one of the nine quantile algorithms, also see the details in quantile(). The default is 2, so that it can be consistent with SAS quantile calculation.

Examples

Run this code
data(adsl_sub)

# Compute the default statistics of AGE by TRTP group
adsl_sub %>%
  descvar(
    var = "AGE",
    bygroup = "TRTP"
  )

# Compute the specific statistics of BMI by TRTP group, adding total column
adsl_sub %>%
  descvar(
    var = "BMIBL",
    bygroup = "TRTP",
    stats = c("N", "MEANSD", "MEDIAN", "RANGE", "IQR"),
    addtot = TRUE
  )

# Set extra decimal to define precision
adsl_sub %>%
  descvar(
    var = "BMIBL",
    bygroup = "TRTP",
    stats = c("N", "MEANSD", "MEDIAN", "RANGE", "IQR"),
    autodecimal = FALSE,
    decimal = 2,
    addtot = TRUE
  )

# Set multiple variables together
adsl_sub %>%
  descvar(
    var = c("AGE", "BMIBL", "HEIGHTBL"),
    bygroup = "TRTP",
    stats = c("N", "MEANSD", "MEDIAN", "RANGE", "IQR"),
    autodecimal = TRUE,
    addtot = TRUE
  )

Run the code above in your browser using DataLab