1) Please state which software and version you are using.
2) Please give a short example of input and desired output.
3) Please learn to indent and space your codes to make it more readable.
The proposed function can be simplified into
IED <- function(risk) {
n <- length(risk)
mrisk <- matrix( rep(risk, n), ncol=n, byrow=TRUE )
diff <- abs(risk - mrisk)
diff[ lower.tri(diff) ]
}
Or you can further simplify/speed it using outer() function which
calculates the outer product of two arrays
IED2 <- function(risk){
o <- abs( outer( risk, risk, FUN="-" ) )
o[ lower.tri(o) ]
}
Here is a call and quick check that IDE and IDE2 produce same results.
sample.risk <- runif(10)
long <- IED ( sample.risk )
short <- IED2( sample.risk )
identical( long, short )
[1] TRUE
Regards, Adai
On Tue, 2005-12-13 at 15:50 +0000, Oarabile Molaodi wrote:
> Martyn
> Thanks for the help the function works. I used lower.tri() in
> package(ape) to extract the Lower triangular portion of the matrix since
> thats what I needed the func looks like this
> Thanks to all who contributed-the function works.
>
> IED <- function(risk) {
> n <- length(risk)
> mrisk <- matrix(rep(risk,n),ncol=n,byrow=T)
> diff <- abs(risk - mrisk)
> library(base)
> keep <- lower.tri(diff)
> c(diff)[c(keep)]
> }
>
> Thanks
> Oarabile
>
>
> Martyn Byng wrote:
>
> > Hi,
> >
> > I am assuming this is in either R or SPlus ?
> >
> > In both of these packages it is best to avoid doing loops where
> > possible, something along the lines of
> >
> > IED <- function(risk) {
> > n <- length(risk)
> > mrisk <- matrix(rep(risk,n),ncol=n,byrow=T)
> > diff <- abs(risk - mrisk)
> > keep <- matrix(T,nrow=n,ncol=n)
> > diag(keep) <- F
> > c(diff)[c(keep)]
> > }
> >
> > seems to work.
> >
> > In your code I think it is the line
> >
> > Difference<-Difference[-c(1:j)]
> >
> > that is at fault, this drops the first j elements from Difference,
> > which is Difference[j] - Difference[j] only for j = 1
> >
> > Hope that helps
> >
> > Martyn
> >
> > 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)
> >>> }
> >>> }
> >>>
> >>>
> >>> Oarabile
> >>>
> >>
> >> ________________________________________________________________________
> >> This e-mail has been scanned for all viruses by Star. The
> >> service is powered by MessageLabs. For more information on a proactive
> >> anti-virus service working around the clock, around the globe, visit:
> >> http://www.star.net.uk
> >> ________________________________________________________________________
> >>
> >
> > ________________________________________________________________________
> > This e-mail has been scanned for all viruses by Star. The
> > service is powered by MessageLabs. For more information on a proactive
> > anti-virus service working around the clock, around the globe, visit:
> > http://www.star.net.uk
> > ________________________________________________________________________
>
|