Skip to contents

[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
)

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.

Value

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

Note

The decimal precision is based on two aspects, one is the original precision from the variable or the decimal argument, and the second is the common use that has been defined in getOption("mcradds.precision.default"). So if you want to change the second decimal precision, you can alter it manually with option().

Examples

data(adsl_sub)

# Compute the default statistics of AGE by TRTP group
adsl_sub %>%
  descvar(
    var = "AGE",
    bygroup = "TRTP"
  )
#> Error in as.list.default(X): no method for coercing this S4 class to a vector

# 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
  )
#> Error in as.list.default(X): no method for coercing this S4 class to a vector

# 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
  )
#> Error in as.list.default(X): no method for coercing this S4 class to a vector

# 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
  )
#> Error in as.list.default(X): no method for coercing this S4 class to a vector