重复定义大量多边形栅格边界的六边形多边形过程
Repeating a process of hexagon polygons that define raster boundaries for a large set of polygons
抱歉。这是我第一次使用Whosebug,所以我不习惯发布问题。这是我正在编码的内容
library(raster)
library(landscapemetrics)
library(landscapetools)
# Add raster data for 2000
hex1_2000<-raster('2000_hex1.tif')
hex2_2000<-raster('2000_hex2.tif')
hex3_2000<-raster('2000_hex3.tif')
hex4_2000<-raster('2000_hex4.tif')
...
hex23_2000<-('2000_hex4.tif')
# Add raster data for 2010
hex1_2010<-raster('2010_hex1.tif')
hex2_2010<-raster('2010_hex2.tif')
hex3_2010<-raster('2010_hex3.tif')
hex4_2010<-raster('2010_hex4.tif')
...
hex23_2010<-('2000_hex4.tif')
#Create data frame as table
hex1 = data.frame(
lc00 = values(hex1_2000),
lc10 = values(hex1_2010))
hex2 = data.frame(
lc00 = values(hex2_2000),
lc10 = values(hex2_2010))
hex3 = data.frame(
lc00 = values(hex3_2000),
lc10 = values(hex3_2010))
hex4 = data.frame(
lc00 = values(hex4_2000),
lc10 = values(hex4_2010))
...
hex23 = data.frame(
lc00 = values(hex23_2000),
lc10 = values(hex23_2010))
...
hex1 = table(hex1[,c('lc00','lc10')])
hex2 = table(hex2[,c('lc00','lc10')])
hex3 = table(hex3[,c('lc00','lc10')])
hex4 = table(hex4[,c('lc00','lc10')])
...
hex23 = table(hex23[,c('lc00','lc10')])
#Define crosstabulation matrix
Hex1_Trans = as.matrix(hex1 / rowSums(hex1))
write.csv(Hex1_Trans, 'hex1Trans.csv')
Hex2_Trans = as.matrix(hex2 / rowSums(hex2))
write.csv(Hex2_Trans, 'hex2Trans.csv')
Hex3_Trans = as.matrix(hex3 / rowSums(hex3))
write.csv(Hex3_Trans, 'hex3Trans.csv')
Hex4_Trans = as.matrix(hex2 / rowSums(hex4))
write.csv(Hex4_Trans, 'hex2Trans.csv')
...
Hex23_Trans = as.matrix(hex23 / rowSums(hex23))
write.csv(Hex23_Trans, 'hex23Trans.csv')
如您所见,我在无数次重复相同过程的实例中。我很高兴知道如何使这段代码更简单、更优雅。我的编码总是这样,我发现这显然非常低效。感谢大家的帮助。
非常简单的解决方案,试试 assign()。此代码来自 Data Camp 的文档页面。
for(i in 1:6) {
#-- Create objects 'r.1', 'r.2', ... 'r.6' --
nam <- paste("r", i, sep = ".")
assign(nam, 1:i)
}
ls(pattern = "^r..$")
这是页面的link。查看 'Examples' 部分。 rdocumentation.org/packages/base/versions/3.6.1/topics/assign
也许像下面这样的东西可以满足问题的要求。
这是重复使用 lapply
读取数据文件和 table
所需的列。
hexnames <- list.files(pattern = "2000_hex\d+\.tif")
hex_list <- lapply(hexnames, raster)
names(hex_list) <- paste0("hex", seq_along(hex_list), "_2000")
hex_table <- lapply(hex_list, function(X) table(X[, c('lc00','lc10')]))
这是一个不完整的草稿,说明了如何使用 Map
同时遍历 2000 年和 2010 年的数据。
fn_y2000 <- c("2000_hex1.tif", "2000_hex2.tif", "2000_hex3.tif")
fn_y2010 <- c("2010_hex1.tif", "2010_hex2.tif", "2010_hex3.tif")
lst <- Map(
function(x1, x2) {
hex1 <- raster(x1)
hex2 <- raster(x2)
tbl <- table(values(hex1), values(hex2))
#... Normalise and write output
},
fn_y2000, fn_y2010)
return 对象是 list
。
抱歉。这是我第一次使用Whosebug,所以我不习惯发布问题。这是我正在编码的内容
library(raster)
library(landscapemetrics)
library(landscapetools)
# Add raster data for 2000
hex1_2000<-raster('2000_hex1.tif')
hex2_2000<-raster('2000_hex2.tif')
hex3_2000<-raster('2000_hex3.tif')
hex4_2000<-raster('2000_hex4.tif')
...
hex23_2000<-('2000_hex4.tif')
# Add raster data for 2010
hex1_2010<-raster('2010_hex1.tif')
hex2_2010<-raster('2010_hex2.tif')
hex3_2010<-raster('2010_hex3.tif')
hex4_2010<-raster('2010_hex4.tif')
...
hex23_2010<-('2000_hex4.tif')
#Create data frame as table
hex1 = data.frame(
lc00 = values(hex1_2000),
lc10 = values(hex1_2010))
hex2 = data.frame(
lc00 = values(hex2_2000),
lc10 = values(hex2_2010))
hex3 = data.frame(
lc00 = values(hex3_2000),
lc10 = values(hex3_2010))
hex4 = data.frame(
lc00 = values(hex4_2000),
lc10 = values(hex4_2010))
...
hex23 = data.frame(
lc00 = values(hex23_2000),
lc10 = values(hex23_2010))
...
hex1 = table(hex1[,c('lc00','lc10')])
hex2 = table(hex2[,c('lc00','lc10')])
hex3 = table(hex3[,c('lc00','lc10')])
hex4 = table(hex4[,c('lc00','lc10')])
...
hex23 = table(hex23[,c('lc00','lc10')])
#Define crosstabulation matrix
Hex1_Trans = as.matrix(hex1 / rowSums(hex1))
write.csv(Hex1_Trans, 'hex1Trans.csv')
Hex2_Trans = as.matrix(hex2 / rowSums(hex2))
write.csv(Hex2_Trans, 'hex2Trans.csv')
Hex3_Trans = as.matrix(hex3 / rowSums(hex3))
write.csv(Hex3_Trans, 'hex3Trans.csv')
Hex4_Trans = as.matrix(hex2 / rowSums(hex4))
write.csv(Hex4_Trans, 'hex2Trans.csv')
...
Hex23_Trans = as.matrix(hex23 / rowSums(hex23))
write.csv(Hex23_Trans, 'hex23Trans.csv')
如您所见,我在无数次重复相同过程的实例中。我很高兴知道如何使这段代码更简单、更优雅。我的编码总是这样,我发现这显然非常低效。感谢大家的帮助。
非常简单的解决方案,试试 assign()。此代码来自 Data Camp 的文档页面。
for(i in 1:6) {
#-- Create objects 'r.1', 'r.2', ... 'r.6' --
nam <- paste("r", i, sep = ".")
assign(nam, 1:i)
}
ls(pattern = "^r..$")
这是页面的link。查看 'Examples' 部分。 rdocumentation.org/packages/base/versions/3.6.1/topics/assign
也许像下面这样的东西可以满足问题的要求。
这是重复使用 lapply
读取数据文件和 table
所需的列。
hexnames <- list.files(pattern = "2000_hex\d+\.tif")
hex_list <- lapply(hexnames, raster)
names(hex_list) <- paste0("hex", seq_along(hex_list), "_2000")
hex_table <- lapply(hex_list, function(X) table(X[, c('lc00','lc10')]))
这是一个不完整的草稿,说明了如何使用 Map
同时遍历 2000 年和 2010 年的数据。
fn_y2000 <- c("2000_hex1.tif", "2000_hex2.tif", "2000_hex3.tif")
fn_y2010 <- c("2010_hex1.tif", "2010_hex2.tif", "2010_hex3.tif")
lst <- Map(
function(x1, x2) {
hex1 <- raster(x1)
hex2 <- raster(x2)
tbl <- table(values(hex1), values(hex2))
#... Normalise and write output
},
fn_y2000, fn_y2010)
return 对象是 list
。