[R-sig-Geo] inconsistent as.data.frame(SpatialPointsDF)

Dominik Schneider Dominik.Schneider at colorado.edu
Fri Mar 20 21:09:45 CET 2015


Thanks for the update.
FWIW, I actually liked the behavior of changing coordinate names to x and y
when converting out of geographic coordinates to projected coordinates. and
vice versa would be convenient but I will make a habit of using
coordinates() and coordnames() instead.
ds


Dominik Schneider
o 303.735.6296 | c 518.956.3978


On Fri, Mar 20, 2015 at 1:39 PM, Robert J. Hijmans <r.hijmans at gmail.com>
wrote:

> The raster package 'enhances' as.data.frame for SpatialLines* and
> SpatialPolygons*. I am not sure why there is also a function for
> SpatialPoints* as it does not add anything, except for setting the
> names to 'x' and 'y'. Perhaps that is what I did it for, I cannot
> remember. But clearly it is not desirable to get different results
> depending on when raster is loaded or not, so I have removed it. Sorry
> for causing trouble.
> Robert
>
> On Fri, Mar 20, 2015 at 11:39 AM, Frede Aakmann Tøgersen
> <frtog at vestas.com> wrote:
> > Hi
> >
> > I can reproduce this using Edzer's example. See below for output from R.
> Also notice the naming of the coordinate columns to "x" and "y" in the S4
> method of the raster package's function as.data.frame! Isn't that quite
> unusual? I have maintainer on CC.
> >
> >> library(sp)
> >> df = data.frame(Longitude=1:2, Latitude=2:1, z = 3:4)
> >> df1 = df
> >> coordinates(df1) = ~Longitude + Latitude
> >> as.data.frame(df1)
> >   Longitude Latitude z
> > 1         1        2 3
> > 2         2        1 4
> >> library(raster)
> >> search()
> >  [1] ".GlobalEnv"        "package:raster"    "package:sp"
> >  [4] "ESSR"              "package:stats"     "package:graphics"
> >  [7] "package:grDevices" "package:utils"     "package:datasets"
> > [10] "package:methods"   "Autoloads"         "package:base"
> >> as.data.frame(df1)
> >   x y z
> > 1 1 2 3
> > 2 2 1 4
> >> showMethods("as.data.frame")
> > Function: as.data.frame (package base)
> > x="ANY"
> > x="data.frame"
> >     (inherited from: x="ANY")
> > x="Raster"
> > x="SpatialLines"
> > x="SpatialPoints"
> > x="SpatialPointsDataFrame"
> >     (inherited from: x="SpatialPoints")
> > x="SpatialPolygons"
> >
> >> selectMethod("as.data.frame", "SpatialPointsDataFrame")
> > Method Definition:
> >
> > function (x, row.names = NULL, optional = FALSE, ...)
> > {
> >     .local <- function (x, row.names = NULL, optional = FALSE,
> >         xy = TRUE, ...)
> >     {
> >         if (!xy) {
> >             if (.hasSlot(x, "data")) {
> >                 return(x at data)
> >             }
> >             else {
> >                 return(NULL)
> >             }
> >         }
> >         nobj <- length(x)
> >         d <- coordinates(x)
> >         if (.hasSlot(x, "data")) {
> >             d <- cbind(d, x at data)
> >         }
> >         colnames(d)[1:2] <- c("x", "y")
> >         rownames(d) <- NULL
> >         as.data.frame(d, row.names = row.names, optional = optional,
> >             ...)
> >     }
> >     .local(x, row.names, optional, ...)
> > }
> > <bytecode: 0x000000000d037260>
> > <environment: namespace:raster>
> >
> > Signatures:
> >         x
> > target  "SpatialPointsDataFrame"
> > defined "SpatialPoints"
> >>
> >
> >
> >
> > Yours sincerely / Med venlig hilsen
> >
> >
> > Frede Aakmann Tøgersen
> > Specialist, M.Sc., Ph.D.
> > Plant Performance & Modeling
> >
> > Technology & Service Solutions
> > T +45 9730 5135
> > M +45 2547 6050
> > frtog at vestas.com
> > http://www.vestas.com
> >
> > Company reg. name: Vestas Wind Systems A/S
> > This e-mail is subject to our e-mail disclaimer statement.
> > Please refer to www.vestas.com/legal/notice
> > If you have received this e-mail in error please contact the sender.
> >
> >> -----Original Message-----
> >> From: R-sig-Geo [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of
> >> dschneiderch
> >> Sent: 20. marts 2015 18:19
> >> To: r-sig-geo at r-project.org
> >> Subject: Re: [R-sig-Geo] inconsistent as.data.frame(SpatialPointsDF)
> >>
> >> I just went through my packages, iteratively unloading each one with:
> >> detach('package:raster')
> >> and found that if I unload the raster package then the expected
> behavior of
> >> keeping the existing coordnames is achieved.
> >>
> >> now why that differs in my script I have no idea.
> >>
> >> > coordnames(snotellocs)
> >> [1] "Longitude" "Latitude"
> >> > detach('package:raster')
> >> > head(as.data.frame(snotellocs))
> >>   network State Station_ID Site_ID       Site_Name Latitude Longitude
> >> 1    SNTL    AZ     11R06S     308     BAKER BUTTE 34.45660 -111.4064
> >> 2    SNTL    AZ     11R07S    1140 BAKER BUTTE SMT 34.45547 -111.3827
> >> 3    SNTL    AZ     09S01S     310           BALDY 33.97883 -109.5034
> >> 4    SNTL    AZ     09S06S     902     BEAVER HEAD 33.69144 -109.2166
> >> 5    SNTL    AZ     09N05S    1143   BEAVER SPRING 36.32678 -109.0571
> >> 6    SNTL    AZ     12P01S    1139       CHALENDER 35.26247 -112.0623
> >>   Elevation_ft Elevation_m
> >> 1         7300        2225
> >> 2         7700        2347
> >> 3         9125        2781
> >> 4         7990        2435
> >> 5         9200        2804
> >> 6         7100        2164
> >> >
> >>
> >> Dominik Schneider
> >> o 303.735.6296 | c 518.956.3978
> >>
> >>
> >> On Fri, Mar 20, 2015 at 11:02 AM, dschneiderch [via R-sig-geo] <
> >> ml-node+s2731867n7587929h1 at n2.nabble.com> wrote:
> >>
> >> > Edzer - Look like we posted at the same time.
> >> > In my example my coordinates are named, no?
> >> >
> >> > I tried your example in my R environment and can replicate the
> behavior I
> >> > mentioned.
> >> >
> >> > > df = data.frame(x=1:2, y=2:1, z = 3:4)
> >> > > df1 = df
> >> > > library(sp)
> >> > > coordinates(df1) = ~x+y
> >> > > as.data.frame(df1)
> >> >   x y z
> >> > 1 1 2 3
> >> > 2 2 1 4
> >> > > coordnames(df1)
> >> > [1] "x" "y"
> >> > > coordnames(df1)=c('long','lat')
> >> > > str(df1)
> >> > Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
> >> >   ..@ data       :'data.frame': 2 obs. of  1 variable:
> >> >   .. ..$ z: int [1:2] 3 4
> >> >   ..@ coords.nrs : int [1:2] 1 2
> >> >   ..@ coords     : num [1:2, 1:2] 1 2 2 1
> >> >   .. ..- attr(*, "dimnames")=List of 2
> >> >   .. .. ..$ : NULL
> >> >   .. .. ..$ : chr [1:2] "long" "lat"
> >> >   ..@ bbox       : num [1:2, 1:2] 1 1 2 2
> >> >   .. ..- attr(*, "dimnames")=List of 2
> >> >   .. .. ..$ : chr [1:2] "long" "lat"
> >> >   .. .. ..$ : chr [1:2] "min" "max"
> >> >   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
> >> >   .. .. ..@ projargs: chr NA
> >> > > as.data.frame(df1)
> >> >   x y z
> >> > 1 1 2 3
> >> > 2 2 1 4
> >> >
> >> >
> >> > Interestingly, when I open a new R instance and without loading all
> the
> >> > packages associated with my project, the names are *not* converted.
> So it
> >> > seems that one of the packages I have loaded is conflicting with sp
> and
> >> > causing the names to change...  my sessionInfo() was attached in the
> other
> >> > post.
> >> >
> >> >
> >> > ------------------------------
> >> >  If you reply to this email, your message will be added to the
> discussion
> >> > below:
> >> >
> >> > http://r-sig-geo.2731867.n2.nabble.com/inconsistent-as-data-frame-
> >> SpatialPointsDF-tp7587920p7587929.html
> >> >  To unsubscribe from inconsistent as.data.frame(SpatialPointsDF),
> click
> >> > here
> >> > <http://r-sig-
> >> geo.2731867.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_
> >> by_code&node=7587920&code=RG9taW5pay5TY2huZWlkZXJAY29sb3JhZG8u
> >> ZWR1fDc1ODc5MjB8LTEwMzMyMTA1OQ==>
> >> > .
> >> > NAML
> >> > <http://r-sig-
> >> geo.2731867.n2.nabble.com/template/NamlServlet.jtp?macro=macro_view
> >> er&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespac
> >> es.BasicNamespace-nabble.view.web.template.NabbleNamespace-
> >> nabble.naml.namespaces.BasicNamespace-
> >> nabble.view.web.template.NabbleNamespace-
> >> nabble.naml.namespaces.BasicNamespace-
> >> nabble.view.web.template.NabbleNamespace-
> >> nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscrib
> >> ers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-
> >> send_instant_email%21nabble%3Aemail.naml>
> >> >
> >>
> >>
> >>
> >>
> >> --
> >> View this message in context: http://r-sig-
> >> geo.2731867.n2.nabble.com/inconsistent-as-data-frame-SpatialPointsDF-
> >> tp7587920p7587931.html
> >> Sent from the R-sig-geo mailing list archive at Nabble.com.
> >>
> >> _______________________________________________
> >> R-sig-Geo mailing list
> >> R-sig-Geo at r-project.org
> >> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list