[R] programming advice

Martin Maechler maechler at stat.math.ethz.ch
Fri Jun 23 11:15:29 CEST 2006


>>>>> "Fred" == Fred JEAN <frederic.jean at univ-brest.fr>
>>>>>     on Thu, 22 Jun 2006 17:58:06 +0200 writes:

    Fred> Dear R users
    Fred> I want to compute Kendall's Tau between two vectors x and y.
    Fred> But x and y  may have zeros in the same position(s) and I wrote the
    Fred> following function to be sure to drop out those "double zeros"

    "cor.kendall" <- function(x,y) {
      nox <- c()
      noy <- c()
      #
      for (i in 1:length(x)) if (x[i]!= 0 | y[i] != 0)
	  nox[length(nox)+1]<- x[i]
      for (i in 1:length(y)) if (x[i]!= 0 | y[i] != 0)
	  noy[length(noy)+1]<- y[i]
      #
      res.kendall <- cor.test(nox,noy,method = "kendall",exact=F)
      return(list(x=nox,y=noy,res.kendall,length(nox)))
    }
    
    Fred> Do you know a more elegant way to achieve the same goal ?
    Fred> (I'm sure you do : it's a newbie's program actually)

"Ronggui" already helped you with your main question.

Just a note:  
Why are you making the detour of calling cor.test(.)
when  
      cor(nox, noy,  method = "kendall")    

is probably all you need?



More information about the R-help mailing list