[R] help with writing function
Oarabile Molaodi
oarabile at stams.strath.ac.uk
Tue Dec 13 12:59:51 CET 2005
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
More information about the R-help
mailing list