An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121023/26532480/attachment.pl>
vector indexing
4 messages · Al Ehan, Ivan Calandra, Rui Barradas +1 more
Hi, Is it what you're looking for? which(y>4) ##all indexes for y>4 [1] 3 6 7 9 11 which(y>4)[1] ##the first index [1] 3 HTH, Ivan -- Ivan CALANDRA Universit? de Bourgogne UMR CNRS/uB 6282 Biog?osciences 6 Boulevard Gabriel 21000 Dijon, FRANCE +33(0)3.80.39.63.06 ivan.calandra at u-bourgogne.fr http://biogeosciences.u-bourgogne.fr/calandra Le 23/10/12 11:21, Al Ehan a ?crit :
Hi, I got a small problem on how to define the vector index without manually inspect the vector. example: 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 below #say; 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 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.
Hello, Try the following. y=c(2,3,5,2,4,6,8,3,6,2,5) 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, Rui Barradas Em 23-10-2012 10:21, Al Ehan escreveu:
Hi, I got a small problem on how to define the vector index without manually inspect the vector. example: 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 below #say; 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 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.
On 12-10-23 5:39 AM, Rui Barradas wrote:
Hello, Try the following. y=c(2,3,5,2,4,6,8,3,6,2,5) 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))
Be careful with this: it fails if the condition is FALSE for every element, e.g. > first(y > 10) [1] Inf Warning message: In min(which(x)) : no non-missing arguments to min; returning Inf I don't know if this is possible in the original context, or what the desired result would be if it happens: but it's something to look out for. Duncan Murdoch