I am trying to read a csv file and then creating 3 matrices out of each row from the csv file and then apply chi-squared test using the method chisq.test(matrix), but somehow this methods seems to fail.
我正在尝试读取一个csv文件,然后从csv文件中创建3个矩阵,然后使用方法chisq.test(matrix)来应用卡方测试,但是这种方法似乎失败了。
It gives me the following error:
它给了我以下错误:
Error in sum(x) : invalid 'type' (list) of argument
sum(x):无效的“类型”(list)参数。
On the other hand, if I simply create a matrix passing some numbers then it works fine. I also tried running str on two types of matrices.
另一方面,如果我简单地创建一个矩阵传递一些数字,它就能正常工作。我还尝试在两种类型的矩阵上运行str。
-
That I create using the row, from the csv file. str on that gives:
我用row来创建,从csv文件中。str出:
List of 12 $ : int 3 $ : int 7 $ : int 3 $ : int 1 $ : int 7 $ : int 3 $ : int 1 $ : int 1 $ : int 1 $ : int 0 $ : int 2 $ : int 0 - attr(*, "dim")= int [1:2] 4 3
-
Matrix created using some numbers. str on that gives:
用一些数字创建的矩阵。str出:
num [1:2, 1:3] 1 2 3 4 5 6
Can someone please tell me what is going on here? Thanks.
有人能告诉我这是怎么回事吗?谢谢。
1 个解决方案
#1
2
The problems is that your data structure is an array of lists, and for chisq.test() you need an array of numeric values.
问题是,您的数据结构是一个列表数组,对于chisq.test(),您需要一个数值数组。
One solution is to coerce your data into numeric, using as.numeric(). I demonstrate this below. Another solution would be to convert the results of your read.csv() into numeric first before you create the array.
一种解决方案是使用as.numeric()将数据强制转换为数值。下面,我证明这一点。另一个解决方案是在创建数组之前将read.csv()的结果转换为数值。
# Recreate data
x <- structure(array(list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)), dim=c(3,4))
str(x)
List of 12
$ : num 1
$ : num 2
$ : num 3
$ : num 4
$ : num 5
$ : num 6
$ : num 7
$ : num 8
$ : num 9
$ : num 10
$ : num 11
$ : num 12
- attr(*, "dim")= int [1:2] 3 4
# Convert to numeric array
x <- array(as.numeric(x), dim=dim(x))
str(x)
num [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
chisq.test(x)
Pearson's Chi-squared test
data: x
X-squared = 0.6156, df = 6, p-value = 0.9961
Warning message:
In chisq.test(x) : Chi-squared approximation may be incorrect
#1
2
The problems is that your data structure is an array of lists, and for chisq.test() you need an array of numeric values.
问题是,您的数据结构是一个列表数组,对于chisq.test(),您需要一个数值数组。
One solution is to coerce your data into numeric, using as.numeric(). I demonstrate this below. Another solution would be to convert the results of your read.csv() into numeric first before you create the array.
一种解决方案是使用as.numeric()将数据强制转换为数值。下面,我证明这一点。另一个解决方案是在创建数组之前将read.csv()的结果转换为数值。
# Recreate data
x <- structure(array(list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)), dim=c(3,4))
str(x)
List of 12
$ : num 1
$ : num 2
$ : num 3
$ : num 4
$ : num 5
$ : num 6
$ : num 7
$ : num 8
$ : num 9
$ : num 10
$ : num 11
$ : num 12
- attr(*, "dim")= int [1:2] 3 4
# Convert to numeric array
x <- array(as.numeric(x), dim=dim(x))
str(x)
num [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
chisq.test(x)
Pearson's Chi-squared test
data: x
X-squared = 0.6156, df = 6, p-value = 0.9961
Warning message:
In chisq.test(x) : Chi-squared approximation may be incorrect