I have gridded data on temperature, from which I want to extract data for specific countries. Put it in another way, I want to kind of assign country names to the coordinates (long and lat). The way I am trying to accomplish it:
library(gapminder)
df = map_data("world") %>% select(long, lat, region)
r = rasterFromXYZ(df)
If I got a raster from df
, then I could adjust the resolution and extent to merge it with my data on temperature. Then, I could convert it to a dataframe and subset by country. But when I try to raster df
, I get an error message:
"Error in rasterFromXYZ(df) : x cell sizes are not regular".
How can I make it regular? Or is there any other easy way to assign country names to the coordinates?