[R-sig-Geo] matching does not work in SpatialPointsDataFrame
Edzer Pebesma
edzer.pebesma at uni-muenster.de
Thu Jan 22 13:29:36 CET 2015
Well spotted, thanks! I reorganized the logic behind the coordinates()
method for data.frame objects, and this should now be fixed; committed
to r-forge.
On 01/21/2015 10:45 PM, Jean-Luc Dupouey wrote:
> 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
>
--
Edzer Pebesma, Co-Editor-in-Chief Computers & Geosciences
Institute for Geoinformatics (ifgi), University of Münster
Heisenbergstraße 2, 48149 Münster, Germany. Phone: +49 251
83 33081 http://ifgi.uni-muenster.de GPG key ID 0xAC227795
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20150122/1c3bec0e/attachment.bin>
More information about the R-sig-Geo
mailing list