[R] Help with a problem

Stephan Kolassa Stephan.Kolassa at gmx.de
Sat Jul 17 22:50:17 CEST 2010


Mike,

I am slightly unclear on what you want to do. Do you want to check rows 
1 and 7 or 1 *to* 7? Should c1 be at least 100 for *any one* or *all* 
rows you are looking at, and same for c2?

You can sort your data like this:
data <- data[order(data$ds),]

Type ?order for help. But also do this for added enlightenment...:

library(fortunes)
fortune("dog")

Next, your analysis on the sorted data frame. As I said, I am not 
entirely clear on what you are looking at, but the following may solve 
your problem with choices "1 to 7" and "any one" above.

foo <- 0
for ( ii in 1:(nrow(data)-8) ) {
   if (any(data$c1[ii+seq(0,6)]>=100) & any(data$c2[ii+seq(0,6)]>=8)) {
     foo <- 1
     break
   }
}

The variable "foo" should contain what you want it to. Look at ?any 
(and, if this does not do what you want it to, at ?all) for further info.

No doubt this could be vectorized, but I think the loop is clear enough.

Good luck!
Stephan



Michael Hess schrieb:
> Hello R users,
> 
> I am a researcher at the University of Michigan looking for a solution to an R problem.  I have loaded my data in from a mysql database and it looks like this
> 
>> data
>            ds c1 c2
> 1  2010-04-03        100           0
> 2  2010-04-30      11141          15
> 3  2010-05-01      3          16
> 4  2010-05-02       7615          14
> 5  2010-05-03       6910          17
> 6  2010-05-04       5035          3
> 7  2010-05-05       3007          15
> 8  2010-05-06       4          14
> 9  2010-05-07       8335          17
> 10 2010-05-08       2897          13
> 11 2010-05-09       6377          17
> 12 2010-05-10       3177          17
> 13 2010-05-11       7946          15
> 14 2010-05-12       8705          0
> 15 2010-05-13       9030          16
> 16 2010-05-14       8682          16
> 17 2010-05-15       8440          15
> 
> 
> What I am trying to do is sort by ds, and take rows 1,7, see if c1 is at least 100 AND c2 is at least 8. If it is not, start with check rows 2,8 and if not there 3,9....until it loops over the entire file.   If it finds a set that matches, set a new variable equal to 1, if never finds a match, set it equal to 0.
> 
> I have done this in stata but on this project we are trying to use R.  Is this something that can be done in R, if so, could someone point me in the correct direction.
> 
> Thanks,
> 
> Michael Hess
> University of Michigan
> Health System
> 
> **********************************************************
> Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues 
> 
> ______________________________________________
> 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