[R-sig-Geo] matching does not work in SpatialPointsDataFrame
Jean-Luc Dupouey
dupouey at nancy.inra.fr
Wed Jan 21 22:45:53 CET 2015
I get an incorrect match between row names of coordinates and data in
SpatialPointsDataFrame:
> Mycoord <- data.frame(row.names=letters[1:5], x=rnorm(5), y=rnorm(5))
>
> Mycoord
x y
a -0.80138451 1.9291882
b -1.58229979 -0.7713053
c -1.28416923 1.3324122
d -0.22558639 0.7320874
e -0.08702876 1.7635046
>
> Myattr <- data.frame(row.names=sample(letters[1:5]),var1=rnorm(5))
>
> Myattr
var1
d 1.2935138
c 0.1143173
a -0.2157230
b -1.9424786
e -0.7234636
>
> MyptsdataT <- SpatialPointsDataFrame(Mycoord, Myattr, match.ID=TRUE)
>
> MyptsdataT
coordinates var1
d (-0.8013845, 1.929188) 1.2935138
c (-1.5823, -0.7713053) 0.1143173
a (-1.284169, 1.332412) -0.2157230
b (-0.2255864, 0.7320874) -1.9424786
e (-0.08702876, 1.763505) -0.7234636
Either I made a mistake somewhere (99.99%), either there is a bug (0.01%).
I read carefully and several times the help page for this function, and
did not find any clue to my problem. It is written: "if (match.ID=)TRUE
AND coords has rownames (i.e., coerced to a matrix,
dimnames(coords)[[2]] is not NULL), AND data has row.names (i.e. is a
data.frame), then the SpatialPointsDataFrame object is formed by
matching the row names of both components".
It seems to me that my coordinates, data and parameters fulfill these
requirements (even if these requirements look a bit strange: shouldn't
it be dimnames(coords)[[1]] which must not be NULL?).
A quick look at the function SpatialPointsDataFrame suggests me that the
problem is perhaps coming from the line:
if (!is(coords, "SpatialPoints")) coords = coordinates(coords)
When coords is a dataframe, it returns a matrix with the rownames lost.
Subsequent matching with data is not longer possible. Should'nt the test
be reversed: if (is(coords, "SpatialPoints")) coords = coordinates(coords) ?
I am working with recent versions of R (3.1.2 (2014-10-31)) and sp (1.0-17).
Thanks for any help towards the resolution of this problem.
Jean-Luc Dupouey
--
INRA
Forest Ecology and Ecophysiology Unit
F-54280 Champenoux
France
mail: dupouey at nancy.inra.fr
More information about the R-sig-Geo
mailing list