-1

I have two vectors, both of which have NA values in them. I am trying to find a Chi-Squared value for a table I created with the two vectors, but I get this error:

Error in chisq.test(data.table) :
all entries of 'x' must be nonnegative and finite

Is there a code to remove the NA values from the table?

I did find some codes to do this for vectors but I am not sure how this would work. If an NA value gets deleted from one vector, will the corresponding value from the other vector not go into the Chi-Squared calculation?

The vectors have over 8,000 values each and each row corresponds to one subject, so if that subject failed to answer a question, I wouldn't want to use his/her other answer either. I hope that makes sense.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    Welcome to StackOverflow! Please take a minute to read about how to provide [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that includes the data and code that generated this error. – Thomas Feb 24 '14 at 01:20

1 Answers1

1

One solution would be to pull out the NA values from your data before you even run the test.

Reproducibility would be helpful here, but I'm guessing your data look something like this:

control<-c(runif(5),NA,runif(4))
treatment<-c(runif(3),NA,runif(6))

In this case, by putting your data into a dataframe, you can both values for every subject with an NA in either value:

df<-data.frame(control,treatment)
df<-df[-which(is.na(df$treatment)),]
df<-df[-which(is.na(df$control)),]

Your data now only includes subjects without any missing data, and can be tested as you please.

Joe
  • 3,831
  • 4
  • 28
  • 44
  • Permanently altering a data object is a great way to dig yourself into a hole. @Thomas's suggestion to supply a working example is more constructive. – IRTFM Feb 24 '14 at 01:46