[R] Vector manipulations
Jonathan P Daily
jdaily at usgs.gov
Wed Mar 2 17:33:04 CET 2011
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 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.
More information about the R-help
mailing list