On Jan 3, 2013, at 1:54 PM, JiangZhengyu wrote: > > > > > > > Dear R experts, > > I have 2 matix: A& B. I am trying to index B against A - (1) find out B rows > that fall between the col 1 and 2 of A& put them into a new vector SNP.I > made code as below, but I cannot think of a right way to do it. Could anyone > help me with the code? Thanks,Jiang---- > > A <- > matrix(c(35838396,35838674,36003908,36004090,36150188,36151202,35838584,35838674,36003908,36003992), > ncol = 2) > B <- matrix(c(36003918,35838399,35838589,36262559),ncol = 1) nr=nrow(A) > rn=nrow(B) for (i in 1:nr) > { > for (j in 1:rn){if (B[i,1]<=A[j,1] && B[i,1]>=A[j,2]){SNP[i]=B[i,1]}} > }
> sapply(B, function(x) apply(A, 1, function(two) x %in% two[1]:two[2])) [,1] [,2] [,3] [,4] [1,] TRUE TRUE TRUE FALSE [2,] FALSE FALSE TRUE FALSE [3,] FALSE FALSE FALSE FALSE [4,] TRUE FALSE FALSE FALSE [5,] FALSE FALSE FALSE FALSE So the first and third B-locations are in the range of two of the rows, the second-B in one range and the third is in none of them. There is also a bioconductor package called `IRanges` that will undoubtedly be more efficient. (This works because the problem is of necessity dealing with integers.) -- David Winsemius Alameda, CA, USA ______________________________________________ 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.