EDIT: now with reproducible code/data.
I am trying to run chi-squared tests on multiple variables in my dataframe.
Using the npk
dataset:
A single variable, N
producing the proper result.
npk %>%
group_by(yield, N) %>%
select(yield, N) %>%
table() %>%
print() %>%
chisq.test()
As you can see the output of table()
is in a form that chisq.test()
can utilize.
N
yield 0 1
44.2 1 0
45.5 1 0
46.8 1 0
48.8 1 1
49.5 1 0
49.8 0 1
51.5 1 0
52 0 1
53.2 1 0
55 1 0
55.5 1 0
55.8 0 1
56 2 0
57 0 1
57.2 0 1
58.5 0 1
59 0 1
59.8 0 1
62 0 1
62.8 1 1
69.5 0 1
Pearson's Chi-squared test
data: .
X-squared = 20, df = 20, p-value = 0.4579
When I try and do multiple tests using a loop something about calling on the particular variable changes the output of my table and the chi-squared test cannot run.
Create the list that the loop runs through:
test_ordinal_variables <- noquote(names(npk[2:4]))
test_ordinal_variables
The loop with the errorcode: (1:1 for clarity, error is repeated if you use 1:3)
for (i in 1:1){
npk %>%
group_by(yield, test_ordinal_variables[i]) %>%
select(yield, test_ordinal_variables[i]) %>%
table() %>%
print() %>%
chisq.test()
}
The output clearly showing the table that chisq.test()
cannot interpret:
Adding missing grouping variables: `test_ordinal_variables[i]`
, , N = 0
yield
test_ordinal_variables[i] 44.2 45.5 46.8 48.8 49.5 49.8 51.5 52 53.2 55 55.5 55.8 56 57 57.2 58.5 59 59.8 62
N 1 1 1 1 1 0 1 0 1 1 1 0 2 0 0 0 0 0 0
yield
test_ordinal_variables[i] 62.8 69.5
N 1 0
, , N = 1
yield
test_ordinal_variables[i] 44.2 45.5 46.8 48.8 49.5 49.8 51.5 52 53.2 55 55.5 55.8 56 57 57.2 58.5 59 59.8 62
N 0 0 0 1 0 1 0 1 0 0 0 1 0 1 1 1 1 1 1
yield
test_ordinal_variables[i] 62.8 69.5
N 1 1
For some reason test_ordinal_variables[i]
is not evaluating perfectly to what I would expect when it is in the loop. You can see as the error claimed that it is "Adding missing grouping variables", but if it just evaluated the expression rather than adding a variable then I think it would work.
This evaluates on its own as I would expect.
> test_ordinal_variables[1]
[1] N
So why won't it do the same when it is in the loop?