Garth.Warren at csiro.au
2008-Jun-13 07:14 UTC
[R] Looping, Control Flow & Conditional Statements
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]]
See ?rle Start with this:> a1.runs <- rle( a1 ) > a1.runs$lengths[ a1.runs$values>0 ][1] 3 4>HTH, Chuck p.s.> library(fortunes) > fortune(106)If the answer is parse() you should usually rethink the question. -- Thomas Lumley R-help (February 2005) -- see ?get On Fri, 13 Jun 2008, Garth.Warren at csiro.au wrote:> 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901