[R] searching for specific row in matrix
Dimitris Rizopoulos
dimitris.rizopoulos at med.kuleuven.be
Wed Jun 11 14:52:14 CEST 2008
try this:
match.pat <- function (mat, target, nomatch = -1) {
f1 <- do.call("paste", c(as.data.frame(mat), sep = "\r"))
f2 <- paste(target, collapse = "\r")
ind <- f1 %in% f2
if (any(ind)) which(ind)[1] else nomatch
}
##################
set.seed(1234)
mat <- matrix(sample(0:1, 50, TRUE), 10, 5)
targ1 <- mat[2, ]
match.pat(mat, targ1)
targ2 <- rep(0, 5)
match.pat(mat, targ2)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Esmail Bonakdarian" <esmail.js at gmail.com>
To: <r-help at r-project.org>
Sent: Wednesday, June 11, 2008 1:58 PM
Subject: [R] searching for specific row in matrix
> Hi,
>
> I have matrix of bits and a target vector. Is there an
> efficient way to search the rows of the matrix for the target?
> I am interested in the first row index where target is found.
>
> Example:
>
> > source("lookup.R")
> [,1] [,2] [,3] [,4] [,5]
> [1,] 1 0 1 1 0
> [2,] 1 1 0 1 0
> [3,] 0 0 1 0 0
> [4,] 1 0 0 1 1
> [5,] 1 0 1 1 1
> [6,] 1 1 0 0 1
> [7,] 1 0 0 1 1
> [8,] 0 0 1 1 1
> [9,] 0 1 1 0 1
> [10,] 0 0 0 1 0
>
> target: 1 1 0 1 1
>
> Should return -1 (or some other indicator) since the
> target was not found in any of the rows.
>
>
>
> > source("lookup.R")
> [,1] [,2] [,3] [,4] [,5]
> [1,] 0 0 1 1 0
> [2,] 1 0 0 0 0
> [3,] 1 0 0 0 0
> [4,] 1 1 0 0 0
> [5,] 1 1 1 0 0
> [6,] 0 0 1 1 0
> [7,] 0 1 1 1 0
> [8,] 0 0 1 1 0
> [9,] 1 1 0 1 1
> [10,] 1 0 1 0 0
>
> target: 1 1 0 1 1
>
> Should return 9 since the target was found in row 9
>
>
> If the target is found, it is no longer necessary to keep
> searching the rest of the matrix (which may be quite large)
>
> The data/size etc may change of course, but target will
> always have the same number of "columns" as the matrix.
>
> I tried variations of "which", and a for loop
> comparing pop[i,] to target without much success, nor
> did google yield any results. I am hoping someone here
> can provide a suggestion.
>
> Thanks,
>
> EB
>
>
> ---------------------------------------------
>
> # Here is the code that generates the above data
>
> create_bin_string <- function(len)
> {
> sample(0:1, len, replace=T)
> }
>
> ROWS = 10
> COLS = 5
> pop = matrix(create_bin_string(ROWS*COLS), ROWS, COLS, byrow=T)
>
>
>
> target=c(1, 1, 0, 1, 1)
>
> # my population
> print(pop)
>
> # I am looking for the index of this in pop
> # if present (else -1?)
> cat("\ntarget: ", target, "\n")
>
>
>
> ##
> ## this is NOT working
> ## plus it would continue to search
> ## after it found the target
> ##
> for(i in ROWS)
> if (pop[i,] == target)
> cat("\nfound in row: ", i, "\n\n")
>
> ______________________________________________
> 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
More information about the R-help
mailing list