add.stat: Add a new statistic to an existing table

Description Usage Arguments Details Value See Also Examples

View source: R/add.stat.desc.R

Description

add.stat.desc enables to add a specific quantitative statistic into an existing table of statistics.

Usage

1
2
3
4
5
add.stat(tab, data, func.stat, func.stat.name, pos, ...)

## S3 method for class 'desc'
add.stat(tab, data, func.stat, func.stat.name, pos = NULL,
  ...)

Arguments

tab

A desc object

data

The data frame used to create tab

func.stat

Passed to report.quanti function

func.stat.name

Passed to report.quanti function

pos

Numeric used to specify the position of the new statistics

...

Other parameters

Details

Add a specific statistic in a desc object (only works for quantitative statistics for now). It calls the report.quanti function with the same attributes as the tab argument and uses the regroup function to bind the two results.

Value

A "quanti" desc object (desc$type.desc equal to "quanti")

See Also

regroup report.quanti

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
# Load the data

data(datafake)

# The default statistics are given here:

tab1=report.quanti(data=datafake,y="y_numeric",x1="GROUP",total=TRUE,subjid="SUBJID")

# Define the function corresponding to the coefficient of variation for example

cv=function(y) sd(y,na.rm=TRUE)/mean(y,na.rm=TRUE)

# We use the add.stat function to add CV at the second row:

tab1.cv=add.stat(tab1,datafake,func.stat=cv,func.stat.name="Coef. Var",
pos=2)

tab1.cv

# Same with 2 explicative variables

tab=report.quanti(data=datafake,y="y_numeric",x1="GROUP",
x2="TIMEPOINT",total=TRUE,subjid="SUBJID",
	at.row="TIMEPOINT")

tab=add.stat(tab,datafake,func.stat=cv,func.stat.name="Coef. Var",
pos=2)
tab

# And on position 5, we can add for example the mode

mode=function(x)
{
  x=na.omit(x)
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}


tab=add.stat(tab,datafake,func.stat=mode,func.stat.name="Mode",
pos=5)
tab

ClinReport documentation built on Sept. 3, 2019, 5:07 p.m.