Skip to content

comparison of two vectors

5 messages · Ben Bolker, tintin_et_milou, Daniel Stepputtis

#
tintin_et_milou wrote:
## 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
#
Thanks for your help, but there is some more problem. The two vectors have
not the same length so there is a problem with cbind. I give you an example.
My first vector is

 >g[g[,1]>2035 & g[,1]<2050,]
    
     M.Z     Intensity
 2035.836  652.9494
 2035.939  664.5841
 2036.043  696.0554
 2036.146  719.8969
 2036.250  750.7660
 2036.767  816.5243
 2036.870  806.8539
 2036.974  774.2397
 2037.491  777.2780
 2039.147  589.9075
 2042.978  807.7167
 2043.082  820.9365
 2043.289  849.4942
 2043.393  883.8975
 2043.495  900.9681
 2043.600  922.3238
 2043.704  956.5985
 2043.911  978.9377
 2044.015  969.1999

and the second one is:
M.Z      Intensity  Echantillon Position
 1802.809 1064.1210           1       A1
 1865.615 8799.4880           1       A1
 1896.426 1667.5908           1       A1
 2001.064  515.6214           1       A1
 2012.016  837.5599           1       A1
 2021.589 4373.6364           1       A1
 2028.425  832.6896           1       A1
 2036.663    0.0000           1       A1
 2043.497    0.0000           1       A1


The two vectors have not the same number of observations and in fact i would
like to replace all the intensity of zero of the second vector by the value
of the intensity of the nearest m/Z of the first vector.
In this case, the value 816.5243 for the m/Z 2036.663 of the second vector
and 900.9681 for the intensity corresponding at m/Z=2043.497.

Your method give me the intensity of the farest m/Z.

Thanks you again.
Lo?c
Ben Bolker wrote:

  
    
#
tintin_et_milou wrote:
oops -- I think you should get the distance matrix via some form of

distfun <- function(x1,x2) { (x1-x2)^2 }

outer(m1[,1],y,distfun)

  my first answer was simply wrong.
  (I don't have time to test this -- play around to make sure
I put the vectors in the right order.)

  cheers
    Ben
#
Dear Ben,
I was searching for the same problem. Thank you very much, it helped me a lot and I will use it quite often!

In addition to the problem given by tintin_et_milou. I have to compare a two pairs of vectors.

I.e. I have two datasets each with latitude and longitude (which defines the geographical position of data points.)
As you might imagine, it is meaningful to take into account both latitude and longitude, when searching for the nearest data point from the otehr dataset.

Do you have any idea how to do this efficiently (actually I used loops ;-( ?
Best regards
Daniel


Ben Bolker schrieb:
#
Daniel Stepputtis wrote:
Maybe (untested)

  distfun <- function(i,j) { (x1[i]-x2[j])^2 + (y1[i]-y2[j])^2) }
outer(i,j,distfun)

  ?