0

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!

Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
洪宇亮
  • 1
  • 1

1 Answers1

0

The problem results from how you extract a column of data.fram in a function. You can compare the following two example. The former fails when extracting Species column of iris dataset, but the later works.

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
# ...

Therefore, you need to revise your function like this:

Result<- function(variable, group, database){
  variable <- database[[variable]]
  group <- database[[group]]
  ...
}

And execute it with quoted variable and group arguments.

Result("PCtoopday", "patho", nmlab_PCintendedLC_forpatho)
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
  • thank you for your reply the effect of [[ and $ were the same in data.frame, both of them get a list, right? I've tried it and input the function with " " `variable <- database$variable` `group <- database$group` `variable <- database[[variable]]` `group <- database[[group]]` but the result seemed to be the same `Error in complete.cases(x, g) : no input has determined the number of cases` – 洪宇亮 Jun 10 '20 at 01:24
  • @洪宇亮 The both codes I showed have different results. The `$` symbol doesn't work in a function. How do you execute your function? Do you add quotes around variable and group? I.e. `Result("PCtoopday", "patho", nmlab_PCintendedLC_forpatho)` – Darren Tsai Jun 10 '20 at 06:20