首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将栅格类替换为R中data.frame中的值

将栅格类替换为R中data.frame中的值
EN

Stack Overflow用户
提问于 2016-08-29 20:29:46
回答 3查看 893关注 0票数 0

我有一个分类结果的整数栅格。现在我想用数据帧中的浮点值替换类,即栅格类1= 0.321;类2= 0.232;类3= 3.211。dataframe有很多列,我想针对几种不同的情况替换这些类:

代码语言:javascript
复制
Class C      N      ....
1    0.321   0.001 
2    0.232   0.012 
3    3.211   0.021 

有没有一种方法可以方便地做到这一点,比如将data.frame合并到栅格中?我需要将生成的栅格与另一个栅格相乘以生成我的输出。

这是栅格文件的元数据:

代码语言:javascript
复制
 > LCC
 class       : RasterLayer 
 dimensions  : 3296, 3711, 12231456  (nrow, ncol, ncell)
 resolution  : 2, 2  (x, y)
 extent      : 514151.8, 521573.8, 7856419, 7863011  (xmin, xmax, ymin, ymax)
 coord. ref. : +proj=utm +zone=55 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
 data source : /home/..../Raster.tif 
 names       : Raster 
 values      : 0, 255  (min, max)

`

这是数据帧的元数据:

代码语言:javascript
复制
 >str(SOC)
 data.frame':   11 obs. of  57 variables:
  $ class             : int  8 9 5 6 7 4 1 2 3 0 ...
  $ area              : int  3135964 3941744 9048672 8564312 11568512     
  $ pixel_count       : int  783991 985436 2262168 2141078 2892128 ...
  $ percent_area      : Factor w/ 11 levels "0.17%","17.50%",..: 9 11 3 2 
  $ label.x           : Factor w/ 11 levels "Barren",..: 5 8 2 
  $ label.y           : Factor w/ 8 levels "Barren",..: 4 7 2 
  $ n                 : int  7 4 4 3 4 1 1 NA NA NA ...
  $ mean_C_100cm      : num  25.8 29 21.3 34.8 31.9 ...
  $ mean_N_100cm      : num  0.469 0.514 0.503 0.621 0.34 ...
 ....

`

EN

回答 3

Stack Overflow用户

发布于 2016-08-30 05:41:37

有一个函数(subs)可以实现这个功能。

示例数据(跟在achaio后面)

代码语言:javascript
复制
library(raster)
inp <- raster(ncol=10, nrow=10) 
set.seed(42)
inp[] <- sample(3, ncell(inp), replace=TRUE)
df <- data.frame(Class=c(1,2,3), C=c(0.321,0.232,3.211), N=c(0.001,0.012,0.021))

要将标识符替换为"C“,您可以这样做

代码语言:javascript
复制
x <- subs(inp, df, by=1, which=2)

或者,要同时获得"C“和"N",请执行以下操作

代码语言:javascript
复制
y <- subs(inp, df, by=1, which=2:3)

事实上,正如mace指出的那样,您也可以使用reclassify (但一次只能使用一个变量)。

代码语言:javascript
复制
z <- reclassify(inp, as.matrix(df)[, 1:2])
票数 4
EN

Stack Overflow用户

发布于 2016-08-29 20:56:47

如果我没弄错您想要什么,那么您可以使用df的列中的值为栅格值赋值,方法是:

代码语言:javascript
复制
inp[] <- df[inp[],"C"]

其中,df是您定义的栅格,inp是值从1到3的整数光栅。

例如:

代码语言:javascript
复制
library(raster)
set.seed(42)  ## for reproducibility

inp <- raster(ncol=10, nrow=10) ## example is small, yours will be large
inp[] <- floor(runif(ncell(inp), min=1, max=4))  ## generate integers from 1 to 3
inp[]
##  [1] 3 3 1 3 2 2 3 1 2 3 2 3 3 1 2 3 3 1 2 2 3 1 3 3 1 2 2 3 2 3 3 3 2 3 1 3 1 1 3 2 2 2 1 3 2 3 3 2
## [49] 3 2 2 2 2 3 1 3 3 1 1 2 3 3 3 2 3 1 1 3 3 1 1 1 1 2 1 3 1 2 2 1 2 1 2 2 3 2 1 1 1 1 3 1 1 3 3 3
## [97] 1 2 3 2

df <- data.frame(Class=c(1,2,3), C=c(0.321,0.232,3.211), N=c(0.001,0.012,0.021))  ## your df

## generate output raster the same size as inp
out <- raster(ncol=10,nrow=10)
## map values of out to values in column C of df
## can overwrite inp here if desired, but for example we want to keep inp
## for following steps
out[] <- df[inp[],"C"]
out[]
##  [1] 3.211 3.211 0.321 3.211 0.232 0.232 3.211 0.321 0.232 3.211 0.232 3.211 3.211 0.321 0.232 3.211
## [17] 3.211 0.321 0.232 0.232 3.211 0.321 3.211 3.211 0.321 0.232 0.232 3.211 0.232 3.211 3.211 3.211
## [33] 0.232 3.211 0.321 3.211 0.321 0.321 3.211 0.232 0.232 0.232 0.321 3.211 0.232 3.211 3.211 0.232
## [49] 3.211 0.232 0.232 0.232 0.232 3.211 0.321 3.211 3.211 0.321 0.321 0.232 3.211 3.211 3.211 0.232
## [65] 3.211 0.321 0.321 3.211 3.211 0.321 0.321 0.321 0.321 0.232 0.321 3.211 0.321 0.232 0.232 0.321
## [81] 0.232 0.321 0.232 0.232 3.211 0.232 0.321 0.321 0.321 0.321 3.211 0.321 0.321 3.211 3.211 3.211
## [97] 0.321 0.232 3.211 0.232

## can create a brick and add layers that map values from N column of df
out.brick <- brick(x=out)
out[] <- df[inp[],"N"]
out.brick <- addLayer(out.brick, out)
out.brick[[2]][]
##  [1] 0.021 0.021 0.001 0.021 0.012 0.012 0.021 0.001 0.012 0.021 0.012 0.021 0.021 0.001 0.012 0.021
## [17] 0.021 0.001 0.012 0.012 0.021 0.001 0.021 0.021 0.001 0.012 0.012 0.021 0.012 0.021 0.021 0.021
## [33] 0.012 0.021 0.001 0.021 0.001 0.001 0.021 0.012 0.012 0.012 0.001 0.021 0.012 0.021 0.021 0.012
## [49] 0.021 0.012 0.012 0.012 0.012 0.021 0.001 0.021 0.021 0.001 0.001 0.012 0.021 0.021 0.021 0.012
## [65] 0.021 0.001 0.001 0.021 0.021 0.001 0.001 0.001 0.001 0.012 0.001 0.021 0.001 0.012 0.012 0.001
## [81] 0.012 0.001 0.012 0.012 0.021 0.012 0.001 0.001 0.001 0.001 0.021 0.001 0.001 0.021 0.021 0.021
## [97] 0.001 0.012 0.021 0.012

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2016-08-30 15:13:00

我改写了aichio的例子,使用了重分类函数,下面是一种适用于我的方法:

代码语言:javascript
复制
library(raster)
set.seed(42)  ## for reproducibility

inp <- raster(ncol=10, nrow=10) ## example is small, yours will be large
inp[] <- floor(runif(ncell(inp), min=1, max=4))  ## generate integers from 1 to 3

df <- data.frame(Class=c(1,2,3), C=c(10.321,1.232,0.211), N=c(0.001,0.012,0.021))  ## your df

## generate output raster the same size as inp
out <- raster(ncol=10,nrow=10)


#### here I generate a matrix that defines the
#### reclassification with an upper and lower limit

mtr <- data.frame(cl_low =df$Class -1, cl_high = df$Class, C =df$C)
data.matrix(mtr)  ### use as matrix

# now reclassify using the matrix and transfer the result in a raster brick

out <- reclassify(inp, rcl=mtr)
out.brick <- brick(x=out)

# now the same can be done for next variable
mtr <- data.frame(cl_low =df$Class -1, cl_high = df$Class, N =df$N)
data.matrix(mtr)
out <- reclassify(inp, rcl=mtr)
out.brick <- addLayer(out.brick, out)
out.brick@layers
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39206395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档