Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops_Try Catch - Fatal编程技术网

R 如何跳过和忽略循环中可以';不能被一行代码读取或提供错误?

R 如何跳过和忽略循环中可以';不能被一行代码读取或提供错误?,r,loops,try-catch,R,Loops,Try Catch,我之前要求提供一行代码,可以为包含的四行中的每一行运行一个CHISQUER structure(list(`total primary - yes RS` = c(0L, 138L, 101L, 86L, 118L), `total primary - no RS` = c(0L, 29L, 39L, 35L, 38L), `total secondary- yes rs` = c(0L, 6L, 15L, 3L, 15L), `total secondary- no rs` = c(0L,

我之前要求提供一行代码,可以为包含的四行中的每一行运行一个CHISQUER

structure(list(`total primary - yes RS` = c(0L, 138L, 101L, 86L, 
118L), `total primary - no RS` = c(0L, 29L, 39L, 35L, 38L), `total secondary- yes rs` = c(0L, 
6L, 15L, 3L, 15L), `total secondary- no rs` = c(0L, 0L, 7L, 1L, 
2L)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
))
虽然脚本有效,但它只起作用,因为四行可以在脚本中运行

https://stackoverflow.com/questions/66750999/with-r-i-would-like-to-loop-through-each-row-and-create-corresponding-chisquare/66751018#66751018
我现在有一行是零,当我运行相同的脚本时,我得到

 library(broom)
 library(dplyr)
 apply(df, 1, function(x) tidy(chisq.test(matrix(x, ncol = 2)))) %>%
 bind_rows
我试图用tryCatch()做一些事情,这样做

 Error in stats::chisq.test(x, y, ...) : 
 at least one entry of 'x' must be positive 

但它没有起作用。最终,数据集有一大堆这样的行,我希望脚本能够识别出它不仅在第1行,而且在多行中,如5、23、67等等。

我不确定我是否完全遵循了您的代码/数据,但是如果您将tryCatch语句移动到apply语句中,比如:
apply(df,2,function(x)会怎么样tryCatch(整洁(chisq.test(矩阵(x,ncol=2 ')))%>%bind_行
?这有帮助吗

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