[R] Using functions/loops for repetitive commands
Gerrit Eichner
Gerrit.Eichner at math.uni-giessen.de
Thu May 5 13:22:47 CEST 2011
Hello, Derek,
see below.
On Thu, 5 May 2011, dereksloan wrote:
> I still need to do some repetitive statistical analysis on some outcomes
> from a dataset.
>
> Take the following as an example;
>
> id sex hiv age famsize bmi resprate
> 1 M Pos 23 2 16 15
> 2 F Neg 24 5 18 14
> 3 F Pos 56 14 23 24
> 4 F Pos 67 3 33 31
> 5 M Neg 34 2 21 23
>
> I want to know if there are statistically detectable differences in all
> of the continuous variables in my data set when subdivided by sex or hiv
> status (ie are age, family size, bmi and resprate different in my male
> and female patients or in hiv pos/neg patients) Of course I can use
> wilcoxon or t-tests e.g:
>
> wilcox.test( age~sex)
> wilcox.test(famsize~sex)
> wilcox.test(bmi~sex)
> wilcox.test(resprate~sex)
> wilcox.test( age~hiv)
> wilcox.test(famsize~hiv)
> wilcox.test(bmi~hiv)
> wilcox.test(resprate~hiv)
> .... [snip]
Define, e. g.,
my.wilcox.tests <- function( var.names, groupvar.name, data) {
lapply( var.names,
function( v) {
form <- as.formula( paste( v, "~", groupvar.name))
wilcox.test( form, data = data)
} )
}
and call something like
my.wilcox.test( <character vector with relevant variable names>,
<character string with relevant grouping variable>,
data = <your data set as data frame>)
Caveat: untested!
Hth -- Gerrit
---------------------------------------------------------------------
Dr. Gerrit Eichner Mathematical Institute, Room 212
gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/cms/eichner
More information about the R-help
mailing list