Re: [R] how to fetch rows with certain characteristics
Try > x <- read.table(textConnection( + "1.2 1 + 1.2 1 + 1.3 1 + 1.5 1 + 1.1 2 + 1.2 2 + 9.9 2 + 0.1 3 + 1.1 3 + 1.9 3") ) > x.min <- tapply(x[,1], x[,2], min) > x[x.min[x[,2]]==x[,1],] V1 V2 [1,] 1.2 1 [2,] 1.2 1 [3,] 1.1 2 [4,] 0.1 3 2009/10/29 Ista Zahn > I still don't understand. Please look at the example I provided in my > last response. What is it that you want that my example does not do? > > On Thu, Oct 29, 2009 at 3:08 PM, Waverley @ Palo Alto > wrote: > > The reason %in% does not work is that there are might be values which > > are not min in other class which are the same as the min of different > > classes. In the example I provided before, this situation did not > > exist. See the new example: > > > >> + 1.2 1 > >> + 1.3 1 > >> + 1.5 1 > >> + 1.1 2 > >> + 1.2 2 > >> + 9.9 2 > >> + 0.1 3 > >> + 1.1 3 > >> + 1.9 3 > > > > if you are using %in%, then > > 1.2 2 > > 1.1 3 > > will also show up in the final result. > > > > > > That is why I need those row index of the min value of each class. If > > I can get those, that would be best. > > Thanks. > > > > On Thu, Oct 29, 2009 at 11:58 AM, Ista Zahn wrote: > >> Hi, > >> I guess I don't understand why you think %in% won't work. > >> > >>> x <- read.table(textConnection("1.2 1 > >> + 1.2 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 <- tapply(x[,1], x[,2], min) > >>> x[x[,1] %in% x.min,] > >>> ## all matches > >> V1 V2 > >> [1,] 1.2 1 > >> [2,] 1.2 1 > >> [3,] 2.0 2 > >> [4,] 1.4 3 > >>> ## unique matches > >>> unique(x[x[,1] %in% x.min,]) > >> V1 V2 > >> [1,] 1.2 1 > >> [2,] 2.0 2 > >> [3,] 1.4 3 > >> > >> -Ista > >> On Thu, Oct 29, 2009 at 12:36 PM, Waverley @ Palo Alto > >> wrote: > >>> 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 > 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 > 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@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 > >>> > >> > >> > >> > >> -- > >> Ista Zahn > >> Graduate student > >> University of Rochester > >> Department of Clinical and Social Psychology > >> http://yourpsyche.org > >> > > > > > > > > -- > > Waverley @ Palo Alto > > > > > > -- > Ista Zahn > Graduate student > University of Rochester > Department of Clinical and Social Psychology > http://yourpsyche.org > > __ > R-help@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
Re: [R] how to fetch rows with certain characteristics
I still don't understand. Please look at the example I provided in my last response. What is it that you want that my example does not do? On Thu, Oct 29, 2009 at 3:08 PM, Waverley @ Palo Alto wrote: > The reason %in% does not work is that there are might be values which > are not min in other class which are the same as the min of different > classes. In the example I provided before, this situation did not > exist. See the new example: > >> + 1.2 1 >> + 1.3 1 >> + 1.5 1 >> + 1.1 2 >> + 1.2 2 >> + 9.9 2 >> + 0.1 3 >> + 1.1 3 >> + 1.9 3 > > if you are using %in%, then > 1.2 2 > 1.1 3 > will also show up in the final result. > > > That is why I need those row index of the min value of each class. If > I can get those, that would be best. > Thanks. > > On Thu, Oct 29, 2009 at 11:58 AM, Ista Zahn wrote: >> Hi, >> I guess I don't understand why you think %in% won't work. >> >>> x <- read.table(textConnection("1.2 1 >> + 1.2 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 <- tapply(x[,1], x[,2], min) >>> x[x[,1] %in% x.min,] >>> ## all matches >> V1 V2 >> [1,] 1.2 1 >> [2,] 1.2 1 >> [3,] 2.0 2 >> [4,] 1.4 3 >>> ## unique matches >>> unique(x[x[,1] %in% x.min,]) >> V1 V2 >> [1,] 1.2 1 >> [2,] 2.0 2 >> [3,] 1.4 3 >> >> -Ista >> On Thu, Oct 29, 2009 at 12:36 PM, Waverley @ Palo Alto >> wrote: >>> 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 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 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@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 >>> >> >> >> >> -- >> Ista Zahn >> Graduate student >> University of Rochester >> Department of Clinical and Social Psychology >> http://yourpsyche.org >> > > > > -- > Waverley @ Palo Alto > -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org __ R-help@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.
Re: [R] how to fetch rows with certain characteristics
The reason %in% does not work is that there are might be values which are not min in other class which are the same as the min of different classes. In the example I provided before, this situation did not exist. See the new example: > + 1.2 1 > + 1.3 1 > + 1.5 1 > + 1.1 2 > + 1.2 2 > + 9.9 2 > + 0.1 3 > + 1.1 3 > + 1.9 3 if you are using %in%, then 1.2 2 1.1 3 will also show up in the final result. That is why I need those row index of the min value of each class. If I can get those, that would be best. Thanks. On Thu, Oct 29, 2009 at 11:58 AM, Ista Zahn wrote: > Hi, > I guess I don't understand why you think %in% won't work. > >> x <- read.table(textConnection("1.2 1 > + 1.2 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 <- tapply(x[,1], x[,2], min) >> x[x[,1] %in% x.min,] >> ## all matches > V1 V2 > [1,] 1.2 1 > [2,] 1.2 1 > [3,] 2.0 2 > [4,] 1.4 3 >> ## unique matches >> unique(x[x[,1] %in% x.min,]) > V1 V2 > [1,] 1.2 1 > [2,] 2.0 2 > [3,] 1.4 3 > > -Ista > On Thu, Oct 29, 2009 at 12:36 PM, Waverley @ Palo Alto > wrote: >> 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 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 >>> 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@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 >> > > > > -- > Ista Zahn > Graduate student > University of Rochester > Department of Clinical and Social Psychology > http://yourpsyche.org > -- Waverley @ Palo Alto __ R-help@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.
Re: [R] how to fetch rows with certain characteristics
Hi, I guess I don't understand why you think %in% won't work. > x <- read.table(textConnection("1.2 1 + 1.2 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 <- tapply(x[,1], x[,2], min) > x[x[,1] %in% x.min,] > ## all matches V1 V2 [1,] 1.2 1 [2,] 1.2 1 [3,] 2.0 2 [4,] 1.4 3 > ## unique matches > unique(x[x[,1] %in% x.min,]) V1 V2 [1,] 1.2 1 [2,] 2.0 2 [3,] 1.4 3 -Ista On Thu, Oct 29, 2009 at 12:36 PM, Waverley @ Palo Alto wrote: > 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 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 >> 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@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 > -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org __ R-help@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.
Re: [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 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 > 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@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 __ R-help@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.
Re: [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 Viechtbauerhttp://www.wvbauer.com/ Department of Methodology and StatisticsTel: +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-boun...@r-project.org [r-help-boun...@r-project.org] On Behalf Of Waverley @ Palo Alto [waverley.paloa...@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@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. __ R-help@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.
[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@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.