我已经SpatialPointsDataFrame
使用sp
R中的包创建了一个类型为对象的对象。但是,对于@, $, . and []
操作符以及何时使用它们来访问对象的不同属性,我感到困惑。这是我的示例代码:
library(sp)
library(rgdal)
#creating a SpatialPointsDataFrame with sample points in UTM
x <- c(15.2, 15.3, 15.4, 15.5, 15.7)
y <- c(50.4, 50.2, 50.3, 50.1, 50.4)
v1 <- c(1.0, 2.0, 3.0, 4.0, 5.0)
v2 <- c("a","b","b","c","a")
attributes <- as.data.frame(cbind(v1,v2))
xy <- cbind(x,y)
locationsDD <- SpatialPointsDataFrame(xy, attributes)
proj4string(locationsDD) <- CRS("+proj=longlat")
locations <- spTransform(locationsDD, CRS("+proj=utm +zone=33"))
plot(locations)
#using the different operators: WHEN TO USE @, $ or [] ?
#all these work!
property1 <- locations$v1
property2 <- locations@data$v1
property3 <- locations@data[,"v1"]
property4 <- locations@data["v1"]
#these also work
property5 <- locations@coords
property6 <- locations@bbox
property7 <- locations@coords[,2]
#these three work only in my special case
property8 <- locations@coords[,"y"]
property9 <- locations$x
property10 <- locations$y
#these don't work: $ operator is invalid for atomic vectors
property11 <- locations@coords$x
property12 <- locations@coords$y
何时可以使用@, $, []
操作员,有人可以帮我吗?当我尝试阅读文档时,?SpatialPointsDataFrame
我可以看到不同的属性,例如coords
或,bbox
但是我困惑@, $, []
于使用哪个运算符来访问或修改它们。
R
语法的问题,所以它并不特定于sp
包或其对象。R
随教程一起安装:在您的研究中开始。网络和印刷媒体提供了大量的学习资源R
。