我是 R 的新手,刚开始在 R 中编写函数。我的目的是创建一个函数来改进检查变异的过程并进一步进行 ANOVA 或 Kruskal wallis 检验,还计算显着项目的平均值或中位数。
Result<- function(variable, group, database){
variable <- database$variable
group <- database$group
R1 <- database%>%
bartlett.test(x= variable, g= group)
R1
if(R1>=0.05){ #ANOVA is suitable
z <- aov(variable, group, data=database)
zx<-summarize(z)
zxc<- unlist(zx)['Pr(>F)1']
if(zx<0.05){ #if result of ANOVA smaller than 0.05 then calculate the mean
R2 <- database%>%
group_by(group)%>%
summarize(mean(variable))%>%
print()
R3 <- TukeyHSD(z, "variable")
R3
}
}else{#k-w is suitable
q <- kruskal.test(variable, group, data=database)
qw <- q[["p.value"]]
if(qw<0.05){
R4 <- database %>%
group_by(group) %>%
summarise(mean(variable))%>%
print()
R5 <- dunnTest(variable, group, method="bonferroni", data= database)
R5
}
}
}
将变量放入函数后,出现错误
Error in complete.cases(x, g) : no input has determined the number of cases
12.
complete.cases(x, g)
11.
bartlett.test.default(., x = variable, g = group)
10.
bartlett.test(., x = variable, g = group)
9.
function_list[[k]](value)
8.
withVisible(function_list[[k]](value))
7.
freduce(value, `_function_list`)
6.
`_fseq`(`_lhs`)
5.
eval(quote(`_fseq`(`_lhs`)), env, env)
4.
eval(quote(`_fseq`(`_lhs`)), env, env)
3.
withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
2.
database %>% bartlett.test(x = variable, g = group)
1.
Result(PCtoopday, patho, nmlab_PCintendedLC_forpatho)
我已经查了一段时间了,complete.case() 在我的变量中都是 TRUE 的。我无法弄清楚我的功能有什么问题,也不确定我的功能的其他部分是否可以解决......希望你们能帮助我,谢谢!