Dear R users I want to compute Kendall's Tau between two vectors x and y. But x and y may have zeros in the same position(s) and I wrote the 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))) } Do you know a more elegant way to achieve the same goal ? (I'm sure you do : it's a newbie's program actually) -- Fred
Will the following code work for you?> index<-!(x==0 & y==0) > x<-x[index] > y<-y[index]2006/6/22, Fred JEAN <frederic.jean at univ-brest.fr>:> Dear R users > > I want to compute Kendall's Tau between two vectors x and y. > But x and y may have zeros in the same position(s) and I wrote the > 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))) > } > > Do you know a more elegant way to achieve the same goal ? > (I'm sure you do : it's a newbie's program actually) > > -- > Fred > > ______________________________________________ > R-help at 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 >-- ?????? Department of Sociology Fudan University
>>>>> "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?