在R中进行多重最低成本分析的循环或者挖掘功能 [英] loop or sapply function for multiple least cost analysis in R

查看:124
本文介绍了在R中进行多重最低成本分析的循环或者挖掘功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用软件包gdistance进行最低成本分析。
这个想法是通过具有定义的成本值的costgrid(raster)来确定从目的地点到源的路径;该路径从而避免了高成本的像素,并且偏好具有低成本值的像素。使用我的数据为我工作的代码是:

  Costpath< -shortestPath(CostTrans,Cherangfirstloc.utm [1,], Cherangfirstloc.utm [132,],output =SpatialLines)

因此, CostTrans 构成costgrid, cherangfirstloc.utm [1,] 是Spatialpoints数据帧(源)和 Cherangfirstloc.utm [132,] 是Spatialpoints数据帧(目标)的最后一个位置/点。输出是连接两个位置/点的线。



然而,我现在想计算多个最小成本路径,因此源数据帧的每一行,目的地保持不变。这意味着下一个来源将是 Cherangfirstloc.utm [2,] ,然后 Cherangfirstloc.utm [3,] 和所以。我认为这可以用一个for循环或者一个 sapply 函数来完成。不幸的是,我不知道如何制定这个。



你能给我一些关于如何制定这个迭代过程的提示吗?
我希望它可以,如果我问在这个地方的问题基本上,我只想知道如何遍历数据框。以下是一些可用作样本数据的代码:


$ b $



b

  library(gdistance)

r< - - (nrows = 6,ncols = 7,xmn = 0,xmx = 7,ymn = ymx = 6,crs =+ proj = utm
+ units = m)

r []< - c(2,2,1,1,5,5, #creates costgrid
2,2,8,8,5,2,1,
7,1,1,8,2,2,2,
8,7,8,8 ,8,8,5,
8,8,1,1,5,3,9,
8,1,1,2,5,3,9)

T < - 转换(r,函数(x)1 /平均值(x),8)#创建costgrid的过渡层
T< - geoCorrection(T)#correction

c1 <-C(5.5,1.5)#第一个源点
c2 < - c(5.5,4)#second源点
c3 <-C(1.5,5.5)#destination

sPath2< - 最短路径(T,c1,c3,output =SpatialLines)#创建最低成本路径

不幸的是,我不知道如何将c1,c2和c3包含在Spatialpoints数据框中,以便它可以穿过希望这仍然有帮助。



如果你能给我任何提示,我将不胜感激。感谢您的努力!

解决方案

如果您只想调用 shortPath 参数,最简单的解决方案是使用循环的循环,如下所示。由于我不熟悉这种分析,我不知道你想对结果做什么,所以这里有两个解决方案:



在这个例子中将立即使用最短路径,因为在下一步将被遗忘:

  for(i in 1:nrow(Cherangfirstloc .utm)){
#计算
Costpath< - 最短路径(CostTrans,Cherangfirstloc.utm [i,],Cherangfirstloc.utm [132,],output =SpatialLines)

###这里您直接使用结果的说明###
}

在这一个中,所有路径都将存储在一个列表中(似乎 shortPath 函数返回一个对象,所以它是更方便的存储方式),每个结果将在结果变量中访问,例如 c code code code code code code code code code code $ result = list()
for(i in 1:nrow(Cherangfirstloc.utm)){
#计算,列表元素中的存储
results [[i]]<最短路径(CostTrans,Cherangfirstloc.utm [i,],Cherangfirstloc.utm [132,],output =SpatialLines)
}

###这里您的全球使用说明结果###


I am using the package gdistance for a least cost analysis. The idea is to determine the path from a destination point to a source over a costgrid (raster) with defined cost values; the path thereby avoids pixels with high costs and prefers pixels with low cost values. The code that works for me with my data is:

Costpath<-shortestPath(CostTrans,Cherangfirstloc.utm[1,],Cherangfirstloc.utm[132,], output="SpatialLines")

Thereby, CostTrans constitutes the costgrid, Cherangfirstloc.utm[1,] is the first location/point from a Spatialpoints dataframe (source) and Cherangfirstloc.utm[132,] is the last location/point from the Spatialpoints dataframe (destination). The output is a line connecting both locations/points.

However, I now want to calculate multiple least cost paths, the source shall thereby be every row of the dataframe, the destination stays the same. This means the next source would be Cherangfirstloc.utm[2,], then Cherangfirstloc.utm[3,] and so on. I think this can be done with a for loop or maybe a sapply function. Unfortunately, I don't know how to formulate this.

Could you give me any hints on how to formulate this iterative process? I hope it is ok, if I ask this question in this place. Basically, I just want to know how to iterate through the dataframe. How gdistance and the least cost analysis work is thereby unimportant.

Here is some code that can be used as sample data:

library(gdistance)

r <- raster(nrows=6, ncols=7, xmn=0, xmx=7, ymn=0, ymx=6, crs="+proj=utm 
+units=m")

r[] <- c(2, 2, 1, 1, 5, 5, 5, #creates costgrid
 2, 2, 8, 8, 5, 2, 1,
 7, 1, 1, 8, 2, 2, 2,
 8, 7, 8, 8, 8, 8, 5,
 8, 8, 1, 1, 5, 3, 9,
 8, 1, 1, 2, 5, 3, 9)

T <- transition(r, function(x) 1/mean(x), 8) #creates transition layer of costgrid
T <- geoCorrection(T) #correction

c1 <- c(5.5,1.5) #first source point
c2 <- c(5.5,4) #second source point
c3 <- c(1.5,5.5) #destination

sPath2 <- shortestPath(T, c1, c3, output="SpatialLines") # creates the least cost path

Unfortunately, I did not know how to include c1, c2 and c3 in a Spatialpoints dataframe so that one can iterate through. Hope this still helps.

I would appreciate if you could give me any hints on that. Thanks for your efforts!

解决方案

If you just want to call the shortestPath function with a varying argument, the simpliest solution is to use a for loop like the following. As i am not familiar with this kind of analysis, i don't know what you want to do with the results, so here are two solutions :

In this example you will use the shortest path immediatly, as it will be forgotten at the next step :

for(i in 1:nrow(Cherangfirstloc.utm)) {
  # Computation
  Costpath <- shortestPath(CostTrans, Cherangfirstloc.utm[i,], Cherangfirstloc.utm[132,], output="SpatialLines") 

  ### Here your instructions for a direct use of the result ###
}

In this one all the paths will be stored in a list (it seems that the shortestPath function returns an object so it is the more convenient storage way), each result will be accessible in the results variable, e.g. results[[12]] for the path beginning at the 12th row :

results = list()
for(i in 1:nrow(Cherangfirstloc.utm)) {
  # Computation, storage in a list element
  results[[i]] <- shortestPath(CostTrans, Cherangfirstloc.utm[i,], Cherangfirstloc.utm[132,], output="SpatialLines") 
}

### Here your instructions for a global use of the result ###

这篇关于在R中进行多重最低成本分析的循环或者挖掘功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆