try this:
> values <- c("Kiwis", "Bananas", "Ananas",
"Cherries", "Peer")
> vector <- c("Oranges", "Bananas",
"Apples", "Cherries", "Lemons")
> vector <- sort(vector)
> vector
[1] "Apples" "Bananas" "Cherries"
"Lemons" "Oranges"> x <- sapply(values, function(x)ifelse(x<=vector, -1, 1))
> x
Kiwis Bananas Ananas Cherries Peer
[1,] 1 1 -1 1 1
[2,] 1 -1 -1 1 1
[3,] 1 -1 -1 -1 1
[4,] -1 -1 -1 -1 1
[5,] -1 -1 -1 -1 1> vector[apply(x, 2, function(z) which(z < 0)[1])]
[1] "Lemons" "Bananas" "Apples"
"Cherries" NA>
On 2/26/07, Nicolas Prune <np@alambic.org> wrote:>
> Dear R users,
>
> I was wondering if R has a built-in function doing the following :
>
> my_match(values_vector,lookup_vector)
> {
> for each value of values_vector :
>
> if value %in% lookup_vector, then value is unchanged
> else, value is changed the the closest element of lookup_vector,
"closest"
> meaning "the one that would come just after if we sorted them using
> order()"
> }
>
> For example :
>
> values <- c("Kiwis", "Bananas", "Ananas",
"Cherries", "Peer")
> vector <- c("Oranges", "Bananas",
"Apples", "Cherries", "Lemons")
>
> my_match(values, vector) should return :
>
>
c("Lemons","Bananas","Apples","Cherries",NA)
>
> I currently use a home-made function for this, but it is quite slow on
> large
> sets, msotly because I did not manage to avoid using a loop.
>
> Many thanks for your ideas,
> Nicolas
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
[[alternative HTML version deleted]]