[英]Error in eval(predvars, data, env) : object 'Practice' not found
我有下面的代码,但是在执行FUN
function 时出现错误
library(table1)
library(RVAideMemoire)
Practice<-c(rep("Clippings",14),rep("Irrigation",14),rep("MowHeight",14),rep("SoilTest",14))
Response<-c(rep("No",23),rep("Yes",33))
Student<-c(rep("a",4),rep("b",4),rep("c",4),rep("d",4),rep("e",4),rep("f",4),rep("g",4),rep("h",4),rep("i",4),rep("j",4),rep("k",4),rep("l",4),rep("m",4),rep("n",4))
dataset<-data.frame(Practice,Student,Response)
Fun<-function(data,var,Response,ID) {
tab<-table1(~ Response | var, data=data,topclass="Rtable1-zebra")
tab<-as.data.frame(tab)
test<-cochran.qtest(Response ~ var | ID,data = data)
pvalue<-test$p.value
list<-c(pvalue,tab)
return(list)
}
Fun(dataset,Practice,Response,Student)
我想打印pvalue
和tab
,这是我得到的错误
有没有可能纠正它?
基本 function table
未找到变量,它们必须从数据中提取但不能使用运算符$
,请参阅此 SO 帖子。
Fun <- function(data, var, Response, ID) {
v <- deparse(substitute(var))
r <- deparse(substitute(Response))
tab <- table1(~ Response | var, data=data, topclass = "Rtable1-zebra")
tab <- as.data.frame(tab)
tbl <- table(data[[v]], data[[r]])
test <- chisq.test(tbl)
pvalue <- test$p.value
list(p.value = pvalue, table = tab)
}
Fun(dataset, Practice, Response, Student)
#$p.value
#[1] 2.82287e-09
#
#$table
# Clippings Irrigation MowHeight SoilTest Overall
#1 (N=14) (N=14) (N=14) (N=14) (N=56)
#2 Response
#3 No 14 (100%) 9 (64.3%) 0 (0%) 0 (0%) 23 (41.1%)
#4 Yes 0 (0%) 5 (35.7%) 14 (100%) 14 (100%) 33 (58.9%)
使用新测试, cochran.test
出现错误。
下面更新的 function 运行两个测试,Cochran 的tryCatch
和错误消息是 output。
Fun <- function(data, var, Response, ID) {
v <- deparse(substitute(var))
r <- deparse(substitute(Response))
tab <- table1(~ Response | var, data=data, topclass = "Rtable1-zebra")
tab <- as.data.frame(tab)
tbl <- table(data[[v]], data[[r]])
test1 <- chisq.test(tbl)
test2 <- tryCatch(
cochran.qtest(Response ~ var | ID, data = data),
error = function(e) e
)
pv2 <- if(inherits(test2, "error"))
paste("Error:", conditionMessage(test2))
else test2$p.value
pvalue <- list(test1$p.value, pv2)
pvalue <- setNames(pvalue, c("chisq", "cochran"))
list(p.value = pvalue, table = tab)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.