2

データに対して空間統計関数を実行するには、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

2 つのサンプル データセットには、理解できない違いがあります。関数 str() は違いを明らかにしませんか?

データセット testgeo で関数 head() が失敗するのはなぜですか?

列を追加するときに 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)
4

3 に答える 3

6

の特定の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ため、それらのように動作しないことです。

于 2013-02-04T10:58:45.167 に答える
2

head(meuse)データセットの最初の数行ではなく、最初の数列meuse(6 + 座標列) が得られました。
データセットtestgeoには列が 1 つしかないため、head(testgeo)失敗します。しかしhead(testgeo,1)、動作します。

head(testgeo,1)
   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
7       (7, 7)          7
8       (8, 8)          8
9       (9, 9)          9
10    (10, 10)         10

行ではなく列が選択される理由は私にはわかりませんが、最初の数行を見たい場合はtestgeo、より伝統的な方法を使用できます。

testgeo[1:5, ]
  coordinates myvariable
1      (1, 1)          1
2      (2, 2)          2
3      (3, 3)          3
4      (4, 4)          4
5      (5, 5)          5
于 2013-02-04T10:38:10.733 に答える
0

sp には、headすべてのSpatialオブジェクトのメソッドがあり、次のように実装されています。

> sp:::head.Spatial
function (x, n = 6L, ...) 
{
    ix <- sign(n) * seq(abs(n))
    x[ix, , drop = FALSE]
}

ネガティブも処理することに注意してくださいn

于 2015-06-26T06:29:08.487 に答える