-1

Possible Duplicate:
R filtering out a subset

I have an R dataset. In this dataset, I wish to create a crosstable using the package gmodels for two categorial variables, and then run a chisq.test on them. The two variables are witness and agegroup. witness consists of observations that has value 1,2 and 9. agegroup consists of values 1,2. I wish to exclude values if witness=9, or/and a 3rd variable EMS=2 from the table but I am not sure how to proceed.

library(gmodels)
CrossTable (mydata$witness, mydata$agegroup)
chisq.test (mydata$witness, mydata$agegroup)

...so my question is, how can i do the above with the conditions that witness!=9 and EMS!=2

Community
  • 1
  • 1
SR441
  • 1
  • 1
  • do you mean how do you subset your data? – user1317221_G Sep 23 '12 at 15:41
  • You simply need to subset your data first either using the aptly named `subset()` function in an interactive session, or use the `[` extraction operator. Try [this](http://stackoverflow.com/search?q=[r]+subset) search on SO. – Chase Sep 23 '12 at 15:45
  • thanks guys. wouldn't subsetting create a new dataset, where those observations are deleted? im interested in something along the if/when/where function in SAS, where it is assigned for each task rather than extracting.. – SR441 Sep 23 '12 at 16:21

2 Answers2

1
 data:
    witness agegroup EMS
    1 1 2
    2 2 2
    1 1 2
    2 1 2
    9 2 2
    2 2 2
    1 2 2
    9 2 2
    2 1 2
 #save the data in your current working directory

 data <- read.table("data", header=TRUE, sep = " ")

 data$witness[data$witness == "9"] <- NA

 mydata <- data[!is.na(data$witness),]

 library("gmodels")

 CrossTable(mydata$witness, mydata$agegroup, chisq=TRUE)

You can leave the variable "EMS" in "mydata". It does no harm to your analysis! HTH

Sathish
  • 12,453
  • 3
  • 41
  • 59
  • thanks a lot sathish.. i might not have expressed myself clearly; actually EMS is a variable that has values 1 and 2. I want to exclude the data from the analysis every time EMS has a value of 2 rather than 1 – SR441 Sep 23 '12 at 16:55
1

I expect this question to be closed as it really seems like a duplicate. But as both Chase and I suggested, I think some form of subsetting is the simplest way to go about this, e.g.

mydata[mydata$witness !=9 & mydata$EMS !=2,]
Community
  • 1
  • 1
user1317221_G
  • 15,087
  • 3
  • 52
  • 78