7 class ‘SpatialPointsDataFrame’

library("adehabitatHR")

SpatialPointsDataFrame是一个s4 class,用于放带了位置坐标的点点集

data(puechabonsp)
relocs <- puechabonsp$relocs
str(relocs)
## Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
##   ..@ data       :'data.frame':  119 obs. of  4 variables:
##   .. ..$ Name: Factor w/ 4 levels "Brock","Calou",..: 1 1 1 1 1 1 1 1 1 1 ...
##   .. ..$ Age : int [1:119] 2 2 2 2 2 2 2 2 2 2 ...
##   .. ..$ Sex : int [1:119] 1 1 1 1 1 1 1 1 1 1 ...
##   .. ..$ Date: int [1:119] 930701 930703 930706 930707 930708 930709 930713 930714 930715 930720 ...
##   ..@ coords.nrs : num(0) 
##   ..@ coords     : num [1:119, 1:2] 699889 700046 698840 699809 698627 ...
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : NULL
##   .. .. ..$ : chr [1:2] "X" "Y"
##   ..@ bbox       : num [1:2, 1:2] 698626 3157848 701410 3161678
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : chr [1:2] "X" "Y"
##   .. .. ..$ : chr [1:2] "min" "max"
##   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
##   .. .. ..@ projargs: chr NA

对比它和自己建的s4 class的结构,差不多的。

setClass("student", slots = list(name = "character", age = "numeric", GPA = "numeric",
    data = "data.frame"))
table <- data.frame(name = c("Jack", "Mary"), id = c("001", "002"), salary = c(1000,
    2000))
s4 <- new("student", name = "John", age = 21, GPA = 3.5, data = table)
str(s4)
## Formal class 'student' [package ".GlobalEnv"] with 4 slots
##   ..@ name: chr "John"
##   ..@ age : num 21
##   ..@ GPA : num 3.5
##   ..@ data:'data.frame': 2 obs. of  3 variables:
##   .. ..$ name  : chr [1:2] "Jack" "Mary"
##   .. ..$ id    : chr [1:2] "001" "002"
##   .. ..$ salary: num [1:2] 1000 2000

也可以像一般的s4 class一样用@访问元素

head(s4@data)
##   name  id salary
## 1 Jack 001   1000
## 2 Mary 002   2000
head(relocs@data)
##    Name Age Sex   Date
## 1 Brock   2   1 930701
## 2 Brock   2   1 930703
## 3 Brock   2   1 930706
## 4 Brock   2   1 930707
## 5 Brock   2   1 930708
## 6 Brock   2   1 930709

不同的是SpatialPointsDataFrame对象支持类似数据框的访问方式。用$访问普通s4 class会出错:

s4$a1
## Error in s4$a1: $ operator not defined for this S4 class

但是可以SpatialPointsDataFrame不会

relocs$Name
##   [1] Brock Brock Brock Brock Brock Brock Brock Brock Brock Brock
##  [11] Brock Brock Brock Brock Brock Brock Brock Brock Brock Brock
##  [21] Brock Brock Brock Brock Brock Brock Brock Brock Brock Brock
##  [31] Calou Calou Calou Calou Calou Calou Calou Calou Calou Calou
##  [41] Calou Calou Calou Calou Calou Calou Calou Calou Calou Chou 
##  [51] Chou  Chou  Chou  Chou  Chou  Chou  Chou  Chou  Chou  Chou 
##  [61] Chou  Chou  Chou  Chou  Chou  Chou  Chou  Chou  Chou  Chou 
##  [71] Chou  Chou  Chou  Chou  Chou  Chou  Chou  Chou  Chou  Chou 
##  [81] Chou  Chou  Chou  Chou  Chou  Chou  Chou  Chou  Chou  Jean 
##  [91] Jean  Jean  Jean  Jean  Jean  Jean  Jean  Jean  Jean  Jean 
## [101] Jean  Jean  Jean  Jean  Jean  Jean  Jean  Jean  Jean  Jean 
## [111] Jean  Jean  Jean  Jean  Jean  Jean  Jean  Jean  Jean 
## Levels: Brock Calou Chou Jean

还有这样

head(relocs[[1]])
## [1] Brock Brock Brock Brock Brock Brock
## Levels: Brock Calou Chou Jean
head(s4[[1]])  # 这个不行
## Error in s4[[1]]: this S4 class is not subsettable

其他的

head(relocs[[1, ]])  # 跟head(relocs[[1]])一样
## [1] Brock Brock Brock Brock Brock Brock
## Levels: Brock Calou Chou Jean
relocs[[1]][1]
## [1] Brock
## Levels: Brock Calou Chou Jean
head(relocs[1])  # 这个返回结果仍然是SpatialPointsDataFrame,但只带第一列的数据
##         coordinates  Name
## 1 (699889, 3161560) Brock
## 2 (700046, 3161540) Brock
## 3 (698840, 3161030) Brock
## 4 (699809, 3161500) Brock
## 5 (698627, 3160940) Brock
## 6 (698719, 3160990) Brock
## Coordinate Reference System (CRS) arguments: NA
s4[1]  # 普通s4 class同样不支持
## Error in s4[1]: object of type 'S4' is not subsettable

还有

head(relocs[, 1])  # 跟relocs[1]一样
##         coordinates  Name
## 1 (699889, 3161560) Brock
## 2 (700046, 3161540) Brock
## 3 (698840, 3161030) Brock
## 4 (699809, 3161500) Brock
## 5 (698627, 3160940) Brock
## 6 (698719, 3160990) Brock
## Coordinate Reference System (CRS) arguments: NA
relocs[1, ]  # 第一个点
##         coordinates  Name Age Sex   Date
## 1 (699889, 3161559) Brock   2   1 930701

跟数据框不同的是:这样不行

relocs[[, 1]]
## Error in relocs[[, 1]]: incorrect number of subscripts
relocs[[1, 1]]
## Error in relocs[[1, 1]]: incorrect number of subscripts
# 只能这样
relocs[[1]][1]
## [1] Brock
## Levels: Brock Calou Chou Jean

推荐的做法是把SpatialPointsDataFrame转换成数据框,这样就啥都行了。

relocs <- as.data.frame(relocs)
relocs[1, 1]
## [1] Brock
## Levels: Brock Calou Chou Jean
head(relocs[, 1])
## [1] Brock Brock Brock Brock Brock Brock
## Levels: Brock Calou Chou Jean

普通的s4对象不能转换成数据框

as.data.frame(s4)
## Error in as.data.frame.default(s4): cannot coerce class 'structure("student", package = ".GlobalEnv")' to a data.frame