1

I am running a kruskal.test on some non-normal data with the agricolae package. Some groups have exactly the same value as each other. The kruskal test doesn't handle this well, I receive the error Error in if (s) { : missing value where TRUE/FALSE needed. At first, I thought this was because all the values were 0, but when I make them all the same large number (to test), the same error appears and the function will stop (running function through a loop) and doesn't evaluate anything beyond the first tied variable.

Obviously there is no point running stats on these groups as there will be no difference, but I am using the information generated by agricolae:kruskal to produce a summary table and I need these variables included. I would prefer to keep using this package as it gives me a lot of valuable information. Is there anything I can do to make it run through the tied variables?

dput(example)
structure(list(TREATMENT = c("A", "A", "A", "B", "B", "C", "C", 
"C", "D", "D"), W = c(0, 1.6941524646937, 1.524431531984, 0.959282869723864, 
1.45273122733115, 0, 1.57479386520925, 0.421759202661462, 1.34235435984449, 
1.52131484305823), X = c(0, 0.663872820198758, 0.202935807030853, 
0.836223346381214, 0.750767193777965, 1.18128574225979, 2.03622986392828, 
3.56466682539425, 0.919751117364462, 0.917347336682722), Y = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), Z = c(2.1477548118197, 2.0111754022729, 
3.14642815196242, 4.46967452127494, 1.53715421615569, 2.36274861406182, 
2.33262528044302, 2.50970456594739, 2.96088598025103, 2.22841740590261
)), class = "data.frame", row.names = c(NA, 10L), .Names = c("TREATMENT", 
"W", "X", "Y", "Z"))
library(agricolae)

example<-as.data.frame(example)


for(i in 2:(ncol(example))){
  krusk <- kruskal(example[,i],TREATMENT,group=TRUE)
print(krusk)  
}
UseR10085
  • 7,120
  • 3
  • 24
  • 54
J.Con
  • 4,101
  • 4
  • 36
  • 64
  • Can you please include a reproducible example? http://stackoverflow.com/help/mcve http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Hack-R Jul 04 '16 at 03:57
  • @Hack-R Your wish is my command. – J.Con Jul 04 '16 at 04:10
  • Thanks. It makes it a lot easier to try to help and understand the question. – Hack-R Jul 04 '16 at 04:13

1 Answers1

1
for(i in 2:(ncol(example))){
  if(var(example[,i]) > 0){
  krusk <- kruskal(example[,i],example$TREATMENT,group=TRUE)
  print(krusk)  
  }
}
Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • Woo hoo! Thank you so much. This works! I usually then create a `data frame` and use `rownames(df)<-colnames(example[-1])` to name the rows. Do you know if it is possible to apply the same `IF` condition to this function so that I can exclude the columns that have been excluded in the `kruskal` test? – J.Con Jul 04 '16 at 04:36
  • 1
    @J.Con Well you could add another line in the function that takes an empty object called `tmp` and adds the name of the current column to it (`tmp <- c(colnames(example)[i],tmp)`) then assign that object to the rownames afterwards. – Hack-R Jul 04 '16 at 04:46
  • Got it! `tmp` has to go first. So `tmp<-c(tmp,colnames(example[i]))` – J.Con Jul 04 '16 at 06:27