[R] How to apply two parameter function in data frame
David L Carlson
dcarlson at tamu.edu
Tue Mar 6 23:26:49 CET 2012
Try this.
> set.seed(123)
> a <- sample(letters[1:4], 250, replace=TRUE)
> b <- sample(letters[1:4], 250, replace=TRUE)
> c <- sample(letters[1:4], 250, replace=TRUE)
> d <- sample(letters[1:4], 250, replace=TRUE)
> e <- sample(letters[1:4], 250, replace=TRUE)
> df <- data.frame(a, b, c, d, e)
> result <- apply(df[,-1], 2, function(x) chisq.test(df$a, x))
> result
$b
Pearson's Chi-squared test
data: df$a and x
X-squared = 8.0032, df = 9, p-value = 0.5338
$c
Pearson's Chi-squared test
data: df$a and x
X-squared = 7.8289, df = 9, p-value = 0.5515
$d
Pearson's Chi-squared test
data: df$a and x
X-squared = 6.1221, df = 9, p-value = 0.7276
$e
Pearson's Chi-squared test
data: df$a and x
X-squared = 6.6181, df = 9, p-value = 0.6768
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of jon waterhouse
Sent: Tuesday, March 06, 2012 2:16 PM
To: r-help at r-project.org
Subject: [R] How to apply two parameter function in data frame
I know this is something simple that I cannot do because I do not yet
"think"
in R.
I have a data frame has a variable participation (a factor), and several
other factors.
I want a chisq test (no contingency tables) for participation vs all of the
other factors.
In SPSS I would do:
CROSSTABS
/TABLES= (my other factors) BY participation
/FORMAT=NOTABLES
/STATISTICS=CHISQ
/COUNT ROUND CELL.
In R I have tried something like:
mapply(chisq.test,participation,surv3[,names(surv3) %in% c('q10','q44')])
which should be effectively the same as
chisq.test(participation,q10)
chisq.test(participation,q44)
except maybe I could store the results better....
but I get told that x and y are not the same length, and in this case,
length() of the y argument is 2, which yes, is not the number of rows in the
data frame.
I'm not sure that it is mapply, rather than lapply that I want
Thanks,
Jon
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-apply-two-parameter-function-in-data-fr
ame-tp4451211p4451211.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list