Sum of spatialGridDataFrame:rowSums or colSums
问题描述:
我想对空间网格数据框进行简单的操作。考虑我需要知道每个空间点(此处为行)的每个列的总和。不幸的是,我无法使用sum或rowSums函数来做到这一点。你能帮我怎么做吗?我搜查了很多,但没有看到任何类似的情况。Sum of spatialGridDataFrame:rowSums or colSums
我的数据汇总为:
FLint
Object of class SpatialGridDataFrame
Object of class SpatialGrid
Grid topology:
cellcentre.offset cellsize cells.dim
x 582228.8 9.071439 568
y 4505538.0 9.071439 445
SpatialPoints:
x y
[1,] 582228.8 4509566
[2,] 582237.9 4509566
....
Data summary:
Deterministic sim001 sim002
Min. :1 Min. :1 Min. :1
1st Qu.:1 1st Qu.:1 1st Qu.:1
Median :1 Median :1 Median :1
Mean :1 Mean :1 Mean :1
3rd Qu.:1 3rd Qu.:1 3rd Qu.:1
Max. :1 Max. :1 Max. :1
NA's :220354 NA's :220354 NA's :220354
在这里,我尝试了很多,总结例如两个最后一栏
y2 <- rowSums (FLint[,2:3], na.rm = TRUE, dims = 1)
Error in base::rowSums(x, na.rm = na.rm, dims = dims, ...) :
'x' must be an array of at least two dimensions
答
我稍微困惑,你是否希望rowSums()
或colSums()
,但都将以相同的方式使用:
您需要拨打rowSums()
通过S4对象的data
插槽
这里的玩具例子
library(sp)
## data taken from ?SpatialGridDataFrame
data(meuse.grid) # only the non-missing valued cells
coordinates(meuse.grid) = c("x", "y") # promote to SpatialPointsDataFrame
gridded(meuse.grid) <- TRUE # promote to SpatialPixelsDataFrame
x = as(meuse.grid, "SpatialGridDataFrame")
## the data is contained in '@data'
rowSums([email protected][, c("part.a", "part.b")], na.rm = T)
所以,在你的榜样我认为你要
rowSums([email protected][, 2:3])
你可以看到,如果你看一下结构中的数据是
str(x)
Formal class 'SpatialGridDataFrame' [package "sp"] with 4 slots
[email protected] data :'data.frame': 8112 obs. of 5 variables:
.. ..$ part.a: num [1:8112] NA NA NA NA NA NA NA NA NA NA ...
.. ..$ part.b: num [1:8112] NA NA NA NA NA NA NA NA NA NA ...
.. ..$ dist : num [1:8112] NA NA NA NA NA NA NA NA NA NA ...
.. ..$ soil : Factor w/ 3 levels "1","2","3": NA NA NA NA NA NA NA NA NA NA ...
.. ..$ ffreq : Factor w/ 3 levels "1","2","3": NA NA NA NA NA NA NA NA NA NA ...
[email protected] grid :Formal class 'GridTopology' [package "sp"] with 3 slots
.. .. [email protected] cellcentre.offset: Named num [1:2] 178460 329620
.. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
.. .. [email protected] cellsize : Named num [1:2] 40 40
.. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
.. .. [email protected] cells.dim : Named int [1:2] 78 104
.. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
[email protected] bbox : num [1:2, 1:2] 178440 329600 181560 333760
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
[email protected] proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. [email protected] projargs: chr NA
比k你兄弟。我错过了几个小时混淆的唯一的事情是'数据'。我是这种语言的新手,经过大量搜索后无法理解。 – Mohammad
@Mhamhammad不客气。值得了解R中'S3'和'S4'对象之间的区别。我会说99%的时间用于'S3',但是这些空间对象都是'S4'。 – SymbolixAU
@Mhamhammad - 如果这回答你的问题,你可以通过按下投票箭头下的“嘀嗒”来“接受”它。 – SymbolixAU