George W. Gilchrist
2006-Sep-23 12:44 UTC
[R] Create a vector of indices from a matrix of start and end points
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 at 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/
jim holtman
2006-Sep-23 12:55 UTC
[R] Create a vector of indices from a matrix of start and end points
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 at 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 at 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 at 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?
David Barron
2006-Sep-23 13:18 UTC
[R] Create a vector of indices from a matrix of start and end points
unlist(apply(tmp1,1,function(x) seq(x[1],x[2]))) On 23/09/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. >-- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP [[alternative HTML version deleted]]