[R] loop problem

David Winsemius dwinsemius at comcast.net
Tue Sep 29 18:29:46 CEST 2009


That code is way more than a _minimal_ example, and its not  
_reproducible_ either, so just a comment:

Have you considered creating a vector rather than separate "event"  
variables?

R_ev[1:16] <- R_event > (rx[1] + 300*(0:15)& R_event <= (rx[1] +  
300*(1:16)

?cut   # would also appear to be a function that might offer advantages

-- 
David

On Sep 29, 2009, at 11:34 AM, crenial30 wrote:

>
> I have the following code which I wanted to convert using for loop
>
> previous code:
> R_ev1 <- R_event[R_event > (rx[1] + 300*0)& R_event <= (rx[1] +  
> 300*1)]
> R_ev2 <- R_event[R_event > (rx[1] + 300*1)& R_event <= (rx[1] +  
> 300*2)]
> R_ev3 <- R_event[R_event > (rx[1] + 300*2)& R_event <= (rx[1] +  
> 300*3)]
> R_ev4 <- R_event[R_event > (rx[1] + 300*3)& R_event <= (rx[1] +  
> 300*4)]
>
> R_ev5 <- R_event[R_event > (rx[1] + 300*4)& R_event <= (rx[1] +  
> 300*5)]
> R_ev6 <- R_event[R_event > (rx[1] + 300*5)& R_event <= (rx[1] +  
> 300*6)]
> R_ev7 <- R_event[R_event > (rx[1] + 300*6)& R_event <= (rx[1] +  
> 300*7)]
> R_ev8 <- R_event[R_event > (rx[1] + 300*7)& R_event <= (rx[1] +  
> 300*8)]
>
> R_ev9 <- R_event[R_event  > (rx[1] + 300*8)& R_event <= (rx[1] +  
> 300*9)]
> R_ev10 <- R_event[R_event > (rx[1] + 300*9)& R_event <= (rx[1] +  
> 300*10)]
> R_ev11 <- R_event[R_event > (rx[1] + 300*10)& R_event <= (rx[1] +  
> 300*11)]
> R_ev12 <- R_event[R_event > (rx[1] + 300*11)& R_event <= (rx[1] +  
> 300*12)]
>
> R_ev13 <- R_event[R_event > (rx[1] + 300*12)& R_event <= (rx[1] +  
> 300*13)]
> R_ev14 <- R_event[R_event > (rx[1] + 300*13)& R_event <= (rx[1] +  
> 300*14)]
> R_ev15 <- R_event[R_event > (rx[1] + 300*14)& R_event <= (rx[1] +  
> 300*15)]
> R_ev16 <- R_event[R_event > (rx[1] + 300*15)& R_event <= (rx[1] +  
> 300*16)]
>
>
> R_ev1c <- as.character(R_ev1)
> R_ev2c <- as.character(R_ev2)
> R_ev3c <- as.character(R_ev3)
> R_ev4c <- as.character(R_ev4)
> R_ev5c <- as.character(R_ev5)
> R_ev6c <- as.character(R_ev6)
> R_ev7c <- as.character(R_ev7)
> R_ev8c <- as.character(R_ev8)
>
> R_ev9c  <- as.character(R_ev9)
> R_ev10c <- as.character(R_ev10)
> R_ev11c <- as.character(R_ev11)
> R_ev12c <- as.character(R_ev12)
> R_ev13c <- as.character(R_ev13)
> R_ev14c <- as.character(R_ev14)
> R_ev15c <- as.character(R_ev15)
> R_ev16c <- as.character(R_ev16)
>
>
>
>
> lis1 <- c(
> length(R_ev1c),
> length(R_ev2c),
> length(R_ev3c),
> length(R_ev4c),
> length(R_ev5c),
> length(R_ev6c),
> length(R_ev7c),
> length(R_ev8c),
> length(R_ev9c),
> length(R_ev10c),
> length(R_ev11c),
> length(R_ev12c),
> length(R_ev13c),
> length(R_ev14c),
> length(R_ev15c),
> length(R_ev16c)
> )
>
>
> # R_ev1_mat <- matrix(unlist(R_ev1),nrow=T)
>
>
> barplot(lis1, angle = 15+10*1:5, density = 20, col = "black",
>       legend = rownames(lis1))
> title(main = list("Number of Occurred Received events in every 5  
> mins",
> font = 4))
>
> # dev.off()
>
>
> New code:
>
> f1<- seq(rx[1],rx[2],by=300)
> f<- as.character(f1)
> len <- length(f)
>
> for (n1 in 0:(len-1)){
> for(n2 in 1:len){
> # R_ev_01 <- R_event[R_event <= (rx[1] + 300)]
>
> R_ev[n1] <- R_event[R_event > (rx[1]+ n1*300)& R_event <= (rx[1] +  
> 300*n2)]
> R_ev_char <- as.character(R_ev)
>
> lis01 <- list(length(R_ev_char))
> barplot(lis01, angle = 15+10*1:5, density = 20, col = "blue",
>       legend = rownames(lis01))
> title(main = list("Number of Occurred Received Events in every 5  
> mins",
> font = 4))
> }
> }
>
> Somehow it is not working and giving an error saying "Fehler in  
> -0.01 *
> height : nicht-numerisches Argument für binären Operator".
>
> Any help would be really appreciated.
>
> Thanks
> Crenial
> -- 
> View this message in context: http://www.nabble.com/loop-problem-tp25665329p25665329.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list