从 R 中的多个 PRISM 栅格之一中提取值
Extracting values from one of multiple PRISM rasters in R
我有一组 100 lat/longs 和 100(有时重复)年。对于每个 lat/longs,我想找到相应年份的年温度(使用 PRISM 数据)。
我已经创建了 for 循环和函数来遍历整个过程,但它们总是卡住。我知道必须有一种更简单的方法来做到这一点(最好避免 for 循环!)。
举个例子,这里有 4 lat/longs 年,我(可笑)试图遍历它们。
library(prism)
library(raster)
locs <- data.frame( lat = c(46.30101, 42.65503, 44.38075, 43.90637), lon = c(-91.764380 -86.201983, -88.951511, -91.081340, -87.896017))
years <- c(1989,1954,2010,1954)
coordinates(locs) <- c('lat', 'lon')
temps <- NULL
for(i in 1:length(years)) {
tryCatch({dir.create(paste0(getwd(),"/",years[i]))}, error=function(e){}) # skip making a new directory for any years that already exist
options(prism.path = paste0(getwd(),"/",years[i]))
get_prism_annual(type = "tmean", years = as.numeric(years[i])) # Get the data
climate_data <- prism_stack(ls_prism_data()) # Stack it
climate_crs <- climate_data@crs@projargs # Get the projection
proj4string(occ.latlong) <- CRS(climate_crs) # Project the climate data's CRS onto the coordinates
temps <- rbind(temps, extract(climate_data, locs[i,]))
}
此循环抛出 NA 重复年份(以上 1954 年)。有更简单的方法吗?!
因为你要下载很多数据,我会把循环分成两部分,先下载。这样也更容易调试您的代码。我也简化了代码是可能的。
years <- c(1989, 1954, 2010, 1954)
library(prism)
for (y in unique(years)) {
dir.create(y, FALSE)
options(prism.path = y)
get_prism_annual(type = "tmean", years = y)
}
现在提取位置。
locs <- data.frame( lat = c(46.30101, 42.65503, 44.38075, 43.90637), lon = c(-91.764380 -86.201983, -88.951511, -91.081340, -87.896017))
# lon/lat order!
locs <- locs[,2:1]
保持简单,先将结果放在列表中
temps <- list()
for(i in 1:length(years)) {
options(prism.path = years[i])
climate_data <- prism_stack(ls_prism_data())
temps[[i]] <- extract(climate_data, locs)
}
合并结果
x <- do.call(cbind, temps)
请注意,多次提取相同的值(同一年)而不是重复使用提取的值可能效率很低。
我有一组 100 lat/longs 和 100(有时重复)年。对于每个 lat/longs,我想找到相应年份的年温度(使用 PRISM 数据)。
我已经创建了 for 循环和函数来遍历整个过程,但它们总是卡住。我知道必须有一种更简单的方法来做到这一点(最好避免 for 循环!)。
举个例子,这里有 4 lat/longs 年,我(可笑)试图遍历它们。
library(prism)
library(raster)
locs <- data.frame( lat = c(46.30101, 42.65503, 44.38075, 43.90637), lon = c(-91.764380 -86.201983, -88.951511, -91.081340, -87.896017))
years <- c(1989,1954,2010,1954)
coordinates(locs) <- c('lat', 'lon')
temps <- NULL
for(i in 1:length(years)) {
tryCatch({dir.create(paste0(getwd(),"/",years[i]))}, error=function(e){}) # skip making a new directory for any years that already exist
options(prism.path = paste0(getwd(),"/",years[i]))
get_prism_annual(type = "tmean", years = as.numeric(years[i])) # Get the data
climate_data <- prism_stack(ls_prism_data()) # Stack it
climate_crs <- climate_data@crs@projargs # Get the projection
proj4string(occ.latlong) <- CRS(climate_crs) # Project the climate data's CRS onto the coordinates
temps <- rbind(temps, extract(climate_data, locs[i,]))
}
此循环抛出 NA 重复年份(以上 1954 年)。有更简单的方法吗?!
因为你要下载很多数据,我会把循环分成两部分,先下载。这样也更容易调试您的代码。我也简化了代码是可能的。
years <- c(1989, 1954, 2010, 1954)
library(prism)
for (y in unique(years)) {
dir.create(y, FALSE)
options(prism.path = y)
get_prism_annual(type = "tmean", years = y)
}
现在提取位置。
locs <- data.frame( lat = c(46.30101, 42.65503, 44.38075, 43.90637), lon = c(-91.764380 -86.201983, -88.951511, -91.081340, -87.896017))
# lon/lat order!
locs <- locs[,2:1]
保持简单,先将结果放在列表中
temps <- list()
for(i in 1:length(years)) {
options(prism.path = years[i])
climate_data <- prism_stack(ls_prism_data())
temps[[i]] <- extract(climate_data, locs)
}
合并结果
x <- do.call(cbind, temps)
请注意,多次提取相同的值(同一年)而不是重复使用提取的值可能效率很低。