Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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_Dataframe_Loops_Chi Squared - Fatal编程技术网

对于R,我希望遍历每一行,并为每一行创建相应的卡方结果

对于R,我希望遍历每一行,并为每一行创建相应的卡方结果,r,dataframe,loops,chi-squared,R,Dataframe,Loops,Chi Squared,例如,我有一个数据集 structure(list(`total primary - yes RS` = c(138L, 101L, 86L, 118L), `total primary - no RS` = c(29L, 39L, 35L, 38L), `total secondary- yes rs` = c(6L, 15L, 3L, 15L), `total secondary- no rs` = c(0L, 7L, 1L, 2L)), row.names = c(NA, -4L),

例如,我有一个数据集

structure(list(`total primary - yes RS` = c(138L, 101L, 86L, 
118L), `total primary - no RS` = c(29L, 39L, 35L, 38L), `total secondary- yes rs` = c(6L, 
15L, 3L, 15L), `total secondary- no rs` = c(0L, 7L, 1L, 2L)), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))
例如,我能够单独运行每一行并获得我想要的信息

Result<-tidy(chisq.test(matrix(unlist(df[1,]), ncol  = 2)))
Result2<-tidy(chisq.test(matrix(unlist(df[2,]), ncol  = 2)))
Result3<-tidy(chisq.test(matrix(unlist(df[3,]), ncol  = 2)))
Result4<-tidy(chisq.test(matrix(unlist(df[4,]), ncol  = 2)))
做了这样的尝试

library(broom)

Results<-for (i in 1:nrow(df)) {
assign(tidy(chisq.test(matrix(unlist(df[1,]), ncol  = 2))))
}
库(扫帚)

结果一个选项是
apply
MARGIN=1
在行上循环。在每一行中,它是一个
向量
,因此我们只需要使用
矩阵
进行包装,将其转换为一个
矩阵
,使用指定的
dim
ension,应用
chisq.test
,并使用
tidy
以TIBLE格式获得输出

library(broom)
library(dplyr)
apply(df, 1, function(x) tidy(chisq.test(matrix(x, ncol = 2)))) %>%
     bind_rows

也可以在
tidyverse
中使用
pmap

library(purrr)
pmap_dfr(df, ~ c(...) %>%
             matrix(ncol = 2) %>%
             chisq.test %>%
             tidy)
-输出

# A tibble: 4 x 4
#  statistic p.value parameter method                                                      
#      <dbl>   <dbl>     <int> <chr>                                                       
#1  3.17e- 1   0.574         1 Pearson's Chi-squared test with Yates' continuity correction
#2  1.66e- 2   0.898         1 Pearson's Chi-squared test with Yates' continuity correction
#3  6.70e-32   1.00          1 Pearson's Chi-squared test with Yates' continuity correction
#4  7.51e- 1   0.386         1 Pearson's Chi-squared test with Yates' continuity correction
#一个tible:4 x 4
#统计p值参数法
#                                                                      
#1 3.17e-1 0.574 1皮尔逊卡方检验与Yates连续性校正
#2 1.66e-2 0.898 1皮尔逊卡方检验和Yates连续性校正
#3 6.70e-32 1.00 1皮尔逊卡方检验和耶茨连续性校正
#4 7.51e-1 0.386 1皮尔逊卡方检验和耶茨连续性校正

太棒了。谢谢。
# A tibble: 4 x 4
#  statistic p.value parameter method                                                      
#      <dbl>   <dbl>     <int> <chr>                                                       
#1  3.17e- 1   0.574         1 Pearson's Chi-squared test with Yates' continuity correction
#2  1.66e- 2   0.898         1 Pearson's Chi-squared test with Yates' continuity correction
#3  6.70e-32   1.00          1 Pearson's Chi-squared test with Yates' continuity correction
#4  7.51e- 1   0.386         1 Pearson's Chi-squared test with Yates' continuity correction