melt spatialPixelDataFrame
Hi,
I tried: 1) to transform the SPDF to a normal dataframe with as.data.frame(spdf)
but then I loose my x and y columns. Thus I can't use them in melt. Was
that behavior changed recently?
No, I really don't think so. Are you sure? Here x.1 and y.1 are the
(re-attached) grid point coordinates, which incidentally are copies of
the original data "x" and "y".
data(meuse.grid)
m = SpatialPixelsDataFrame(points = meuse.grid[c("x", "y")], data =
meuse.grid)
head(as.data.frame(m))
x y part.a part.b dist soil ffreq x.1 y.1
1 181180 333740 1 0 0.0000000 1 1 181180 333740
2 181140 333700 1 0 0.0000000 1 1 181140 333700
3 181180 333700 1 0 0.0122243 1 1 181180 333700
I am not 100% sure but my output looks different from yours (I am missing x.1 and y.1, the original coords from the SPDF and not the re-attached ones) Here the output with the same example and sessionInfo():
library(raster)
Loading required package: sp raster 2.0-08 (27-June-2012)
data(meuse.grid)
m = SpatialPixelsDataFrame(points = meuse.grid[c("x", "y")], data = meuse.grid)
head(as.data.frame(m))
x y part.a part.b dist soil ffreq 1 181180 333740 1 0 0.0000000 1 1 2 181140 333700 1 0 0.0000000 1 1 3 181180 333700 1 0 0.0122243 1 1 4 181220 333700 1 0 0.0434678 1 1 5 181100 333660 1 0 0.0000000 1 1 6 181140 333660 1 0 0.0122243 1 1
sessionInfo()
R version 2.14.1 (2011-12-22) Platform: i686-pc-linux-gnu (32-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods [7] base other attached packages: [1] raster_2.0-08 sp_0.9-99 loaded via a namespace (and not attached): [1] grid_2.14.1 lattice_0.20-0 tools_2.14.1 Any idea? /johannes