[R] Subsetting a data frame by a factor, using the level that occurs the most times

Sean Davis sdavis2 at mail.nih.gov
Thu Jan 20 15:16:12 CET 2005


On Jan 20, 2005, at 8:57 AM, michael watson ((IAH-C)) wrote:

> I think that title makes sense... I hope it does...
>
> I have a data frame, one of the columns of which is a factor.  I want
> the rows of data that correspond to the level in that factor which
> occurs the most times.
>
> I can get a list by doing:
>
> by(data,data$pattern,subset)
>

see ?split.

data.split <- split(data,data$pattern)


> And go through each element of the list counting the rows, to find the
> maximum....
>

sort(sapply(data.split,nrow))

> BUT I can't help thinking there's a more elegant way of doing this....

We'll see what the other responses are....

Sean




More information about the R-help mailing list