mecp
mecp

Reputation: 1

Error in sum(x) : invalid 'type' (character) of argument when running chisq.test

I am importing an excel file and trying to run chisq.test on it but keep getting the following error: Error in sum(x) : invalid 'type' (character) of argument Below is what it looks like in the console. Can anyone tell me what I need to do to fix this error?

M <- read_excel("~/Desktop/M1.xlsx")
M
# A tibble: 2 × 3
  
Location          S     R
  
<chr>           <dbl> <dbl>
1 ShadysidePark    37    11
2 SilverCreek      37    11
chisq.test(M, correct=T)
Error in sum(x) : invalid 'type' (character) of argument



I assume it may have something to do with the <chr> or <dbl> labels

M <- read_excel("~/Desktop/M1.xlsx")
M
# A tibble: 2 × 3
  
Location          S     R
  
<chr>           <dbl> <dbl>
1 ShadysidePark    37    11
2 SilverCreek      37    11
chisq.test(M, correct=T)
Error in sum(x) : invalid 'type' (character) of argument

Upvotes: 0

Views: 267

Answers (1)

akrun
akrun

Reputation: 887221

The first column is character. We may remove that column and do the test

chisq.test(M[-1], correct = TRUE)

According to ?chisq.test

x- a numeric vector or matrix. x and y can also both be factors.

Here, the data showed is a tibble with first column as character. We remove the first column and apply the chisq.test. Although, it is mentioned as matrix, it still works with tibble

Upvotes: 1

Related Questions