[R] Help with a problem

Michael Hess mlhess at med.umich.edu
Sat Jul 17 23:38:07 CEST 2010


Sorry for not being clear.

In the dataset there are around 100 or so days of data (in the case also rows of data)

I need to make sure that the person meets that c1 is at least 100 AND c2 is at least 8 for 5 of 7 continuous days.

I will play with what I have and see if I can find out how to do this.

Thanks for the help!

Michael  

>>> Stephan Kolassa  07/17/10 4:50 PM >>>
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.
> 


**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues 



More information about the R-help mailing list