user3315563
user3315563

Reputation: 543

Cross-table for subset in R

I have the following data frame (simplified):

IPET Task Type
1      1    1
1      2    2
1      3    1
2      1    1
2      1    2

How can I create a cross table (using the crosstable function in gmodels, because I need to do a chi-square test), but only if Type equals 1.

Upvotes: 2

Views: 1010

Answers (1)

jay.sf
jay.sf

Reputation: 73712

You probably want this.

library(gmodels)
with(df.1[df.1$Type==1, ], CrossTable(IPET, Task))

Yielding

   Cell Contents
|-------------------------|
|                       N |
| Chi-square contribution |
|           N / Row Total |
|           N / Col Total |
|         N / Table Total |
|-------------------------|


Total Observations in Table:  3 


             | Task 
        IPET |         1 |         3 | Row Total | 
-------------|-----------|-----------|-----------|
           1 |         1 |         1 |         2 | 
             |     0.083 |     0.167 |           | 
             |     0.500 |     0.500 |     0.667 | 
             |     0.500 |     1.000 |           | 
             |     0.333 |     0.333 |           | 
-------------|-----------|-----------|-----------|
           2 |         1 |         0 |         1 | 
             |     0.167 |     0.333 |           | 
             |     1.000 |     0.000 |     0.333 | 
             |     0.500 |     0.000 |           | 
             |     0.333 |     0.000 |           | 
-------------|-----------|-----------|-----------|
Column Total |         2 |         1 |         3 | 
             |     0.667 |     0.333 |           | 
-------------|-----------|-----------|-----------|

Data

df.1 <- read.table(header=TRUE, text="IPET Task Type
1      1    1
1      2    2
1      3    1
2      1    1
2      1    2")

Upvotes: 3

Related Questions