[R] Subsetting a data frame by a factor, using the level that occurs the most times
Chuck Cleland
ccleland at optonline.net
Thu Jan 20 15:08:10 CET 2005
newdata <- subset(mydata, mydata$myfact ==
names(which.max(table(mydata$myfact))))
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)
>
> And go through each element of the list counting the rows, to find the
> maximum....
>
> BUT I can't help thinking there's a more elegant way of doing this....
>
> The second part is figuring out the rows which have the maximum number
> of consecutive patterns which are the same... Now that I would love some
> help with... :-)
>
> Thanks
> Mick
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894
More information about the R-help
mailing list