Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将卡方检验结果添加到每行_R - Fatal编程技术网

R 将卡方检验结果添加到每行

R 将卡方检验结果添加到每行,r,R,我感兴趣的是在每一行上生成一个卡方值(X平方和pvalue),并将测试结果附加到单独的列中。我的数据是每行的一个基因,以及两个不同组的突变或正常(野生型)计数。以下是示例数据集的设置: Genes<-c("GENE_A", "GENE_B","GENE_C") Group1_Mut<-c(20,10,5) Group1_WT<-c(40,50,55) Group2_Mut<-c(10, 30, 10) Group2_WT<-c(80, 60, 80) main<

我感兴趣的是在每一行上生成一个卡方值(X平方和pvalue),并将测试结果附加到单独的列中。我的数据是每行的一个基因,以及两个不同组的突变或正常(野生型)计数。以下是示例数据集的设置:

Genes<-c("GENE_A", "GENE_B","GENE_C")
Group1_Mut<-c(20,10,5)
Group1_WT<-c(40,50,55)
Group2_Mut<-c(10, 30, 10)
Group2_WT<-c(80, 60, 80)
main<-data.frame(Genes,Group1_Mut,Group1_WT,Group2_Mut,Group2_WT)

Genes这里的奇怪之处在于,你不是给矩阵一个向量,而是给它一个数据帧

main[1,2:5]
  Group1_Mut Group1_WT Group2_Mut Group2_WT
1         20        40         10        80
因为矩阵中的每个元素都必须具有相同的类型,所以矩阵元素最终都是列表

m <- matrix(main[1,2:5], nrow=2, byrow = TRUE)

class(m)
"matrix"
typeof(m)
"list"

class(m[1, 1])
"list"

这将产生你想要的。

奇怪的是,你没有给矩阵一个向量,而是给它一个数据帧

main[1,2:5]
  Group1_Mut Group1_WT Group2_Mut Group2_WT
1         20        40         10        80
因为矩阵中的每个元素都必须具有相同的类型,所以矩阵元素最终都是列表

m <- matrix(main[1,2:5], nrow=2, byrow = TRUE)

class(m)
"matrix"
typeof(m)
"list"

class(m[1, 1])
"list"

这将产生您想要的结果。

若要查看错误尝试通信的原因,请将您的数据与chisq.test
期望的数据类型进行比较:

dput(matrix(main[1,2:5,drop=T], nrow=2, 2,2))
# structure(list(20, 10, 40, 80), .Dim = c(2L, 2L))
dput(matrix(1:4, nrow=2, 2,2))
# structure(c(1L, 3L, 2L, 4L), .Dim = c(2L, 2L))
一种补救方法是强制将数据放入
数值
向量:

res <- chisq.test(matrix(as.numeric(main[1,2:5]), nrow=2, 2,2))
res
#   Pearson's Chi-squared test with Yates' continuity correction
# data:  matrix(as.numeric(main[1, 2:5]), nrow = 2, 2, 2)
# X-squared = 9.7656, df = 1, p-value = 0.001778
如果您想将(例如)测试统计数据作为一个数字,您可以执行以下操作:

chisq.statistic <- sapply(seq_len(nrow(main)), function(row) {
  chisq.test(matrix(as.numeric(main[row,2:5]), nrow=2, 2,2))$statistic
})
main$chisq.statistic <- chisq.statistic
main
#    Genes Group1_Mut Group1_WT Group2_Mut Group2_WT chisq.statistic
# 1 GENE_A         20        40         10        80      9.76562500
# 2 GENE_B         10        50         30        60      4.29687500
# 3 GENE_C          5        55         10        80      0.07716049

这个例子展示了一件您可能希望合并到您使用的任何方法中的事情:列的显式命名。也就是说,“2:5”可能会根据您的输入矩阵而变化。

若要查看错误尝试通信的原因,请将您的数据与数据类型进行比较。
chisq.test
期望:

dput(matrix(main[1,2:5,drop=T], nrow=2, 2,2))
# structure(list(20, 10, 40, 80), .Dim = c(2L, 2L))
dput(matrix(1:4, nrow=2, 2,2))
# structure(c(1L, 3L, 2L, 4L), .Dim = c(2L, 2L))
一种补救方法是强制将数据放入
数值
向量:

res <- chisq.test(matrix(as.numeric(main[1,2:5]), nrow=2, 2,2))
res
#   Pearson's Chi-squared test with Yates' continuity correction
# data:  matrix(as.numeric(main[1, 2:5]), nrow = 2, 2, 2)
# X-squared = 9.7656, df = 1, p-value = 0.001778
如果您想将(例如)测试统计数据作为一个数字,您可以执行以下操作:

chisq.statistic <- sapply(seq_len(nrow(main)), function(row) {
  chisq.test(matrix(as.numeric(main[row,2:5]), nrow=2, 2,2))$statistic
})
main$chisq.statistic <- chisq.statistic
main
#    Genes Group1_Mut Group1_WT Group2_Mut Group2_WT chisq.statistic
# 1 GENE_A         20        40         10        80      9.76562500
# 2 GENE_B         10        50         30        60      4.29687500
# 3 GENE_C          5        55         10        80      0.07716049

这个例子展示了一件您可能希望合并到您使用的任何方法中的事情:列的显式命名。也就是说,“2:5”可能会根据您的输入矩阵而变化。

比我快33秒…;-)可能是因为我在一些细节上松懈了。:)比我快33秒…;-)可能是因为我在一些细节上松懈了。:)