[R] code optimization
Ole Christensen
o.christensen at lancaster.ac.uk
Mon Apr 29 17:53:30 CEST 2002
A suggestion :
eval.delta <- function(delta){
cat("VALUES\n")
numbers <- 1:length(delta)
case1 <- numbers[delta <= 2]
case2 <- numbers[delta>2 & delta <= 4]
case3 <- numbers[delta>4 & delta <= 7]
case4 <- numbers[delta>7 & delta <= 10]
case5 <- numbers[delta>10]
cat("<= 2 (no credible
evidence)\t",length(case1),"\t",delta[case1],"\n")
cat("> 2 y <= 4 (weak
evidence)\t",length(case2),"\t",delta[case2],"\n")
cat("> 4 y <= 7 (definite
evidence)\t",length(case3),"\t",delta[case3],"\n")
cat("> 7 y <= 10 (strong
evidence)\t",length(case4),"\t",delta[case4],"\n")
cat("> 10 (very strong
evidence)\t",length(case5),"\t",delta[case5],"\n")
cat("\nMODELS\n")
cat("<= 2 (no credible evidence)\t",length(case1),"\t",case1,"\n")
cat("> 2 y <= 4 (weak evidence)\t",length(case2),"\t",case2,"\n")
cat("> 4 y <= 7 (definite
evidence)\t",length(case3),"\t",case3,"\n")
cat("> 7 y <= 10 (strong
evidence)\t",length(case4),"\t",case4,"\n")
cat("> 10 (very strong evidence)\t",length(case5),"\t",case5,"\n")
}
Ole
"Peter B. Mandeville" wrote:
>
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
--
Ole F. Christensen
Department of Mathematics and Statistics
Fylde College, Lancaster University
Lancaster, LA1 4YF, England
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list