[R] comparison of two vectors

Ben Bolker bolker at ufl.edu
Mon Dec 3 14:04:18 CET 2007




tintin_et_milou wrote:
> 
> Hello,
> 
> I have a vector of two columns like this:
> m/Z                I
> 1000.235       125
> 1000.356       126.5
> ....
> ...
> 
> and a second vector with only one column:
> m/Z 
> 995.547
> 1000.320
> ...
> 
> For each value of the second vector I want to associate the value of the
> intensity of the nearest m/Z associated.
> I can do this with loops (Comparison value by value) but I think that
> maybe a command exist...
> 
> Thanks
> 
> 

## construct sample data
m1 =
  matrix(c(1000.235,1000.356,
    125,126.5),ncol=2,
         dimnames=list(NULL,c("mZ","I")))

y = c(995.547,1000.320)

## compute distances, set diagonal to infinity
d = as.matrix(dist(cbind(m1[,1],y)))
diag(d) <- Inf

## find minimum distances, extract values
cbind(y,m1[apply(d,2,which.min),2])

  Ben Bolker

-- 
View this message in context: http://www.nabble.com/comparison-of-two-vectors-tf4936213.html#a14129523
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list