[R] match() function with a little enhancement
Nicolas Prune
np at alambic.org
Mon Feb 26 17:51:52 CET 2007
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
More information about the R-help
mailing list