[R] vectorial search for elements with certain attributes

From: <infochat_at_gmx.net>
Date: Thu, 19 May 2011 11:58:17 +0200


I have 2 vectors A and B. For each element of B I'd like to find the index of the next higher or equal element in A. And I'd like to store it effectiv. E.g.: A <- c(1,3,7,8,10)
B <- c(5,8)

result: 3, 4

I have a possibility but for long vectors it works not very effectiv:

ans <- sapply(B, function(x) which.max(A[A < x]) ) as.integer(ans + 1)

Is there anyone how has a better idea?
Thank you for your help,
Thomas.

--

______________________________________________
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.
Received on Thu 19 May 2011 - 17:52:36 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 19 May 2011 - 18:50:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive