[R] Using functions/loops for repetitive commands
Gerrit Eichner
Gerrit.Eichner at math.uni-giessen.de
Fri May 6 09:11:01 CEST 2011
Hello, Derek,
first of all, be very aware of what David Winsemius said; you are about to
enter the area of "unprincipled data-mining" (as he called it) with its
trap -- one of many -- of multiple testing. So, *if* you know what the
consequences and possible remedies are, a purely R-syntactic "solution" to
your problem might be the (again not fully tested) hack below.
> If so how can I change my code to automate the chisq.test in the same
> way I did for the wilcox.test?
Try
lapply( <your_data_frame>[<selection_of_relevant_components>],
function( y)
chisq.test( y, <your_data_frame>$<group_name>)
)
or even shorter:
lapply( <your_data_frame>[<selection_of_relevant_components>],
chisq.test, <your_data_frame>$<group_name>
)
However, in the resulting output you will not be seeing the names of the
variables that went into the first argument of chisq.test(). This is a
little bit more complicated to resolve:
lapply( names( <your_data_frame>[<selection_of_relevant_components>]),
function( y)
eval( substitute( chisq.test( <your_data_frame>$y0,
<your_data_frame>$tension),
list( y0 = y) ) )
)
Still another possibility is to use xtabs() (with its summary-method)
which has a formula argument.
Hoping that you know what to do with the results -- 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