[R] loop problem

David Winsemius dwinsemius at comcast.net
Sat Oct 3 04:46:54 CEST 2009


On Oct 2, 2009, at 7:34 AM, crenial30 wrote:

>
> Thanks a lot David for your answer. I am sorry for being so minimal.
>
> I wanted to produce a list/vector/table consisting each vector  
> produced from
> this code
>
> len<-20
> for (n1 in seq(0,(len-1),by=1)){
>
> f <- R_event[R_event > (rx[1]+ n1*300)& R_event <= (rx[1] +  
> 300*(n1+1))]
> //create list for each values of n1

That throws an error:
f <- R_event[R_event > (rx[1]+ n1*300)& R_event <= (rx[1] + 300*(n1+1))]
+ //create list for each values of n1
Error: syntax error

Plus... even if it were syntactically correct, we are not going to  
know what R_event is.

Try posting code that creates a data situation and then express in  
English (not mangled R) what you want to happen.


>
> R_ev_char <- as.character(f)
>
> lis01 <- c(length(R_ev_char)) // here I want to store the values of  
> lengths
> of R_ev_char vector for successive values of n1(0,1,2,3,...20)
>
> I already tried with vector notation but no help.
>
> I just want to store Lengths on each successive  integer so that I  
> can draw
> the list on 300(secs) interval.
>
> Any suggestion would be very much helpful.
> crenial
>
>
> 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-tp25665329p25714305.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.




More information about the R-help mailing list