I am a rookie in R, and just start to write function in R. My purpose is to create a function to improve the process of checking variation and further conduct ANOVA or Kruskal wallis test, also calculated the mean or median of significant item.
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
}
}
}
After I put my variables into my function, the error showed up
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)
I've looked it up for a while, the complete.case() is all TRUE in my variables. I can't figure out what is wrong in my function, also not sure about other part of my function can work out...hope you guys can help me, thanks!