Re: [R] match() function with a little enhancement

2007-02-26 Thread jim holtman
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

[R] match() function with a little enhancement

2007-02-26 Thread Nicolas Prune
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