Roger Levy
2004-Feb-09 20:22 UTC
[R] simple question on picking out some rows of a matrix/data frame
Hi, I have a simple question about matrix/data frame manipulation. I have a data frame that looks a something like this X Y Z 1 0 "apples" -1 -1 "oranges" ... 0 -1 "bananas" and I'd like to pull out all the rows for which X and Y are (un)equal into a submatrix. How can I do that? Many thanks, Roger Levy
Douglas Bates
2004-Feb-09 21:05 UTC
[R] simple question on picking out some rows of a matrix/data frame
"subset" will do the selection. If you really want a matrix for the result you will need to coerce it using "as.matrix". Roger Levy <rog at stanford.edu> writes:> I have a simple question about matrix/data frame manipulation. I have > a data frame that looks a something like this > > X Y Z > 1 0 "apples" > -1 -1 "oranges" > ... > 0 -1 "bananas" > > and I'd like to pull out all the rows for which X and Y are (un)equal > into a submatrix. > > How can I do that? > > Many thanks, > > Roger Levy
Thomas Lumley
2004-Feb-09 21:25 UTC
[R] simple question on picking out some rows of a matrix/data frame
On Mon, 9 Feb 2004, Roger Levy wrote:> Hi, > > I have a simple question about matrix/data frame manipulation. I have > a data frame that looks a something like this > > X Y Z > 1 0 "apples" > -1 -1 "oranges" > ... > 0 -1 "bananas" > > and I'd like to pull out all the rows for which X and Y are (un)equal > into a submatrix. >subset(the.data.frame, X!=Y) -thomas
John Fox
2004-Feb-09 21:28 UTC
[R] simple question on picking out some rows of a matrix/data frame
Dear Roger, Something like mat[mat[,1] == mat[,2],] or mat[mat[,1] != mat[,2],] should give you what you want. I hope that this helps, John On 09 Feb 2004 12:22:48 -0800 Roger Levy <rog at stanford.edu> wrote:> Hi, > > I have a simple question about matrix/data frame manipulation. I > have > a data frame that looks a something like this > > X Y Z > 1 0 "apples" > -1 -1 "oranges" > ... > 0 -1 "bananas" > > and I'd like to pull out all the rows for which X and Y are (un)equal > into a submatrix. > > How can I do that? > > Many thanks, > > Roger Levy > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
Roger Levy
2004-Feb-09 21:44 UTC
[R] Re: simple question on picking out some rows of a matrix/data frame
Many thanks for the numerous edifying responses! Best Roger
Hadley Wickham
2004-Feb-09 23:26 UTC
[R] simple question on picking out some rows of a matrix/data frame
Have you looked at subset? eg subset(dataframe, x != y, select = c(x, y)) Hadley Roger Levy wrote:> Hi, > > I have a simple question about matrix/data frame manipulation. I have > a data frame that looks a something like this > > X Y Z > 1 0 "apples" > -1 -1 "oranges" > ... > 0 -1 "bananas" > > and I'd like to pull out all the rows for which X and Y are (un)equal > into a submatrix. > > How can I do that? > > Many thanks, > > Roger Levy > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html