gpt4 book ai didi

R 编程无法在 SpatialPointsDataFrame 上运行 head()

转载 作者:行者123 更新时间:2023-12-02 07:04:33 26 4
gpt4 key购买 nike

我需要将 R data.frame 对象转换为 SpatialPointsDataFrame 对象,以便对数据运行空间统计函数。但是,出于某种原因,将 data.frame 对象转换为 SpatialPointsDataFrame 会在对转换后的对象运行特定函数时产生意外行为。

在此示例中,我尝试在生成的 SpatialPointsDataFrame 上运行 head() 函数为什么函数 head() 在某些 SpatialPointsDataFrame 对象上失败?

这是重现该行为的代码。

例子1,没有报错:

#beginning of r code
#load S Classes and Methods for Spatial Data package "sp"
library(sp)
#Load an example dataset that contain geographic ccoordinates
data(meuse)
#check the structure of the data, it is a data.frame
str(meuse)
#>'data.frame': 155 obs. of 14 variables: ...
#with coordinates x,y
#Convert the data into a SpatialPointsDataFrame, by function coordinates()
coordinates(meuse) <- c("x", "y")
#check structure, seems ok
str(meuse)
#Check first rows of the data
head(meuse)
#It worked!
#Now create a small own dataset
testgeo <- as.data.frame(cbind(1:10,1:10,1:10))
#set colnames
colnames(testgeo) <- c("x", "y", "myvariable")
#convert to SpatialPointsDataFrame
coordinates(testgeo) <- c("x", "y")
#Seems ok
str(testgeo)
#But try running for instance head()
head(testgeo)
#Resulting output: Error in `[.data.frame`(x@data, i, j, ..., drop = FALSE) :
#undefined columns selected
#end of example code

我不明白这两个示例数据集之间存在一些差异。函数 str() 没有揭示差异?

为什么函数 head() 在数据集 testgeo 上失败?

为什么 head() 在添加更多列时起作用,10 似乎是极限:

testgeo <- as.data.frame(cbind(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10))
coordinates(testgeo) <- c("V1", "V2")
head(testgeo)

最佳答案

没有具体head SpatialPoints/PolygonsDataFrames 的方法, 所以当你调用 head(testgeo)head(meuse)它落入默认方法:

> getAnywhere("head.default")
A single object matching ‘head.default’ was found
It was found in the following places
registered S3 method for head from namespace utils
namespace:utils
with value

function (x, n = 6L, ...)
{
stopifnot(length(n) == 1L)
n <- if (n < 0L)
max(length(x) + n, 0L)
else min(n, length(x))
x[seq_len(n)]
}
<bytecode: 0x97dee18>
<environment: namespace:utils>

然后返回 x[1:n] ,但对于那些空间类,像这样的方括号索引需要列:

> meuse[1]
coordinates cadmium
1 (181072, 333611) 11.7
2 (181025, 333558) 8.6
3 (181165, 333537) 6.5
4 (181298, 333484) 2.6
5 (181307, 333330) 2.8
6 (181390, 333260) 3.0
7 (181165, 333370) 3.2
8 (181027, 333363) 2.8
9 (181060, 333231) 2.4
10 (181232, 333168) 1.6
> meuse[2]
coordinates copper
1 (181072, 333611) 85
2 (181025, 333558) 81
3 (181165, 333537) 68
4 (181298, 333484) 81
5 (181307, 333330) 48
6 (181390, 333260) 61
7 (181165, 333370) 31
8 (181027, 333363) 29
9 (181060, 333231) 37
10 (181232, 333168) 24

所以当你做 head(meuse)它试图获得 meuse[1]meuse[6] ,因为 meuse 而存在有很多列。

但是testgeo没有。所以它失败了。

真正的解决方法可能是写一个 head.SpatialPointsDataFrame那是:

> head.SpatialPointsDataFrame = function(x,n=6,...){x[1:n,]}

这样:

> head(meuse)
coordinates cadmium copper lead zinc elev dist om ffreq soil
1 (181072, 333611) 11.7 85 299 1022 7.909 0.00135803 13.6 1 1
2 (181025, 333558) 8.6 81 277 1141 6.983 0.01222430 14.0 1 1
3 (181165, 333537) 6.5 68 199 640 7.800 0.10302900 13.0 1 1
4 (181298, 333484) 2.6 81 116 257 7.655 0.19009400 8.0 1 2
5 (181307, 333330) 2.8 48 117 269 7.480 0.27709000 8.7 1 2
6 (181390, 333260) 3.0 61 137 281 7.791 0.36406700 7.8 1 2
lime landuse dist.m
1 1 Ah 50
2 1 Ah 30
3 1 Ah 150
4 0 Ga 270
5 0 Ah 380
6 0 Ga 470
> head(testgeo)
coordinates myvariable
1 (1, 1) 1
2 (2, 2) 2
3 (3, 3) 3
4 (4, 4) 4
5 (5, 5) 5
6 (6, 6) 6

这里真正的问题是空间类不继承自 data.frame ,所以他们的行为不像他们。

关于R 编程无法在 SpatialPointsDataFrame 上运行 head(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14684748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com