Re: [R] Create a vector of indices from a matrix of start and end points

From: jim holtman <jholtman_at_gmail.com>
Date: Sat 23 Sep 2006 - 12:55:05 GMT

try this:

> x <- rbind(c(2,5), c(7,9), c(15,20))
> x

     [,1] [,2]

[1,]    2    5
[2,]    7    9
[3,]   15   20

> unlist(mapply(seq, x[,1], x[,2]))

 [1] 2 3 4 5 7 8 9 15 16 17 18 19 20
>

On 9/23/06, George W. Gilchrist <gwgilc@wm.edu> wrote:
> I have a very large dataframe and wish to extract a subset of rows. I
> have a two column matrix listing the starting and ending indices of
> one subset on each row. My idea is to create a vector of indices that
> could be applied to the dataframe and I have a solution using a for
> loop (below). But surely there is some more elegant way to do this! I
> looked thorough the archives without success. Thanks for any ideas.
>
> > tmp1 <- matrix(c(2,5,7,9,15,20), 3,2, byrow=T)
> > tmp1
> [,1] [,2]
> [1,] 2 5
> [2,] 7 9
> [3,] 15 20
> > t.ind <- NULL
> > for (i in 1:3) t.ind <- c(t.ind, seq(tmp1[i,1], tmp1[i,2]))
> > t.ind
> [1] 2 3 4 5 7 8 9 15 16 17 18 19 20
> >
>
> cheers, George
>
> ..................................................................
> George W. Gilchrist Email: gwgilc@wm.edu
> Director of Graduate Studies Phone: (757) 221-7751
> Department of Biology, Box 8795 Fax: (757) 221-6483
> College of William & Mary
> Williamsburg, VA 23187-8795
> http://gwgilc.people.wm.edu/
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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?

______________________________________________
R-help@stat.math.ethz.ch 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.
Received on Sat Sep 23 22:59:29 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 23 Sep 2006 - 13:30:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.