Re: [S] A subsetting question.

Bill Venables (wvenable@attunga.stats.adelaide.edu.au)
Sat, 25 Apr 1998 09:27:57 +0930


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 V.

-- 
Bill Venables, Head, Dept of Statistics,    Tel.: +61 8 8303 5418
University of Adelaide,                     Fax.: +61 8 8303 3696
South AUSTRALIA.     5005.   Email: Bill.Venables@adelaide.edu.au

----------------------------------------------------------------------- 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