tabular.ade {etable} | R Documentation |
Tabular representation of a wide selection of statistics
Description
Creates simple to highly customized tables for a wide selection of descriptive statistics, with or without weighting the data.
Usage
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.
|
xname |
Labels for x.
|
y_vars |
This variable can be used to calculate bivariable statistics.
|
yname |
Labels for y.
|
z_vars |
This variable can be used for additional calculations.
|
zname |
Labels for z.
|
rows |
These factors will be used to separate the rows of the table in subgroups.
|
rnames |
Labels for rows.
|
cols |
These factors will be used to separate the columns of the table in subgroups.
|
cnames |
Labels for cols.
|
w |
This numeric variable will be used to weight the table.
|
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.
x, The whole x variable.
y, The whole y variable.
z, The whole z variable.
w, The whole w variable.
cell_ids, Index vector to select values that belong in this cell.
row_ids, Index vector to select values that belong in this row.
col_ids, Index vector to select values that belong in this col.
vnames, A vector of length 3, with labels of variables (x,y,z)
vars, A vector of length 3, with names of variables (x,y,z)
n_min , Min needed N for calculation.
... , additional custom parameters.
For an example with simple mean see below.
Value
A character Matrix.(Table)
Author(s)
Andreas Schulz <ades-s@web.de>
Examples
# 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)