Thanks for the help Petr it works, and all who contributed managed with the suggested function below as I needed to remove the zeros and the upper triangular portion of the matrix.:
IED <- function(risk) { n <- length(risk) mrisk <- matrix( rep(risk, n), ncol=n, byrow=TRUE ) diff <- abs(risk - mrisk) diff[ lower.tri(diff) ] } OR IED2 <- function(risk){ o <- abs( outer( risk, risk, FUN="-" ) ) o[ lower.tri(o) ] } Oarabile Petr Pikal wrote: >Hi > >or use outer > >x<-1:10 >x<-sample(x) >mat<-outer(x,x,"-") # result with zeroes >matrix(mat[!mat==0],9,9) # get rid of them > >will give you a matrix with columns with intended result. > >HTH >Petr > > > > >On 13 Dec 2005 at 11:53, Robert Burrows wrote: > >Date sent: Tue, 13 Dec 2005 11:53:00 -0500 (EST) >From: Robert Burrows <[EMAIL PROTECTED]> >To: Oarabile Molaodi <[EMAIL PROTECTED]> >Copies to: r-help@stat.math.ethz.ch >Subject: Re: [R] help with writing function > > > >>On Tue, 13 Dec 2005, Oarabile Molaodi wrote: >> >> >> >>>I'm trying to write a function that takes a vector of length n and >>>then takes the first value of the vector i.e j=1 and forms a new >>>vector of length n (i.e replicate the first value n times). This >>>function will then calculate the absoulte difference of the original >>>vector and the new vector and store the results omitting the >>>difference between the value and itself. This function should be >>>able to repeat the procedure for each of the j's i.e j=2 to n. The >>>results should all be stored together. Below is what I've tried so >>>far but it seems to work only for j=1 . >>> >>>Your help will be highly appreciated. >>>IED<-function(risk){ >>>n<-length(risk) >>>i<-c(1:n) >>>Diff<-numeric() >>>for(j in 1:n){ >>>relrisk<-risk >>>relrisk[i]<-relrisk[j] >>>Difference<-abs(risk-relrisk) >>>Difference<-Difference[-c(1:j)] >>>Difference<-append(Diff,Difference) >>>return(Difference) >>>} >>>} >>> >>> >>How about >> >>"IED" <- >>function(risk){ >> n<-length(risk) >>Diff<-numeric(n) >>for(j in 1:n){ >>relrisk<-rep(risk[j],n) >>Diff[j]<-sum(abs(risk-relrisk)[-j]) >>} >>Diff >>} >> >>-- >>Robert Burrows, PhD >>New England Biometrics >>[EMAIL PROTECTED] >> >>______________________________________________ >>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 >> >> > >Petr Pikal >[EMAIL PROTECTED] > > > ______________________________________________ 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