[R] Looping, Control Flow & Conditional Statements

From: <Garth.Warren_at_csiro.au>
Date: Fri, 13 Jun 2008 17:14:36 +1000


Dear R Group:  

I have little experience using R and even less experience with control flow type questions.  

See the following code:  

a1 = c(0, 1, 1, 1,

0, 0, 0, 0, 0,

0, 0, 1,

1, 1, 1, 0, 0)  

for(i in 1:1){

    sx <- paste("a",i,sep="")

            s <- eval(parse(text = paste("a",i,sep="")))

{g = numeric(length(s))

 k = numeric(length(s))

    {for (i in 1:length(s))

    {for (j in 1:length(s))

        ifelse(((j=i)>1),(g[j] = s[j] + s[i]),(k[j] = s[j] + s[i]))

}}

h1 <- hist(g,freq=TRUE)

h <- h1$counts[4]

cat(sx,":", h,"\n",file = "C:/temp/test-beta.txt", append=TRUE)

}}    

The output is:

> g

 [1] 0 2 2 2 0 0 0 0 0 0 0 2 2 2 2 0 0

> k

 [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

>

> h

[1] 7  

& a text file, which has:

                        a1 : 7

 

k is a by-product of the ifelse statement and is of no interest & g and h only go part-way to answering my question, which is:  

For every time an object i.e. a1 (which is actually a time series) - 0 1 1 1 0 0 0 0 0 0 0 1 1 1 1 0 0 has as value over 0 how long do the values stay above 0. So in this case a1 has two goups or events where the value is above zero, the first event lasts for 3 'days' and the second event lasts for 4 'days'. I have my code telling me that there was a total of 7 'days' in event or above 0, but what I need to know is that there were two 'events' and the 1st lasted 3 'days' and the 2nd lasted '4' days. Essentially I want a text file output to say:  

a1.1 : 3  

a1.2 : 4  

My thinking is that I need to somehow get the code working through each vector one value at a time and when a value is found to meet the critera of > 0 R creates a new vector; to use the above example it would come to the first value >0 and then create the new vector a1.1 = (1,1,1) then as the next value in the series is 0 it would close this new vector 'a1.1'. It would then continue until it reaches the next value >0 and then create the vector a1.2 = (1,1,1,1) then again as the next value in the series is 0 it would close this new vector, and so on.  

Then all I need to do is perform a count of '1's in these new vectors to find how many days they met this criteria of being greater than 0  

I hope the above makes sense and I really hope there is someone willing and able to help. I don't know how to proceed.  

Thanks,

Garth            

        [[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. Received on Fri 13 Jun 2008 - 11:04:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 13 Jun 2008 - 20:31:55 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive