本文介绍了对空间网格数据框求和:rowSums 或 colSums的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对空间网格数据框做简单的操作.考虑我需要知道每个空间点(此处为行)的每列的总和.不幸的是,我无法使用 sum 或 rowSums 函数来做到这一点.你能帮我怎么做吗?我搜索了很多,但真的没有看到任何类似的案例.

I want to do simple operation on a spatial grid data frame. Consider I need to know the sum of each columns for each spatial points(herein row). Unfortunately I couldn't do that using sum or rowSums function. Could you please help me how to do that? I've searched a lot but really didn't see any similar case.

我的数据摘要是:

 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   

在这里我尝试了很多总结,例如最后两列:

Here I tried a lot to sum for example two last column:

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() 我有点困惑,但是两者都将以相同的方式使用:

I'm slightly confused as to whether you want rowSums() or colSums(), but both will be used in the same way:

您需要在 S4 对象的 data 槽上调用 rowSums()

You need to call rowSums() over the data slot of the S4 object

这是一个玩具示例

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(x@data[, c("part.a", "part.b")], na.rm = T)

所以在你的例子中,我认为你会想要

So in your example I think you'll want

rowSums(Flint@data[, 2:3])

看结构就知道数据在哪里


You can see where the data is if you look at the structure

str(x)
Formal class 'SpatialGridDataFrame' [package "sp"] with 4 slots
  ..@ 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 ...
  ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
  .. .. ..@ cellcentre.offset: Named num [1:2] 178460 329620
  .. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
  .. .. ..@ cellsize         : Named num [1:2] 40 40
  .. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
  .. .. ..@ cells.dim        : Named int [1:2] 78 104
  .. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
  ..@ 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"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr NA

这篇关于对空间网格数据框求和:rowSums 或 colSums的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:21