# [R] code optimization

Peter B. Mandeville mandevip at uaslp.mx
Mon Jun 24 19:11:41 CEST 2002

```I have a function "eval.delta" which does what I want but isn't very
elegant. I have consulted the R documents, MASS, and S Programming. Is
there a practical way to optimize the code? Thank you very much.

Peter B.

Function:

eval.delta <- function(delta){
cat("VALUES\n")
vlr <- NULL
k <- 0
for(j in 1:length(delta)) if(delta[j] <= 2){
k <- k+1
vlr[k] <- delta[j]
}
cat("<= 2 (no credible evidence)\t",k,"\t",vlr,"\n")
vlr <- NULL
k <- 0
for(j in 1:length(delta)) if(delta[j]>2 & delta[j] <= 4){
k <- k+1
vlr[k] <- delta[j]
}
cat("> 2 y <= 4 (weak evidence)\t",k,"\t",vlr,"\n")
vlr <- NULL
k <- 0
for(j in 1:length(delta)) if(delta[j]>4 & delta[j] <= 7){
k <- k+1
vlr[k] <- delta[j]
}
cat("> 4 y <= 7 (definite evidence)\t",k,"\t",vlr,"\n")
vlr <- NULL
k <- 0
for(j in 1:length(delta)) if(delta[j]>7 & delta[j] <= 10){
k <- k+1
vlr[k] <- delta[j]
}
cat("> 7 y <= 10 (strong evidence)\t",k,"\t",vlr,"\n")
vlr <- NULL
k <- 0
for(j in 1:length(delta)) if(delta[j]>10){
k <- k+1
vlr[k] <- delta[j]
}
cat("> 10 (very strong evidence)\t",k,"\t",vlr,"\n")
cat("\nMODELS\n")
vlr <- NULL
k <- 0
for(j in 1:length(delta)) if(delta[j] <= 2){
k <- k+1
vlr[k] <- j
}
cat("<= 2 (no credible evidence)\t",k,"\t",vlr,"\n")
vlr <- NULL
k <- 0
for(j in 1:length(delta)) if(delta[j]>2 & delta[j] <= 4){
k <- k+1
vlr[k] <- j
}
cat("> 2 y <= 4 (weak evidence)\t",k,"\t",vlr,"\n")
vlr <- NULL
k <- 0
for(j in 1:length(delta)) if(delta[j]>4 & delta[j] <= 7){
k <- k+1
vlr[k] <- j
}
cat("> 4 y <= 7 (definite evidence)\t",k,"\t",vlr,"\n")
vlr <- NULL
k <- 0
for(j in 1:length(delta)) if(delta[j]>7 & delta[j] <= 10){
k <- k+1
vlr[k] <- j
}
cat("> 7 y <= 10 (strong evidence)\t",k,"\t",vlr,"\n")
vlr <- NULL
k <- 0
for(j in 1:length(delta)) if(delta[j]>10){
k <- k+1
vlr[k] <- j
}
cat("> 10 (very strong evidence)\t",k,"\t",vlr,"\n")
}

Data:

> delta <- c(0,1.4,2.3,4.5,2.3,8.9,12.4,6.4,7.4,11.5,2,2)

Function Call and Output:

> eval.delta(delta)
VALUES
<= 2 (no credible evidence)      4       0 1.4 2 2
> 2 y <= 4 (weak evidence)       2       2.3 2.3
> 4 y <= 7 (definite evidence)   2       4.5 6.4
> 7 y <= 10 (strong evidence)    2       8.9 7.4
> 10 (very strong evidence)      2       12.4 11.5

MODELS
<= 2 (no credible evidence)      4       1 2 11 12
> 2 y <= 4 (weak evidence)       2       3 5
> 4 y <= 7 (definite evidence)   2       4 8
> 7 y <= 10 (strong evidence)    2       6 9
> 10 (very strong evidence)      2       7 10

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```