
Hi David, Thanks for your reply!  But what if I cannot change the positions of each row pairs in A. Sorry I did not make it very clear. The two columns in A represent start-and-end or end-and-start positions of a gene. The one column in B is the single nucleotide position .  I am trying to index out all the  single nucleotides that fall between the start and end region of a gene. Jiang
  > From: dcarlson@tamu.edu
> To: dcarlson@tamu.edu; zhyjiang2006@hotmail.com; r-help@r-project.org
> CC: sarah.goslee@gmail.com
> Subject: RE: [R] Index out SNP position
> Date: Thu, 3 Jan 2013 16:35:30 -0600
> 
> I missed the fact that the columns are not consistently smaller/larger:
> 
> > A <- t(apply(A, 1, function(x) c(min(x), max(x))))
> > A
>          [,1]     [,2]
> [1,] 35838396 36151202
> [2,] 35838584 35838674
> [3,] 35838674 36003908
> [4,] 36003908 36004090
> [5,] 36003992 36150188
> > indx <- sapply(1:nrow(B), function(i) any(B[i]>A[,1] & B[i]<A[,2]))
> > SNP <- B[indx]
> > SNP
> [1] 36003918 35838399 35838589
> 
> -------
> David
> 
> 
> > -----Original Message-----
> > From: David L Carlson [mailto:dcarlson@tamu.edu]
> > Sent: Thursday, January 03, 2013 4:23 PM
> > To: 'JiangZhengyu'; 'r-help@r-project.org'
> > Subject: RE: [R] Index out SNP position
> > 
> > Something like this?
> > 
> > > indx <- sapply(1:nrow(B), function(i) any(B[i]>A[,1] & B[i]<A[,2]))
> > > SNP <- B[indx]
> > > SNP
> > [1] 36003918 35838399 35838589
> > 
> > ----------------------------------------------
> > David L Carlson
> > Associate Professor of Anthropology
> > Texas A&M University
> > College Station, TX 77843-4352
> > 
> > > -----Original Message-----
> > > From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-
> > > project.org] On Behalf Of JiangZhengyu
> > > Sent: Thursday, January 03, 2013 3:55 PM
> > > To: r-help@r-project.org
> > > Subject: [R] Index out SNP position
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > 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]}}
> > > }
> > >
> > > 	[[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.
> 
 		 	   		  
	[[alternative HTML version deleted]]


