[R] vector indexing
ruipbarradas at sapo.pt
Tue Oct 23 11:39:35 CEST 2012
Try the following.
first <- function(x) min(which(x))
prefix <- function(x, v) x[seq_len(v)]
suffix <- function(x, v) x[-seq_len(v)]
first(y > 4)
prefix(y, first(y > 4))
suffix(y, first(y > 4))
Hope this helps,
Em 23-10-2012 10:21, Al Ehan escreveu:
> I got a small problem on how to define the vector index without manually
> inspect the vector.
> y=c(2,3,5,2,4,6,8,3,6,2,5) #I have ten set of this kind of vectors (with
> different values but same length) that I would also like to run the routine
> v=the first index in y where the value is larger than 4, in this case index
> 3, value 5
> #what I would like to do is take y[1:v] and run it to some function
> #hence I should also get y[(v+1),length(y)] and can run to other function
> as well..
> I know this is easy peasy for you..please help, many thanks.
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> 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