本文介绍了如何从r中的数据框创建栅格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中为笛卡尔坐标 (x, y) 指定了值 (l),如下面的最小工作示例所示.

I have a data frame in which values (l) are specified for Cartesian coordinates (x, y) as in the following minimal working example.

set.seed(2013)
df <- data.frame( x = rep( 0:1, each=2 ),
                  y = rep( 0:1,  2),
                  l = rnorm( 4 ))

df
#   x y           l
# 1 0 0 -0.09202453
# 2 0 1  0.78901912
# 3 1 0 -0.66744232
# 4 1 1  1.36061149

我想使用 raster 包创建一个光栅,但我阅读的文档没有揭示一种简单的方法来将数据加载到光栅单元格中.我想出了几种使用 for 循环的方法,但我怀疑我缺少一种更直接的方法.

I want to create a raster using the raster package, but my reading of the documentation has not revealed a simple method for loading data in the form that I have it into the raster cells. I've come up with a couple ways to do it using for loops, but I suspect that there's a much more direct approach that I'm missing.

推荐答案

这是一种方法,通过 SpatialPixelsDataFrame

library(raster)
# create spatial points data frame
spg <- df
coordinates(spg) <- ~ x + y
# coerce to SpatialPixelsDataFrame
gridded(spg) <- TRUE
# coerce to raster
rasterDF <- raster(spg)
rasterDF
# class       : RasterLayer 
# dimensions  : 2, 2, 4  (nrow, ncol, ncell)
# resolution  : 1, 1  (x, y)
# extent      : -0.5, 1.5, -0.5, 1.5  (xmin, xmax, ymin, ymax)
# coord. ref. : NA 
# data source : in memory
# names       : l 
# values      : -0.6674423, 1.360611  (min, max)

help('raster') 描述了许多从不同类的对象创建栅格的方法.

help('raster') describes a number of methods to create a raster from objects of different classes.

这篇关于如何从r中的数据框创建栅格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 03:24