Hi Sarah, Outstanding! Thanks! I did not notice that numbers of each row of A are different. Actually, I have over 10,000 in B and over 5000 ranges in A. What if I only need to take out all the B rows that fall into the ranges of A? - remove the repetitive results. Best,Jiang > Date: Thu, 3 Jan 2013 17:04:59 -0500 > Subject: Re: [R] Index out SNP position > From: sarah.gos...@gmail.com > To: zhyjiang2...@hotmail.com > CC: r-help@r-project.org > > Assuming I understand what you want, which I'm not certain of, here's > one way; there are more (probably some more elegant). > > I'm not sure how you'd put them in a vector, since there are different > numbers of values for each row of A, so instead I've made a list. > unlist(SNP) will turn it into a vector. > > It's also not consistent which column of A has the higher and lower values. > > SNP <- lapply(seq_len(nrow(A)), function(x)B[B >= min(A[x,]) & B <= > max(A[x,])]) > > > SNP > [[1]] > [1] 36003918 35838399 35838589 > > [[2]] > [1] 35838589 > > [[3]] > numeric(0) > > [[4]] > [1] 36003918 > > [[5]] > numeric(0) > > On Thu, Jan 3, 2013 at 4:54 PM, JiangZhengyu <zhyjiang2...@hotmail.com> 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]}} > > } > > > > -- > Sarah Goslee > http://www.functionaldiversity.org [[alternative HTML version deleted]]
______________________________________________ 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.