Waverley @ Palo Alto
2009-Oct-29 05:47 UTC
[R] how to fetch rows with certain characteristics
Hi, I have a matrix, first column is of certain values, second column is the class labels or a factor. e.g. 1.2 1 1.3 1 1.3 1 1.5 1 2.1 2 2.0 2 9.9 2 1.4 3 1.8 3 1.9 3 I want to find out what is the min values of column 1 for each corresponding class (column 2). For the above example, I want to return a matrix of 1.2 1 2.0 2 1.3 3 Can someone suggest how to code for that? The second column can be of characters. Thanks much. -- Waverley @ Palo Alto
Viechtbauer Wolfgang (STAT)
2009-Oct-29 06:56 UTC
[R] how to fetch rows with certain characteristics
tapply(x, y, min) where x is the vector of numbers and y the vector of class labels. Best, -- Wolfgang Viechtbauer http://www.wvbauer.com/ Department of Methodology and Statistics Tel: +31 (0)43 388-2277 School for Public Health and Primary Care Office Location: Maastricht University, P.O. Box 616 Room B2.01 (second floor) 6200 MD Maastricht, The Netherlands Debyeplein 1 (Randwyck) ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Waverley @ Palo Alto [waverley.paloalto at gmail.com] Sent: Thursday, October 29, 2009 6:47 AM To: r-help Subject: [R] how to fetch rows with certain characteristics Hi, I have a matrix, first column is of certain values, second column is the class labels or a factor. e.g. 1.2 1 1.3 1 1.3 1 1.5 1 2.1 2 2.0 2 9.9 2 1.4 3 1.8 3 1.9 3 I want to find out what is the min values of column 1 for each corresponding class (column 2). For the above example, I want to return a matrix of 1.2 1 2.0 2 1.3 3 Can someone suggest how to code for that? The second column can be of characters. Thanks much. -- Waverley @ Palo Alto ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Waverley @ Palo Alto
2009-Oct-29 16:36 UTC
[R] how to fetch rows with certain characteristics
Thanks. That works. However, in my own case, there are more columns of other kinds of data. So to me, it is more important to get the row index of those that has the min values of particular column in particular class (which is another column). Can you help more as how to get those row index? One issue is that for some class they may share the same min value so that using %in% does not work. My goal is to reduce the original matrix size and get the result back in the original matrix format. Thanks. On Wed, Oct 28, 2009 at 11:55 PM, Ista Zahn <istazahn at gmail.com> wrote:> There are various ways, including > > x <- read.table(textConnection("1.2 1 > + 1.3 1 > + 1.3 1 > + 1.5 1 > + 2.1 2 > + 2.0 2 > + 9.9 2 > + 1.4 3 > + 1.8 3 > + 1.9 3") ) > > x <- as.matrix(x) > > x.min <- cbind(tapply(x[,1], x[,2], min), unique(x[,"V2"])) > > Most of that is just formatting it in the way you requested. All you > need to compute the values is > > tapply(x[,1], x[,2], min) > > -Ista > > On Thu, Oct 29, 2009 at 1:47 AM, Waverley @ Palo Alto > <waverley.paloalto at gmail.com> wrote: >> Hi, >> >> I have a matrix, first column is of certain values, second column is >> the class labels or a factor. >> e.g. >> >> 1.2 1 >> 1.3 1 >> 1.3 1 >> 1.5 1 >> 2.1 2 >> 2.0 2 >> 9.9 2 >> 1.4 3 >> 1.8 3 >> 1.9 3 >> >> I want to find out what is the min values of column 1 for each >> corresponding class (column 2). ?For the above example, I want to >> return a matrix of >> 1.2 1 >> 2.0 2 >> 1.3 3 >> >> Can someone suggest how to code for that? ?The second column can be of >> characters. >> >> Thanks much. >> >> >> -- >> Waverley @ Palo Alto >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > > > -- > Ista Zahn > Graduate student > University of Rochester > Department of Clinical and Social Psychology > http://yourpsyche.org >-- Waverley @ Palo Alto