Luedde, Mirko
2010-Nov-25 09:59 UTC
[R] how to find a row index in a matrix or a data frame ?
Dear all, this looks pretty much a standard problem, but I couldn't find a satisfying and understandable solution. (A) Given a data frame (or matrix), e.g. x <- data.frame(A=c(1, 2, 2), B=c(4, 5, 5)) and a row of this data frame, e.g. r <- c(2, 5) I need to find one row index i (or all such indices) such that r is at the i-th row in x, that is, the expression all(x[i,]==as.list(r)) evaluates to TRUE. I can not evaluate an expression like x[x[,1]==2 & x[,2]==5,] because I do not know in advance how many columns x will have. Basically, thus, I'm looking for an equivalent of "vectorfind" in Scilab. (B) Which would be the most appropriate data type for x, matrix or data frame or another type? (C) What will be better, searching for rows or searching for columns? Thank you for your help! Best, Mirko
Eric Lecoutre
2010-Nov-25 10:22 UTC
[R] how to find a row index in a matrix or a data frame ?
Mirko, Here is a solution - I am sure other R mentors would find a more efficient one> x <- data.frame(A=c(1, 2, 2), B=c(4, 5, 5))> x <- rbind(x,x)> r <- c(2,5)>> (rowfind=apply(x,1,FUN=function(row,add){isTRUE(all.equal(row,add,check.attributes= FALSE))},add=r)) 1 2 3 4 5 6 FALSE TRUE TRUE FALSE TRUE TRUE> (rowfindindex=(1:nrow(x))[rowfind])[1] 2 3 5 6 Hope this help, Eric 2010/11/25 Luedde, Mirko <mirko.luedde@sap.com>> Dear all, > > this looks pretty much a standard problem, but I couldn't find a > satisfying and understandable solution. > > (A) Given a data frame (or matrix), e.g. > > x <- data.frame(A=c(1, 2, 2), B=c(4, 5, 5)) > > and a row of this data frame, e.g. > > r <- c(2, 5) > > I need to find one row index i (or all such indices) such that r > is at the i-th row in x, that is, the expression > > all(x[i,]==as.list(r)) > > evaluates to TRUE. I can not evaluate an expression like > > x[x[,1]==2 & x[,2]==5,] > > because I do not know in advance how many columns x will have. > > Basically, thus, I'm looking for an equivalent of "vectorfind" in > Scilab. > > (B) Which would be the most appropriate data type for x, matrix or > data frame or another type? > > (C) What will be better, searching for rows or searching for columns? > > Thank you for your help! > > Best, Mirko > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Eric Lecoutre Consultant - Business & Decision Business Intelligence & Customer Intelligence [[alternative HTML version deleted]]
Henrik Bengtsson
2010-Nov-25 10:24 UTC
[R] how to find a row index in a matrix or a data frame ?
rows <- which(apply(mapply(x, r, FUN="=="), MARGIN=1, FUN=all)); /H On Thu, Nov 25, 2010 at 1:59 AM, Luedde, Mirko <mirko.luedde at sap.com> wrote:> Dear all, > > this looks pretty much a standard problem, but I couldn't find a > satisfying and understandable solution. > > (A) Given a data frame (or matrix), e.g. > > ? ? ?x <- data.frame(A=c(1, 2, 2), B=c(4, 5, 5)) > > ? ?and a row of this data frame, e.g. > > ? ? ?r <- c(2, 5) > > ? ?I need to find one row index i (or all such indices) such that r > ? ?is at the i-th row in x, that is, the expression > > ? ? ?all(x[i,]==as.list(r)) > > ? ?evaluates to TRUE. ?I can not evaluate an expression like > > ? ? ?x[x[,1]==2 & x[,2]==5,] > > ? ?because I do not know in advance how many columns x will have. > > ? ?Basically, thus, I'm looking for an equivalent of "vectorfind" in > ? ?Scilab. > > (B) Which would be the most appropriate data type for x, matrix or > ? ?data frame or another type? > > (C) What will be better, searching for rows or searching for columns? > > Thank you for your help! > > Best, Mirko > > ______________________________________________ > 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. >
Henrique Dallazuanna
2010-Nov-25 11:19 UTC
[R] how to find a row index in a matrix or a data frame ?
Try this: which((apply(x, 1, toString) %in% toString(r))) On Thu, Nov 25, 2010 at 7:59 AM, Luedde, Mirko <mirko.luedde@sap.com> wrote:> Dear all, > > this looks pretty much a standard problem, but I couldn't find a > satisfying and understandable solution. > > (A) Given a data frame (or matrix), e.g. > > x <- data.frame(A=c(1, 2, 2), B=c(4, 5, 5)) > > and a row of this data frame, e.g. > > r <- c(2, 5) > > I need to find one row index i (or all such indices) such that r > is at the i-th row in x, that is, the expression > > all(x[i,]==as.list(r)) > > evaluates to TRUE. I can not evaluate an expression like > > x[x[,1]==2 & x[,2]==5,] > > because I do not know in advance how many columns x will have. > > Basically, thus, I'm looking for an equivalent of "vectorfind" in > Scilab. > > (B) Which would be the most appropriate data type for x, matrix or > data frame or another type? > > (C) What will be better, searching for rows or searching for columns? > > Thank you for your help! > > Best, Mirko > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Gabor Grothendieck
2010-Nov-25 13:07 UTC
[R] how to find a row index in a matrix or a data frame ?
On Thu, Nov 25, 2010 at 4:59 AM, Luedde, Mirko <mirko.luedde at sap.com> wrote:> Dear all, > > this looks pretty much a standard problem, but I couldn't find a > satisfying and understandable solution. > > (A) Given a data frame (or matrix), e.g. > > ? ? ?x <- data.frame(A=c(1, 2, 2), B=c(4, 5, 5)) > > ? ?and a row of this data frame, e.g. > > ? ? ?r <- c(2, 5) > > ? ?I need to find one row index i (or all such indices) such that r > ? ?is at the i-th row in x, that is, the expression > > ? ? ?all(x[i,]==as.list(r)) > > ? ?evaluates to TRUE. ?I can not evaluate an expression like > > ? ? ?x[x[,1]==2 & x[,2]==5,] > > ? ?because I do not know in advance how many columns x will have. > > ? ?Basically, thus, I'm looking for an equivalent of "vectorfind" in > ? ?Scilab. > > (B) Which would be the most appropriate data type for x, matrix or > ? ?data frame or another type? > > (C) What will be better, searching for rows or searching for columns? >Try this: which(apply(t(x) == r, 2, all)) or this: which(colSums((t(x) != r) > 0) == 0) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com