简体   繁体   English

为什么我无法使用 R 获得 state 的 worldclim 数据(平均温度和精确度)?

[英]Why can I not get worldclim data (avg temp and prec) for the state which I live at all using R?

I'd like to get the average temperature and precipitation for the state of Ceara in Brazil;我想得到巴西 Ceara 的 state 的平均温度和降水量; I made a cropped area on the map as:我在 map 上做了一个裁剪区域:

在此处输入图像描述

and I used the center lat/lon as lat=-5.49839 and lon=-39.32062 I got the caps of latitude and longitude as latinicial=-7.24614 (minimum latitude), latfinal=-3.76140 (maximum latitude), longinicial=-40.38084 (minimum longitude) and longfinal=-38.77385 (maximum longitude) then I've simulated a uniformly distributed temperature for both latitude and longitude which lies in their maxima and minima.我使用中心纬度/经度作为lat=-5.49839lon=-39.32062我得到纬度和经度的上限为latinicial=-7.24614 (最小纬度), latfinal=-3.76140 (最大纬度), longinicial=-40.38084 (最小经度)和longfinal=-38.77385 (最大经度)然后我模拟了纬度和经度的均匀分布温度,位于它们的最大值和最小值。

My code is given as follows:我的代码如下:

library(raster)
library(sp)
library(rgeos)
library(rgdal)
library(dismo)
library(rgdal)
library(sf)
d=getData('worldclim',lat=-5.49839,lon=-39.32062,res=0.5,var='bio')
latinicial=-7.24614
latfinal=-3.76140
longinicial=-40.38084
longfinal=-38.77385
latitude=runif(100,latinicial,latfinal)
longitude=runif(100,longinicial,longfinal)
coord=data.frame(latitude,longitude)
points = SpatialPoints(coord, proj4string = d@crs)
d <- d[[c(1,12)]]
names(d)=c("Temp","Prec")
extract(d,coord)

But when I run it, I got NA values for all rows even though I'm showing you only 4 rows:但是当我运行它时,我得到了所有行的 NA 值,即使我只显示了 4 行:

在此处输入图像描述

So, I'd like to know what happened to it.所以,我想知道它发生了什么。 Why do I get NA values?为什么我会得到 NA 值?

The problem is with the order of longitude and latitude in coords .问题在于coords中经度和纬度的顺序。 When you put coords into SpatialPoints , it expects the order to be longitude then latitude , but you have it reversed.当您将coords放入SpatialPoints时,它期望顺序是longitude然后latitude ,但是您将其颠倒了。 Once you fix that, then it will extract the data correctly.一旦你解决了这个问题,它就会正确地提取数据。 All the code above coord works fine. coord上面的所有代码都可以正常工作。 Also, if you are going to run this code multiple times, then I would recommend using set.seed .另外,如果您要多次运行此代码,那么我建议您使用set.seed This will allow you to get the same values every time when you run the runif statements.这将允许您在每次运行runif语句时获得相同的值。

library(raster)
library(sp)

set.seed(243)
d = getData(
  'worldclim',
  lat = -5.49839,
  lon = -39.32062,
  res = 0.5,
  var = 'bio'
)
latinicial = -7.24614
latfinal = -3.76140
longinicial = -40.38084
longfinal = -38.77385
latitude = runif(100, latinicial, latfinal)
longitude = runif(100, longinicial, longfinal)
coord = data.frame(longitude, latitude)
points = SpatialPoints(coord, proj4string = d@crs)
d <- d[[c(1, 12)]]
names(d) = c("Temp", "Prec")
extract(d, coord)

Output Output

head()

       Temp Prec
  [1,]  239  655
  [2,]  267  832
  [3,]  256  541
  [4,]  269  740
  [5,]  242  784
  [6,]  233  981

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 R 为单个国家/地区提取 WORLDCLIM 数据 - Extract WORLDCLIM data using R for a single country 使用不带rgdal软件包的R提取Worldclim数据 - Extract Worldclim Data Using R without rgdal package 在 R 中提取 WorldClim 数据但缺少几个月 - Extracting WorldClim data in R but some months are missing 我如何使用kohonen找出哪个数据记录进入R中的哪个集群 - How can I find out which data record goes into which cluster in R using kohonen and means 如何找到所有子字符串,它们在两个字符串之间,包括使用R的换行符? - How can I find all substrings, which are between two strings including a line-break using R? 如何获得当前平均 CPC使用R(最好是R)或Python出价对Google AdWords API中的关键字出价吗? - How do I get the current Avg. CPC bid for a keyword in Google AdWords API using R (preferably) or Python? 如何从R Revolution Enterprise中分离的大文件中获取所有数据? - How can I get all the data from separated large files in R Revolution Enterprise? 如何使用 R 在条形图中添加没有数据的 bin? - How can I add bins which has no data in bar chart using R? 如何获得R(使用哪个程序包)中的重新捕获概率? - How can I get the recapture probabilities in R (which package to use) ? R:我无法使用do.call将所有data.frame列转换为因子 - R: I can't convert all my data.frame columns in factors using do.call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM