[R] unexpected results in comparison (x == y)
Peter Tillmann
peter.tillmann at t-online.de
Wed Nov 4 13:49:34 CET 2009
Dear readers of the list,
I have a problem a comparison of two data from a vector. The comparison
yields FALSE but should be TRUE. I have checked for mode(), length() and
attributes(). See the following code (R2.10.0):
-----------------------------------------------
# data vector of 66 double data
X =
matrix(c(41.41,38.1,39.22,38.1,47.29,46.82,82.46,90.11,45.24,45.74,49.96,53.40,38.20,42.65,45.41,47.92,39.82,42.02,48.17,49.47,39.67,43.89,47.55,50.05,35.75,37.41,46.13,53.64,52.18,56.30,45.15,47.13,41.57,39.08,43.39,44.73,49.38,47.00,45.67,50.53,41.08,44.22,49.28,47.83,49.48,46.04,48.37,47.00,33.96,36.30,49.40,46.44,24.40,24.79,41.55,46.26,37.43,39.88,40.63,38.64,49.92,50.19,47.88,48.61,43.73,44.18),ncol=1)
i = dim(X)
# calculating pairwise differences for each entry in X
Y = matrix(rep(0.0,i[1]*i[1]),ncol=i[1])
for (j in 1:i[1]) {
Y[j,] = X - X[j,]
}
# getting pairwise absolute differences to vector Z and selecting only (xj -
xk), omitting (xk - xj)
# and (xj - xj) i.e. diagonal vector
Z = rep(0, ((i[1]*i[1])-i[1])/2)
nn <- 1
for (j in 1:i[1]) {
for (k in 1:i[1]) {
if (j > k) {
Z[nn] <- abs(Y[j,k])
nn <- nn + 1
}
}
}
nn <- nn - 1
# sorting Z ascending to ZZ, to determine double entries with same
difference
ii <- 0
ZZ <- sort(Z)
# here the problem occurs:
ZZ[1:10]
ZZ[4]
ZZ[5]
ZZ[4] == ZZ[5]
mode(ZZ[4])
mode(ZZ[5])
length(ZZ[4])
length(ZZ[5])
attributes(ZZ[4])
attributes(ZZ[5])
-----------------------------------------------
I get:
> ZZ[1:10]
[1] 0.00 0.00 0.01 0.02 0.02 0.02 0.04 0.04 0.04 0.05
> ZZ[4]
[1] 0.02
> ZZ[5]
[1] 0.02
> ZZ[4] == ZZ[5]
[1] FALSE
> mode(ZZ[4])
[1] "numeric"
> mode(ZZ[5])
[1] "numeric"
> length(ZZ[4])
[1] 1
> length(ZZ[5])
[1] 1
> attributes(ZZ[4])
NULL
> attributes(ZZ[5])
NULL
Which is ok, except for ZZ[4] == ZZ[5].
Can someone please give me an advice where to look?
In real world situations the original vector (X) will contain upto 100
entries.
--
View this message in context: http://old.nabble.com/unexpected-results-in-comparison-%28x-%3D%3D-y%29-tp26195749p26195749.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list