本文介绍了在R中进行多重最低成本分析的循环或者挖掘功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用软件包gdistance进行最低成本分析。 这个想法是通过具有定义的成本值的costgrid(raster)来确定从目的地点到源的路径;该路径从而避免了高成本的像素,并且偏好具有低成本值的像素。使用我的数据为我工作的代码是: 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 costgridT <- geoCorrection(T) #correctionc1 <- c(5.5,1.5) #first source pointc2 <- c(5.5,4) #second source pointc3 <- c(1.5,5.5) #destinationsPath2 <- shortestPath(T, c1, c3, output="SpatialLines") # creates the least cost pathUnfortunately, 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中进行多重最低成本分析的循环或者挖掘功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-30 02:11