Re: [R] programming advice

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Fri 23 Jun 2006 - 19:15:29 EST

>>>>> "Fred" == Fred JEAN <frederic.jean@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?



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Jun 23 19:20:43 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 23 Jun 2006 - 20:11:42 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.