# [R] code optimization

james.holtman@convergys.com james.holtman at convergys.com
Mon Apr 29 19:32:33 CEST 2002

```Try this:

split(delta,cut(delta, breaks=c(0,2,4,7,10,max(delta)),include.lowest=T))
\$"[0,2]"
 0.0 1.4 2.0 2.0

\$"(2,4]"
 2.3 2.3

\$"(4,7]"
 4.5 6.4

\$"(7,10]"
 8.9 7.4

\$"(10,12.4]"
 12.4 11.5

"Peter B. Mandeville" <mandevip at uaslp.mx>@stat.math.ethz.ch on 06/24/2002
13:11:41

Sent by:  owner-r-help at stat.math.ethz.ch

To:   r-help at stat.math.ethz.ch
cc:
Subject:  [R] code optimization

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._

--

NOTICE:  The information contained in this electronic mail transmission is
intended by Convergys Corporation for the use of the named individual or
entity to which it is directed and may contain information that is
privileged or otherwise confidential.  If you have received this electronic
copying or forwarding it, and notify the sender of the error by reply email
or by telephone (collect), so that the sender's address records can be
corrected.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```