I have a question regarding the most efficient way to select a substring of a vector: I have a vector of value v, and I want to select a subspace of this vector called w such that: w=v[1:n] where sum(w) = x I am interested in what you thing would be the most efficient way to do this - I would like to avoid slowing down my simulations as much as possible. Thank you very much for any help that anyone is able to give. [[alternative HTML version deleted]]
Hi Benjamin, There may be faster ways, but v <- 1:100 x <- 10 n <- which(cumsum(v) == x) w <- v[1:n] seems pretty straightforward. Best, Ista On Wed, Mar 2, 2011 at 10:42 AM, Benjamin Hartley <benhartley903 at googlemail.com> wrote:> I have a question regarding the most efficient way to select a substring of > a vector: > > I have a vector of value v, and I want to select a subspace of this vector > called w such that: > > w=v[1:n] > > where > > sum(w) = x > > I am interested in what you thing would be the most efficient way to do this > - I would like to avoid slowing down my simulations as much as possible. > > Thank you very much for any help that anyone is able to give. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Is this what you want? I don't know what your v looks like, but this won't work if there are cases in which v won't sum to exactly x. x <- 20 v <- sample(0:1, 100, T) w <- v[1:which(cumsum(v)==x)] -------------------------------------- Jonathan P. Daily Technician - USGS Leetown Science Center 11649 Leetown Road Kearneysville WV, 25430 (304) 724-4480 "Is the room still a room when its empty? Does the room, the thing itself have purpose? Or do we, what's the word... imbue it." - Jubal Early, Firefly r-help-bounces at r-project.org wrote on 03/02/2011 10:42:12 AM:> [image removed] > > [R] Vector manipulations > > Benjamin Hartley > > to: > > r-help > > 03/02/2011 11:08 AM > > Sent by: > > r-help-bounces at r-project.org > > I have a question regarding the most efficient way to select a substringof> a vector: > > I have a vector of value v, and I want to select a subspace of thisvector> called w such that: > > w=v[1:n] > > where > > sum(w) = x > > I am interested in what you thing would be the most efficient way to dothis> - I would like to avoid slowing down my simulations as much as possible. > > Thank you very much for any help that anyone is able to give. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.