R Under development (unstable) (2023-05-24 r84463 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > options("rgdal_show_exportToProj4_warnings"="none") > library(sp) The legacy packages maptools, rgdal, and rgeos, underpinning this package will retire shortly. Please refer to R-spatial evolution reports on https://r-spatial.org/r/2023/05/15/evolution4.html for details. This package is now running under evolution status 0 > data(meuse) > x = meuse > coordinates(x) = cbind(rnorm(155), rnorm(155)) > # should pass: > names(x@data) [1] "x" "y" "cadmium" "copper" "lead" "zinc" "elev" [8] "dist" "om" "ffreq" "soil" "lime" "landuse" "dist.m" > names(as.data.frame(x)) [1] "x" "y" "cadmium" "copper" "lead" "zinc" [7] "elev" "dist" "om" "ffreq" "soil" "lime" [13] "landuse" "dist.m" "coords.x1" "coords.x2" > class(as(x, "data.frame")) [1] "data.frame" > x = meuse > # coordinates defined as data: > coordinates(x) = cbind(xcoord = rnorm(155), ycoord = rnorm(155)) > # should pass: > names(x@data) [1] "x" "y" "cadmium" "copper" "lead" "zinc" "elev" [8] "dist" "om" "ffreq" "soil" "lime" "landuse" "dist.m" > names(as.data.frame(x)) [1] "x" "y" "cadmium" "copper" "lead" "zinc" "elev" [8] "dist" "om" "ffreq" "soil" "lime" "landuse" "dist.m" [15] "xcoord" "ycoord" > is.projected(x) [1] NA > proj4string(x) [1] NA > > set.seed(13131) # make sample reproducable: > x = meuse[, sample(ncol(meuse))] # 'randomly' shuffle columns > # coordinates defined as variable names: > coordinates(x) = c("x", "y") # no matter their position > #plot(x, cex=.05 * sqrt(x@data[,"zinc"]), > plot(x, cex=.05 * sqrt(as.data.frame(x)[["zinc"]]),pch=1) > title("Meuse: zinc bubble plot") > print(summary(x)) Object of class SpatialPointsDataFrame Coordinates: min max x 178605 181390 y 329714 333611 Is projected: NA proj4string : [NA] Number of points: 155 Data attributes: lime om landuse ffreq zinc 0:111 Min. : 1.000 W :50 1:84 Min. : 113.0 1: 44 1st Qu.: 5.300 Ah :39 2:48 1st Qu.: 198.0 Median : 6.900 Am :22 3:23 Median : 326.0 Mean : 7.478 Fw :10 Mean : 469.7 3rd Qu.: 9.000 Ab : 8 3rd Qu.: 674.5 Max. :17.000 (Other):25 Max. :1839.0 NA's :2 NA's : 1 dist copper cadmium soil dist.m Min. :0.00000 Min. : 14.00 Min. : 0.200 1:97 Min. : 10.0 1st Qu.:0.07569 1st Qu.: 23.00 1st Qu.: 0.800 2:46 1st Qu.: 80.0 Median :0.21184 Median : 31.00 Median : 2.100 3:12 Median : 270.0 Mean :0.24002 Mean : 40.32 Mean : 3.246 Mean : 290.3 3rd Qu.:0.36407 3rd Qu.: 49.50 3rd Qu.: 3.850 3rd Qu.: 450.0 Max. :0.88039 Max. :128.00 Max. :18.100 Max. :1000.0 lead elev Min. : 37.0 Min. : 5.180 1st Qu.: 72.5 1st Qu.: 7.546 Median :123.0 Median : 8.180 Mean :153.4 Mean : 8.165 3rd Qu.:207.0 3rd Qu.: 8.955 Max. :654.0 Max. :10.520 > > # coordinates defined as formula: > x = meuse[, 1:5] > coordinates(x) = ~x+y > print(summary(x)) Object of class SpatialPointsDataFrame Coordinates: min max x 178605 181390 y 329714 333611 Is projected: NA proj4string : [NA] Number of points: 155 Data attributes: cadmium copper lead Min. : 0.200 Min. : 14.00 Min. : 37.0 1st Qu.: 0.800 1st Qu.: 23.00 1st Qu.: 72.5 Median : 2.100 Median : 31.00 Median :123.0 Mean : 3.246 Mean : 40.32 Mean :153.4 3rd Qu.: 3.850 3rd Qu.: 49.50 3rd Qu.:207.0 Max. :18.100 Max. :128.00 Max. :654.0 > > # a = NULL > # cc = cbind(sample(1:10), sample(1:10), sample(1:10)) > # coordinates(a) = cc > # summary(a) > > xx = SpatialPointsDataFrame(matrix(1:10,5,2),data.frame(f = 1:5)) > rbind(xx,xx,xx,xx) coordinates f 1 (1, 6) 1 2 (2, 7) 2 3 (3, 8) 3 4 (4, 9) 4 5 (5, 10) 5 6 (1, 6) 1 7 (2, 7) 2 8 (3, 8) 3 9 (4, 9) 4 10 (5, 10) 5 11 (1, 6) 1 12 (2, 7) 2 13 (3, 8) 3 14 (4, 9) 4 15 (5, 10) 5 16 (1, 6) 1 17 (2, 7) 2 18 (3, 8) 3 19 (4, 9) 4 20 (5, 10) 5 > > grd <- GridTopology(c(1,1), c(1,1), c(10,10)) > polys <- as.SpatialPolygons.GridTopology(grd) > summary(rbind(polys[1:10], polys[11:20], polys[21:30])) Object of class SpatialPolygons Coordinates: min max x 0.5 10.5 y 7.5 10.5 Is projected: NA proj4string : [NA] > plot(rbind(polys[1:10],polys[21:30])) > title("2 x 10 blocks -- test rbind on SpatialPolygons") > > l1 = cbind(c(1,2,3),c(3,2,2)) > l1a = cbind(l1[,1]+.05,l1[,2]+.05) > l2 = cbind(c(1,2,3),c(1,1.5,1)) > Sl1 = Line(l1) > Sl1a = Line(l1a) > Sl2 = Line(l2) > S1 = Lines(list(Sl1, Sl1a), ID="a") > S2 = Lines(list(Sl2), ID="b") > Sl = SpatialLines(list(S1,S2)) > > summary(as(polys, "SpatialLines")) Object of class SpatialLines Coordinates: min max x 0.5 10.5 y 0.5 10.5 Is projected: NA proj4string : [NA] > summary(as(Sl, "SpatialPoints")) Object of class SpatialPoints Coordinates: min max coords.x1 1 3.05 coords.x2 1 3.05 Is projected: NA proj4string : [NA] Number of points: 9 > summary(as(Sl, "SpatialPointsDataFrame")) Object of class SpatialPointsDataFrame Coordinates: min max coords.x1 1 3.05 coords.x2 1 3.05 Is projected: NA proj4string : [NA] Number of points: 9 Data attributes: Lines.NR Lines.ID Line.NR Min. :1.000 Length:9 Min. :1.000 1st Qu.:1.000 Class :character 1st Qu.:1.000 Median :1.000 Mode :character Median :1.000 Mean :1.333 Mean :1.333 3rd Qu.:2.000 3rd Qu.:2.000 Max. :2.000 Max. :2.000 > SlDf = SpatialLinesDataFrame(Sl, data.frame(xx = c("foo", "bar")), match.ID = FALSE) > summary(as(SlDf, "SpatialPointsDataFrame")) Object of class SpatialPointsDataFrame Coordinates: min max coords.x1 1 3.05 coords.x2 1 3.05 Is projected: NA proj4string : [NA] Number of points: 9 Data attributes: xx Lines.NR Lines.ID Line.NR Length:9 Min. :1.000 Length:9 Min. :1.000 Class :character 1st Qu.:1.000 Class :character 1st Qu.:1.000 Mode :character Median :1.000 Mode :character Median :1.000 Mean :1.333 Mean :1.333 3rd Qu.:2.000 3rd Qu.:2.000 Max. :2.000 Max. :2.000 > > meuse[["xxx"]] = log(meuse$zinc) > summary(meuse) x y cadmium copper Min. :178605 Min. :329714 Min. : 0.200 Min. : 14.00 1st Qu.:179371 1st Qu.:330762 1st Qu.: 0.800 1st Qu.: 23.00 Median :179991 Median :331633 Median : 2.100 Median : 31.00 Mean :180005 Mean :331635 Mean : 3.246 Mean : 40.32 3rd Qu.:180630 3rd Qu.:332463 3rd Qu.: 3.850 3rd Qu.: 49.50 Max. :181390 Max. :333611 Max. :18.100 Max. :128.00 lead zinc elev dist Min. : 37.0 Min. : 113.0 Min. : 5.180 Min. :0.00000 1st Qu.: 72.5 1st Qu.: 198.0 1st Qu.: 7.546 1st Qu.:0.07569 Median :123.0 Median : 326.0 Median : 8.180 Median :0.21184 Mean :153.4 Mean : 469.7 Mean : 8.165 Mean :0.24002 3rd Qu.:207.0 3rd Qu.: 674.5 3rd Qu.: 8.955 3rd Qu.:0.36407 Max. :654.0 Max. :1839.0 Max. :10.520 Max. :0.88039 om ffreq soil lime landuse dist.m Min. : 1.000 1:84 1:97 0:111 W :50 Min. : 10.0 1st Qu.: 5.300 2:48 2:46 1: 44 Ah :39 1st Qu.: 80.0 Median : 6.900 3:23 3:12 Am :22 Median : 270.0 Mean : 7.478 Fw :10 Mean : 290.3 3rd Qu.: 9.000 Ab : 8 3rd Qu.: 450.0 Max. :17.000 (Other):25 Max. :1000.0 NA's :2 NA's : 1 xxx Min. :4.727 1st Qu.:5.288 Median :5.787 Mean :5.886 3rd Qu.:6.514 Max. :7.517 > meuse$xxy = log(meuse[["zinc"]]) > summary(meuse) x y cadmium copper Min. :178605 Min. :329714 Min. : 0.200 Min. : 14.00 1st Qu.:179371 1st Qu.:330762 1st Qu.: 0.800 1st Qu.: 23.00 Median :179991 Median :331633 Median : 2.100 Median : 31.00 Mean :180005 Mean :331635 Mean : 3.246 Mean : 40.32 3rd Qu.:180630 3rd Qu.:332463 3rd Qu.: 3.850 3rd Qu.: 49.50 Max. :181390 Max. :333611 Max. :18.100 Max. :128.00 lead zinc elev dist Min. : 37.0 Min. : 113.0 Min. : 5.180 Min. :0.00000 1st Qu.: 72.5 1st Qu.: 198.0 1st Qu.: 7.546 1st Qu.:0.07569 Median :123.0 Median : 326.0 Median : 8.180 Median :0.21184 Mean :153.4 Mean : 469.7 Mean : 8.165 Mean :0.24002 3rd Qu.:207.0 3rd Qu.: 674.5 3rd Qu.: 8.955 3rd Qu.:0.36407 Max. :654.0 Max. :1839.0 Max. :10.520 Max. :0.88039 om ffreq soil lime landuse dist.m Min. : 1.000 1:84 1:97 0:111 W :50 Min. : 10.0 1st Qu.: 5.300 2:48 2:46 1: 44 Ah :39 1st Qu.: 80.0 Median : 6.900 3:23 3:12 Am :22 Median : 270.0 Mean : 7.478 Fw :10 Mean : 290.3 3rd Qu.: 9.000 Ab : 8 3rd Qu.: 450.0 Max. :17.000 (Other):25 Max. :1000.0 NA's :2 NA's : 1 xxx xxy Min. :4.727 Min. :4.727 1st Qu.:5.288 1st Qu.:5.288 Median :5.787 Median :5.787 Mean :5.886 Mean :5.886 3rd Qu.:6.514 3rd Qu.:6.514 Max. :7.517 Max. :7.517 > > # test behaviour on zero-length objects: > demo(meuse, ask = FALSE, echo = FALSE) > p = as(meuse, "SpatialPoints") > > p[0] SpatialPoints: x y Coordinate Reference System (CRS) arguments: +proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs > dim(p[0]) NULL > > meuse[0,] [1] coordinates cadmium copper lead zinc elev [7] dist om ffreq soil lime landuse [13] dist.m <0 rows> (or 0-length row.names) > dim(meuse[1:3,0]) [1] 3 0 > dim(subset(meuse, zinc < 100, 0)) [1] 0 0 > dim(subset(meuse, , 0)) [1] 155 0 > dim(subset(meuse, F, 0)) [1] 0 0 > > dim(meuse.grid[0,]) [1] 0 5 > fullgrid(meuse.grid) = TRUE > dim(meuse.grid[0,]) [1] 8112 5 > summary(meuse.grid[0,]) Object of class SpatialGridDataFrame Coordinates: min max x 178440 181560 y 329600 333760 Is projected: TRUE proj4string : [+proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs] Grid attributes: cellcentre.offset cellsize cells.dim x 178460 40 78 y 329620 40 104 Data attributes: part.a part.b dist soil ffreq Mode:logical Mode:logical Mode:logical Mode:logical Mode:logical NA's:8112 NA's:8112 NA's:8112 NA's:8112 NA's:8112 > dim(meuse.grid[,0]) [1] 8112 5 > summary(meuse.grid[,0]) Object of class SpatialGridDataFrame Coordinates: min max x 178440 181560 y 329600 333760 Is projected: TRUE proj4string : [+proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs] Grid attributes: cellcentre.offset cellsize cells.dim x 178460 40 78 y 329620 40 104 Data attributes: part.a part.b dist soil ffreq Mode:logical Mode:logical Mode:logical Mode:logical Mode:logical NA's:8112 NA's:8112 NA's:8112 NA's:8112 NA's:8112 > dim(meuse.grid[0,,0]) [1] 8112 0 > summary(meuse.grid[0,,0]) Object of class SpatialGridDataFrame Coordinates: min max x 178440 181560 y 329600 333760 Is projected: TRUE proj4string : [+proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs] Grid attributes: cellcentre.offset cellsize cells.dim x 178460 40 78 y 329620 40 104 > > # SpatialPolygons: > L = as(meuse.riv, "SpatialPolygons") > length(L[1]) [1] 1 > length(L[0]) [1] 0 > summary(L[0]) Object of class SpatialPolygons Coordinates: min max x 178304.0 182331.5 y 325698.5 337684.8 Is projected: TRUE proj4string : [+proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs] > > # SpatialPolygonsDataFrame: > L$something = "bla" > class(L) [1] "SpatialPolygonsDataFrame" attr(,"package") [1] "sp" > length(L[1]) [1] 1 > summary(L[1]) Object of class SpatialPolygonsDataFrame Coordinates: min max x 178304.0 182331.5 y 325698.5 337684.8 Is projected: TRUE proj4string : [+proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs] Data attributes: something Length:1 Class :character Mode :character > length(L[0]) [1] 1 > summary(L[0]) Object of class SpatialPolygonsDataFrame Coordinates: min max x 178304.0 182331.5 y 325698.5 337684.8 Is projected: TRUE proj4string : [+proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs] > > # SpatialLines > L = as(meuse.riv, "SpatialLines") > length(L[1]) [1] 1 > length(L[0]) [1] 0 > summary(L[0]) Object of class SpatialLines Coordinates: min max x 178304.0 182331.5 y 325698.5 337684.8 Is projected: TRUE proj4string : [+proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs] > > # SpatialLinesDataFrame > L$something = "bla" > class(L) [1] "SpatialLinesDataFrame" attr(,"package") [1] "sp" > length(L[1]) [1] 1 > summary(L[1]) Object of class SpatialLinesDataFrame Coordinates: min max x 178304.0 182331.5 y 325698.5 337684.8 Is projected: TRUE proj4string : [+proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs] Data attributes: something Length:1 Class :character Mode :character > length(L[0]) [1] 1 > summary(L[0]) Object of class SpatialLinesDataFrame Coordinates: min max x 178304.0 182331.5 y 325698.5 337684.8 Is projected: TRUE proj4string : [+proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs] > > m = meuse > all.equal(rbind(m[1:3,], m[5:7,]), rbind(m[1:3,], m[0,], m[5:7,])) [1] TRUE > all.equal(rbind(m[1:3,], m[5:7,]), rbind(m[0,], m[1:3,], m[0,], m[5:7,])) [1] TRUE > all.equal(rbind(m[1:3,], m[5:7,]), rbind(m[1:3,], m[0,], m[5:7,], m[0,])) [1] TRUE > > # match.ID settings for SpatialPointsDataFrame(): > set.seed(1331) > pts = cbind(1:5, 1:5) > dimnames(pts)[[1]] = letters[1:5] > df = data.frame(a = 1:5) > row.names(df) = letters[5:1] > > library(sp) > options(warn=1) # show warnings where they occur > SpatialPointsDataFrame(pts, df) # warn Warning in SpatialPointsDataFrame(pts, df) : forming a SpatialPointsDataFrame based on maching IDs, not on record order. Use match.ID = FALSE to match on record order coordinates a a (1, 1) 5 b (2, 2) 4 c (3, 3) 3 d (4, 4) 2 e (5, 5) 1 > SpatialPointsDataFrame(pts, df, match.ID = TRUE) # don't warn coordinates a a (1, 1) 5 b (2, 2) 4 c (3, 3) 3 d (4, 4) 2 e (5, 5) 1 > SpatialPointsDataFrame(pts, df, match.ID = FALSE) # don't warn coordinates a e (1, 1) 1 d (2, 2) 2 c (3, 3) 3 b (4, 4) 4 a (5, 5) 5 > df$m = letters[5:1] > SpatialPointsDataFrame(pts, df, match.ID = "m") # don't warn coordinates a m a (1, 1) 5 a b (2, 2) 4 b c (3, 3) 3 c d (4, 4) 2 d e (5, 5) 1 e > > dimnames(pts)[[1]] = letters[5:1] > pts [,1] [,2] e 1 1 d 2 2 c 3 3 b 4 4 a 5 5 > SpatialPointsDataFrame(pts, df) # don't warn: match doesn't reorder coordinates a m e (1, 1) 1 e d (2, 2) 2 d c (3, 3) 3 c b (4, 4) 4 b a (5, 5) 5 a > > # duplicated row name: > dimnames(pts)[[1]] = letters[c(1:4,1)] > pts [,1] [,2] a 1 1 b 2 2 c 3 3 d 4 4 a 5 5 > xx = try(x <- SpatialPointsDataFrame(pts, df)) Warning in SpatialPointsDataFrame(pts, df) : forming a SpatialPointsDataFrame based on maching IDs, not on record order. Use match.ID = FALSE to match on record order Error in SpatialPointsDataFrame(pts, df) : row.names of data and dimnames of coords do not match > class(xx) [1] "try-error" > xx [1] "Error in SpatialPointsDataFrame(pts, df) : \n row.names of data and dimnames of coords do not match\n" attr(,"class") [1] "try-error" attr(,"condition") > SpatialPointsDataFrame(pts, df, match.ID = FALSE) Warning in validityMethod(object) : duplicate rownames are interpreted by rgeos as MultiPoints; use SpatialMultiPoints to define these; in future sp versions this warning will become an error coordinates a m e (1, 1) 1 e d (2, 2) 2 d c (3, 3) 3 c b (4, 4) 4 b a (5, 5) 5 a > try(x <- SpatialPointsDataFrame(pts, df, match.ID = TRUE)) # fail Error in SpatialPointsDataFrame(pts, df, match.ID = TRUE) : row.names of data and dimnames of coords do not match > > dimnames(pts)[[1]] = letters[5:1] > row.names(df) = letters[6:2] # non-matching row.names > SpatialPointsDataFrame(pts, df) # do not match.ID coordinates a m f (1, 1) 1 e e (2, 2) 2 d d (3, 3) 3 c c (4, 4) 4 b b (5, 5) 5 a > SpatialPointsDataFrame(pts, df, match.ID = FALSE) coordinates a m f (1, 1) 1 e e (2, 2) 2 d d (3, 3) 3 c c (4, 4) 4 b b (5, 5) 5 a > try(x <- SpatialPointsDataFrame(pts, df, match.ID = TRUE)) # fail Error in SpatialPointsDataFrame(pts, df, match.ID = TRUE) : row.names of data and coords do not match > > proc.time() user system elapsed 1.01 0.12 1.14