Re: [S] A subsetting question.

Bendix Carstensen (bxc@svs.dk)
Mon, 27 Apr 1998 14:07:29 +0100


Bill Venables wrote:
>
> Calvin L. Williams writes:
>
> > Suppose the data frame is called "x". Then you would write
> >
> > x[x$visit > 3,]
> >
> > Don't forget the terminal comma.
>
> I don't think this quite does it (even if you correct ">" to ">="
> as I am sure was intended). This selects patients with at least
> three visits, but discards the information on those patients from
> visits 1 and 2. (In the simple example given the patient
> information was constant for all visits, but for the question to
> have any importance this will not always be the case.)
>
> Calvin L. Williams wrote:
> > ... can some one tell me how
> > to subset data of the form
> >
> > patient sex visit
> > 1 1 1
> > 1 1 2
> > ...
> > 1 1 10
> > 2 1 1
> > ...
> > 2 1 5
> > 3 1 1
> > 3 1 2
> > 4 1 1
> > 4 1 2
> > 4 1 3
> > ...
> >
> > ... so that my subset only has those paitients with 3 or more visits?
>
> I think this requires the old `match' trick:
>
> (Warning: untested code.)
>
> three.or.mores <- unique(x[x$visit > 2, "patient"])
> xsub <- x[!is.na(match(x$patient, three.or.mores)), ]
>
> To impress your friends or confound your enemies, you could
> write it in one line, I know.

Bill's solution assumes that patient visits actually are numbered
consecutively in the file.
That kind of assumption is not always advisable in real life...

So what you really would like might be:

xsub <-
x[!is.na(match(patient,names(table(patient))[table(patient)>=3]))]

which will give you patients with more than three RECORDED visits,
freeing you
from assuming any numbering of visits.

-- 
                              \\\|///
                            \\  - -  //
                             (  @ @  )
+--------------------------o00o-(_)-o00o-----------------------+
| Bendix Carstensen                                            |
| Statistician                            tel: +45 35 30 01 00 |
| Danish Zoonosis Centre                  dir: +45 35 30 01 39 |
| Danish Veterinary Laboratory            fax: +45 35 30 01 20 |
| Bülowsvej 27                         e-mail: bxc@svs.dk      |
| DK-1790 København V                                          |
| Denmark                          Ooooo                       |
+-------------------------ooooO----(   )-----------------------+
                          (   )     ) /
                           \ (     (_/
                            \_)
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news