tabular.ade: Tabular representation of a wide selection of statistics

Description Usage Arguments Details Value Author(s) Examples

View source: R/tabular.ade.R

Description

Creates simple to highly customized tables for a wide selection of descriptive statistics, with or without weighting the data.

Usage

1
2
3
4
5
tabular.ade(x_vars, xname=NULL, y_vars=NULL, yname=NULL,
            z_vars=NULL, zname=NULL,
            rows=NULL, rnames=NULL, cols=NULL, cnames=NULL, w=NULL,
            data=NULL, FUN, allnames=FALSE, nonames=TRUE, alllabel='Total',
            inset='?', remove='', n_min=0, ...)

Arguments

x_vars

This variable will be used to calculate the statistics for it.

  • a character string with the name of the variable in the data.frame

  • a vector of character strings with names of variables in data.frame

xname

Labels for x.

  • a character string with the label for x

  • a vector of character strings with labels for x, with the same length as x.

y_vars

This variable can be used to calculate bivariable statistics.

  • a character string with the name of the variable in the data.frame

  • a vector of character strings with names of variables in data.frame

yname

Labels for y.

  • a character string with the label for y

  • a vector of character strings with labels for y, with same length as x.

z_vars

This variable can be used for additional calculations.

  • a character string with the name of the variable in the data.frame

zname

Labels for z.

  • a character string with the label for y

rows

These factors will be used to separate the rows of the table in subgroups.

  • a character string with the name of the factor variable in the data.frame

  • a vector of character strings with names of factor variables in data.frame (max 6)

  • a vector with names of factors and/or Keyword 'ALL' adds extra overall group for leading factor.

rnames

Labels for rows.

  • a character string with the label for rows

  • a vector of character strings with labels for rows, with same length as rows.

  • a vector with names of factors and/or keyword 'ALL' adds extra overall group for leading factor.

cols

These factors will be used to separate the columns of the table in subgroups.

  • a character string with the name of the factor variable in the data.frame

  • a vector of character strings with names of factor variables in data.frame (max 6)

cnames

Labels for cols.

  • a character string with the label for cols

  • a vector of character strings with labels for rows, with same length as cols.

w

This numeric variable will be used to weight the table.

  • a character string with the name of the factor variable in the data.frame

data

A data frame with all used variables.

FUN

An abstract cell function to calculate statistics in every cell of the table. See details.

allnames

Logical asking whether to fill every cell with labels or only the first one.

nonames

Logical asking whether to use dimnames for variable labels or make all labeling in the table self.

alllabel

Label for overall group without splitting in this factor.

inset

Inset text in each cell, '?' will be replaced with the value of the cell.

remove

Remove a character string from each cell.

n_min

min N in each cell, it will be only passed in the cell function. But it is necessary to suppress calculation of statistics using only few values.

...

additional parameters passed to the FUN

Details

FUN can be a cell function from this package or a custom cell function.

The custom cell function must take the following parameters, but it is not necessary to use them.

For an example with simple mean see below.

Value

A character Matrix.(Table)

Author(s)

Andreas Schulz <ades-s@web.de>

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 1) simple own FUN cell function.
s_mean<- function(x, y, z, w, cell_ids, row_ids, col_ids, vnames, vars, n_min, ds=3){
out<- ''
if(length(cell_ids)>= n_min){
out<-  format(mean(x[cell_ids], na.rm=TRUE), digits=ds)
}
return(out)
}
##########################################
# 2) simple 2 x 2 table of means
sex   <- factor(rbinom(5000, 1, 0.5), labels=c('Men', 'Women'))
age   <- round(runif(5000, 18, 89))
treat <- factor(rbinom(5000, 1, 0.3), labels=c('control', 'treated'))
d<-data.frame(sex, age, treat)

tabular.ade(x_vars='age', xname='Age [y]', rows='sex', rnames='Sex', cols='treat',
cnames='Treatment', data=d, nonames=FALSE, FUN=s_mean)

##########################################
# 3) Relative frequency table
d$dosis <- round(runif(5000, 0.5, 6.49))
tabular.ade(x_vars='age', xname='Age [y]', rows=c('sex', 'treat'),
rnames=c('Sex', 'Treatment'), cols='dosis', cnames='Dosis', data=d, FUN=n_cell,
type='pct')

##########################################
# 4) Weighted median table
d$w <- runif(5000, 0.1, 5)
d$bmi <- rnorm(5000, 30, 3)
tabular.ade(x_vars=c('age', 'bmi'), xname=c('Age', 'BMI'),
cols=c('sex', 'ALL', 'treat'),
cnames=c('Sex', 'Treatment'), w='w', data=d, FUN=quantile_cell)

##########################################
# 5) Correlation table between age and bmi
tabular.ade(x_vars='age', xname='Age', y_vars='bmi', yname='BMI',
rows=c('dosis'), rnames=c('Dosis'), cols=c('sex', 'treat'),
cnames=c('Sex', 'Treatment'), data=d, FUN=corr_p_cell)

##########################################
# 6) Multiple statistics
tabular.ade(x_vars=c('N', 'MEAN', 'SD', 'SKEW', 'KURT', 'RANGE'),
y_vars=c('age', 'bmi'), yname=c('Age', 'BMI'),
cols=c('sex', 'ALL', 'treat'), cnames=c('Sex', 'Treatment'),
w='w', data=d, FUN=stat_cell)

etable documentation built on May 22, 2021, 5:07 p.m.