[R] array question on indexing

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Sun May 4 20:23:02 CEST 2003


Troels Ring <tring at gvdnet.dk> writes:

> Dear friends,
> I have struggled to do what is likely rather simple but cannot get it working.
> I have a dataframe with 428 obs. of 5 variables on 26 patients. Two 
> variables are responses, one an index of patients (1:26), one a 
> period-indicator :1 :max 3, and one total time from min 1 to max 36 weeks 
> (max 21 entries). A complete set would be 26*21=546 long but here are only 
> 428 entries.
> Now, within each period nested within patients a max of one of the response 
> variables is found, with length 78, 6 are NA from MAX <- 
> by(response,list(period,id),max).
> Then how do I get the individual times corresponding to these within 
> period, within patient MAX values ?
> (Windows R 1.7)

Let's see...

if you had a data frame of one person in one period, you'd do 

time[which.max(response)]

so use by to split the data frame  and write a little function to
handle each subframe:

timeAtMax <- function(d) with(d, time[which.max(response)])
# or: function(d) d$time[which.max(d$response)])
by(mydata, list(period,id), timeAtMax)

[untested of course, and you might want to do something about the fact
that which.max returns a value even in the precense of NA's]

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907



More information about the R-help mailing list