[R] locate nearest value in lookup table

From: Dylan Beaudette <dylan.beaudette_at_gmail.com>
Date: Sun 08 Apr 2007 - 00:14:04 GMT

Hi everyone,

I have a pile of data derived from an analytical device, which reports values as a continuous distribution. I need to associate classes (based on the Munsell color system) using a standard look-up table - the problem is that I would like to find the *closest* matching entry in the lookup table.

I have attempted to do this by first creating as difference vector between color space coordinates associated with a single analytical value and the entire lookup table. Then I found the smallest difference vector, and used that entry in the lookup table. Hoerver, This does not always produce logical results...

here are the steps:

# read in the data

soil <- read.table("", header=T)

x <- read.csv('', header=FALSE)

# extract important variables X,Y,Z

c <- data.frame(id=x$V1, X=x$V2, Y=x$V3, Z=x$V4)

# convert munsell to XYZ

soil$X <- soil$x * (soil$Y/soil$y)
soil$Y <- soil$Y
soil$Z <- (1 - soil$x - soil$y) * (soil$Y / soil$y)

## init some vars and try to find the closest in the table res <- list()
for( i in as.numeric(rownames(c)) )

# compute a difference for each color component
d <- cbind( abs(soil$X - c$X[i]), abs(soil$Y - c$Y[i]), abs(soil$Z - c$Z[i]) )

# find the difference vector
# 3D distance formula

b <- sqrt( sqrt( d[,1]^2 + d[,2]^2) + d[,3]^2)

# get the smallest difference vector

i.closest <- head( soil[order(b), ], 1)

res$input_row[i] <- i
res$H[i] <-  as.vector(unlist(i.closest[1]))
res$V[i] <- as.vector(unlist(i.closest[2]))
res$C[i] <- as.vector(unlist(i.closest[3]))
res$diff_vect[i] <- min(b)


# summarize the conversions

paste(res$input_row, res$H, res$V, res$C)

Does this approach even make sense?



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. Received on Sun Apr 08 10:21:22 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 08 Apr 2007 - 03:31:05 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.