[R] hairy indexing problem

Russell Senior seniorr at aracnet.com
Wed Jun 5 03:00:31 CEST 2002


I've got a data frame that looks like this:

   subject   foo   bar
      2      1.7   3.2
      2      2.3   4.1
      3      7.6   2.3
      3      7.1   3.3
      3      7.3   2.3
      3      7.4   1.3
      5      6.2   6.1
      5      3.4   6.9
     ...

That is, I've got multiple rows per subject.  I need to compute
summaries within categories where the subject has the same number of
rows.  For example, subject 2 and 5 both have two rows.  I need to
compute mean for those four values of foo.  This looks like a good
candidate for index vectors, but I need some help.  I've tried
something like:

  table(data) -> tmp
 
and:

  tmp[tmp == 2]

and even:

  as.numeric(attr(tmp[tmp == 2],"names"))

to get a vector of subject numbers that have two rows in the original
data frame.  But I am getting stuck there.  I want some kind of
"is.member" function to use in a subsequent index vector expression,
like:

  i <- as.numeric(attr(tmp[tmp == 2],"names"))
  data[is.member($subject,i)]$foo

but there isn't an is.member() function.  Can someone please give me a
pointer on the canonical way to do this?

Thanks!

-- 
Russell Senior         ``The two chiefs turned to each other.        
seniorr at aracnet.com      Bellison uncorked a flood of horrible       
                         profanity, which, translated meant, `This is
                         extremely unusual.' ''                      
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list