0

I have a dataframe with following data:

age.cat ds.cat  finalstate
<65     AGN     dead
<65     AGN     alive
<65     PC      alive
65+     AMI     alive
65+     PC      dead
<65     AMI     alive

For every level of ds.cat factor variable I need to perform Fisher test for age.cat and finalstate variables.

I tried to do it by several ways but was not received what I need. The following R code doesn't produce desired result:

library(descr)
# 1 syntax - doesn't work
by(split(df, df$ds.cat), CrossTable(age.cat, finalstate, prop.chisq = FALSE, fisher = TRUE))
# 2 syntax - doesn't work
sapply( split(df, df$ds.cat), function(x) CrossTable(age.cat, finalstate, prop.chisq = FALSE, fisher = TRUE) )
# 3 syntax - doesn't work
for(i in 1:nlevels(ds.cat)){
    curcat <- subset(df, ds.cat == ds.cat[i])
    CrossTable(age.cat[ds.cat == ds.cat[i]], finalstate[ds.cat == ds.cat[i]], prop.chisq = FALSE, fisher = TRUE, data = curcat)
}

Please could anyone suggest how to perform Fisher test for age.cat and finalstate variables for every level of ds.cat factor variable?

Resolved!

Thank you so much for all of you!

This code with CrossTable produces an error

Error in chisq.test(t, correct = FALSE) : 
'x' must at least have 2 elements

But thanks for @BenBolker the base fisher.test function works well in your code

l <- split(arf.mort, arf.mort$ds.cat)
lapply(l, function(x) fisher.test(x$age.cat, x$finalstate))

as well as works well

by(arf.mort, arf.mort$ds.cat, function(x){ fisher.test(x$age.cat, x$finalstate) })
boris
  • 13
  • 5
  • 2
    reproducible example? why are you using `CrossTable` (from some unknown package) rather than `fisher.test` from base R ? – Ben Bolker Mar 06 '13 at 17:22
  • 1
    @BenBolker, the package seems to be `gmodels` and they've credited *you* (or some other Ben Bolker, that must be an awesome coincidence) in their manual [**here**](http://cran.r-project.org/web/packages/gmodels/gmodels.pdf) – Arun Mar 06 '13 at 18:09
  • Sorry that I didn't mentioned the package. It is descr library. Added to the question this important issue. – boris Mar 06 '13 at 19:12

2 Answers2

0

You should mention the package you're using (gmodels). We don't know every package that have been developed. This seems to work and I think is what you're looking for.

require(gmodels)
l <- split(df, df$ds.cat)
lapply(l, function(x) CrossTable(x$age.cat, x$finalstate, 
            prop.chisq = FALSE, fisher = TRUE)

It gives a whole bunch of tables (I guess its similar to S-plus format).

Arun
  • 116,683
  • 26
  • 284
  • 387
  • Thank you so much! The code with `CrossTable` produces an error, while `fisher.test` function works well. Put the solution to the answer. – boris Mar 06 '13 at 19:30
0

Lets say you're working with the iris dataset(it's loaded when you start R), and you wanted to take the CrossTable of the first 2 columns, split by the Species. This is how you do it:

#whatever a crosstable is
CrossTable <- function(x, y){
    #something tricky
    sum(x+y)
}

by(iris, iris$Species, function(x){ CrossTable(x[1], x[2]) })
kith
  • 5,486
  • 1
  • 21
  • 21
  • Thank you so much! The code with `CrossTable` produces an error, while `fisher.test` function works well. Put the solution to the answer. – boris Mar 06 '13 at 19:30