[R-sig-Geo] a goofy question

Michael Sumner mdsumner at gmail.com
Mon Feb 17 01:53:40 CET 2014


Well, with a reproducible example this is what I (expect and) get:

coal2 <- data.frame(x1 = rnorm(10), y1 = rnorm(10), var1.pred = 10)
library(sp)
buzz <- function(x, y, u) {coordinates(u) <- ~x+y; print(str(u))}
buzz(x=coal2$x1,y=coal2$y1,u=coal2)
Error in `[.data.frame`(object, , -coord.numbers, drop = FALSE) :
  undefined columns selected


I'm not sure the formula method for "coordinates<-" is the best idea
here, try with the actual names (and think about what ~x+y is vs.
coal2$x1 and coal2$x2):

buzz <- function(x, y, u) {coordinates(u) <- c(x, y); print(str(u))}
buzz("x1", "y1", coal2)

Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 10 obs. of  1 variable:
  .. ..$ var1.pred: num [1:10] 10 10 10 10 10 10 10 10 10 10
  ..@ coords.nrs : int [1:2] 1 2
  ..@ coords     : num [1:10, 1:2] -1.112 -0.933 0.485 -0.104 -0.369 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "x1" "y1"
  ..@ bbox       : num [1:2, 1:2] -2.08 -1.15 3.19 2.04
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x1" "y1"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
  .. .. ..@ projargs: chr NA
NULL


HTH


On Mon, Feb 17, 2014 at 11:36 AM, Hodgess, Erin <HodgessE at uhd.edu> wrote:
> Hi!
>
> I have a data frame and I'm passing the column names and the df name itself into the function.  Here is my code:
>
>
>> str(coal2)
> 'data.frame':    5006 obs. of  5 variables:
>  $ x1        : num  3.99 4.2 4.41 4.62 4.83 ...
>  $ x2        : num  1.13 1.13 1.13 1.13 1.13 ...
>  $ var1.pred : num  9.91 9.87 9.83 9.79 9.77 ...
>  $ var1.var  : num  1.26 1.25 1.25 1.25 1.24 ...
>  $ var1.stdev: num  1.12 1.12 1.12 1.12 1.11 ...
>> buzz
> function(x,y,u) {
>     coordinates(u) <- ~x+y
>     print(str(u))
>     }
>> buzz(x=coal2$x1,y=coal2$x2,u=coal2)
>
>
> Shouldn't I get a print statement, please?
>
> Thanks,
> Erin
>
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



-- 
Michael Sumner
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-sig-Geo mailing list