Hello list,
I read in a txt file using
<B<-read.table(file="data.snp",header=TRUE,row.names=NULL)
by specifying the row.names=NULL so that the rows are numbered.
Below is an example after how the table looks like using
<B[1:10,1:3]
SNP Chromosome PhysicalPosition
1 SNP_A-1909444 1 7924293
2 SNP_A-2237149 1 8173763
3 SNP_A-4303947 1 8191853
4 SNP_A-2236359 1 8323433
5 SNP_A-2205441 1 8393263
6 SNP_A-1909445 1 7924293
7 SNP_A-2237146 2 8173763
8 SNP_A-4303946 2 8191853
9 SNP_A-2236357 2 8323433
10 SNP_A-2205442 2 8393263
I am wondering if there is a way to return the start and end row numbers
for a subset of rows.
For example, If I specify B[,2]=1, I would like to get
start=1 and end=6
if B[,2]=2, then start=7 and end=10
Is there any way in R to quickly do this?
Thanks a bunch!
Allen
Here is a way of doing it using 'rle':> x <- read.table(textConnection(" SNP Chromosome PhysicalPosition+ 1 SNP_A-1909444 1 7924293 + 2 SNP_A-2237149 1 8173763 + 3 SNP_A-4303947 1 8191853 + 4 SNP_A-2236359 1 8323433 + 5 SNP_A-2205441 1 8393263 + 6 SNP_A-1909445 1 7924293 + 7 SNP_A-2237146 2 8173763 + 8 SNP_A-4303946 2 8191853 + 9 SNP_A-2236357 2 8323433 + 10 SNP_A-2205442 2 8393263"), header=TRUE)> # use rle to get the 'runs' > y <- rle(x$Chromosome) > # create dataframe with start/ends and values > start <- head(cumsum(c(1, y$lengths)), -1) > index <- data.frame(values=y$values, start=start, end=start + y$lengths - 1) > > indexvalues start end 1 1 1 6 2 2 7 10>On Nov 14, 2007 10:56 AM, affy snp <affysnp at gmail.com> wrote:> Hello list, > > I read in a txt file using > > <B<-read.table(file="data.snp",header=TRUE,row.names=NULL) > > by specifying the row.names=NULL so that the rows are numbered. > Below is an example after how the table looks like using > <B[1:10,1:3] > > > SNP Chromosome PhysicalPosition > 1 SNP_A-1909444 1 7924293 > 2 SNP_A-2237149 1 8173763 > 3 SNP_A-4303947 1 8191853 > 4 SNP_A-2236359 1 8323433 > 5 SNP_A-2205441 1 8393263 > 6 SNP_A-1909445 1 7924293 > 7 SNP_A-2237146 2 8173763 > 8 SNP_A-4303946 2 8191853 > 9 SNP_A-2236357 2 8323433 > 10 SNP_A-2205442 2 8393263 > > I am wondering if there is a way to return the start and end row numbers > for a subset of rows. > > For example, If I specify B[,2]=1, I would like to get > start=1 and end=6 > > if B[,2]=2, then start=7 and end=10 > > Is there any way in R to quickly do this? > > Thanks a bunch! > > Allen > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
One way to do this is range(which(B[,2]==1)) Julian affy snp wrote:> Hello list, > > I read in a txt file using > > <B<-read.table(file="data.snp",header=TRUE,row.names=NULL) > > by specifying the row.names=NULL so that the rows are numbered. > Below is an example after how the table looks like using > <B[1:10,1:3] > > > SNP Chromosome PhysicalPosition > 1 SNP_A-1909444 1 7924293 > 2 SNP_A-2237149 1 8173763 > 3 SNP_A-4303947 1 8191853 > 4 SNP_A-2236359 1 8323433 > 5 SNP_A-2205441 1 8393263 > 6 SNP_A-1909445 1 7924293 > 7 SNP_A-2237146 2 8173763 > 8 SNP_A-4303946 2 8191853 > 9 SNP_A-2236357 2 8323433 > 10 SNP_A-2205442 2 8393263 > > I am wondering if there is a way to return the start and end row numbers > for a subset of rows. > > For example, If I specify B[,2]=1, I would like to get > start=1 and end=6 > > if B[,2]=2, then start=7 and end=10 > > Is there any way in R to quickly do this? > > Thanks a bunch! > > Allen > > ______________________________________________ > 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.
Thank you very much, Julian. I got it.
Best,
Allen
On Nov 14, 2007 2:38 PM, Julian Burgos <jmburgos at u.washington.edu>
wrote:> One way to do this is
>
> range(which(B[,2]==1))
>
> Julian
>
>
> affy snp wrote:
> > Hello list,
> >
> > I read in a txt file using
> >
> >
<B<-read.table(file="data.snp",header=TRUE,row.names=NULL)
> >
> > by specifying the row.names=NULL so that the rows are numbered.
> > Below is an example after how the table looks like using
> > <B[1:10,1:3]
> >
> >
> > SNP Chromosome PhysicalPosition
> > 1 SNP_A-1909444 1 7924293
> > 2 SNP_A-2237149 1 8173763
> > 3 SNP_A-4303947 1 8191853
> > 4 SNP_A-2236359 1 8323433
> > 5 SNP_A-2205441 1 8393263
> > 6 SNP_A-1909445 1 7924293
> > 7 SNP_A-2237146 2 8173763
> > 8 SNP_A-4303946 2 8191853
> > 9 SNP_A-2236357 2 8323433
> > 10 SNP_A-2205442 2 8393263
> >
> > I am wondering if there is a way to return the start and end row
numbers
> > for a subset of rows.
> >
> > For example, If I specify B[,2]=1, I would like to get
> > start=1 and end=6
> >
> > if B[,2]=2, then start=7 and end=10
> >
> > Is there any way in R to quickly do this?
> >
> > Thanks a bunch!
> >
> > Allen
> >
>
> > ______________________________________________
> > 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.
>
>