[R] While loop set up

bartjoosen bartjoosen at hotmail.com
Tue Nov 18 14:31:38 CET 2008


Hello Rodrigo,

You're almost there:
you should make the variable distance before the while loop, and this should
be higher than 14 to go inside the while loop:
selectmarkers<- function(n=10){ 
  tapply(mm$marker, mm$chr, function(m){ 
	distances <- 15
   while (max(distances) > 14) { 
     mars<- sort(sample(m, n)) 

    distances<- rep(NA, (length(mars)-1)) 
    for (i in 1:(length(mars)-1)) { 
      distances[i]<- mm[mars[i+1], 'loc'] - mm[mars[i], 'loc'] 
      } #end of for loop 
   } # end of while loop 

    return (mars) 

    }  # end of tapply f(m) 
  ) #tapply 
} # end of selectmarkers 


Also take a look at the diff function, to get rid of the for loop:

selectmarkers2<- function(n=10){ 
  tapply(mm$marker, mm$chr, function(m){ 
	distances <-15
   while (max(distances) > 14) { 
	     mars<- sort(sample(m, n)) 
	    distances <- diff(mm[mars, 'loc']) 
    } # end of while loop 
    return (mars) 

    }  # end of tapply f(m) 
  ) #tapply 
} # end of selectmarkers 

for n = 10 this won't be a significant difference, but check for n=80:
system.time(selectmarkers(80)) #127 seconds at my system
system.time(selectmarkers2(80)) #1.44 seconds at my system

Kind regards

Bart Joosen

Rodrigo Gularte wrote:
> 
> I am attempting to sample 10 markers from each chr, with a maximum
> distance
> of 14, calculated by the location of the marker in each chromosome as
> loc[i+1] - loc[i].  I presume the easiest way to do this is with a while
> loop, so that the function keeps resampling when the max distains is
> greater
> than 14. Example code is below.
> 
> I have gon as far as to select markers and calculate the distances, A
> while
> loop with set with max(distances)>14 should work, but I'm not sure where
> to
> set this.
> Any help would be appreciated.
> 
> Rodrigo
> 
> #some markers, chromosomes and locations
> loc<- rep(1:100, 5)
> marker<- paste("A", seq(1:500), sep="")
> chr<- rep(1:5, rep(100,5))
> mm<- data.frame(marker, chr, loc)
> 
> selectmarkers<-
> function(n=10){
>   tapply(mm$marker, mm$chr, function(m){
> 
> #   while (max(distances) > 14) {
>      mars<- sort(sample(m, n))
> 
>     distances<- rep(NA, (length(mars)-1))
> 
>     for (i in 1:(length(mars)-1)) {
>       distances[i]<- mm[mars[i+1], 'loc'] - mm[mars[i], 'loc']
>       } #end of for loop
> #    } # end of while loop
> 
>     return (mars)
> 
>     }  # end of tapply f(m)
>   ) #tapply
> } # end of selectmarkers
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://www.nabble.com/While-loop-set-up-tp20555712p20559659.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list