我是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。我无法弄清楚我的功能出了什么问题,也不确定我的功能的其他部分是否可以解决...希望你们能为我提供帮助,谢谢!
data.fram
列而导致的。您可以比较以下两个示例。前者在提取Species
数据集的iris
列时失败,但是后者有效。fun <- function(data, var){
return(data$var)
}
fun(iris, Species)
# NULL
fun <- function(data, var){
return(data[[var]])
}
fun(iris, "Species")
# [1] setosa setosa setosa setosa setosa
# ...
因此,您需要像这样修改功能:
Result<- function(variable, group, database){
variable <- database[[variable]]
group <- database[[group]]
...
}
并使用quoted
variable
和group
自变量执行。Result("PCtoopday", "patho", nmlab_PCintendedLC_forpatho)