[R] locate nearest value in lookup table

Dylan Beaudette dylan.beaudette at gmail.com
Sun Apr 8 02:14:04 CEST 2007


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("http://169.237.35.250/~dylan/temp/munsell-all.dat",
header=T)

x <- read.csv('http://169.237.35.250/~dylan/temp/data_dump_4-7-2007.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?


thanks,

dylan



More information about the R-help mailing list