[R] Vector manipulations

Ista Zahn izahn at psych.rochester.edu
Wed Mar 2 17:26:57 CET 2011


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



More information about the R-help mailing list