How to create a raster from a data frame in r?

后端 未结 2 1959
醉话见心
醉话见心 2020-12-08 20:26

I have a data frame in which values (l) are specified for Cartesian coordinates (x, y) as in the following minimal working example.

set.seed(2013)
df <- d         


        
2条回答
  •  我在风中等你
    2020-12-08 20:50

    An easier solution exists as

     library(raster)
     dfr <- rasterFromXYZ(df)  #Convert first two columns as lon-lat and third as value                
     plot(dfr)
     dfr                  
     class       : RasterLayer 
     dimensions  : 2, 2, 4  (nrow, ncol, ncell)
     resolution  : 1, 1  (x, y)
     extent      : -0.5, 1.5, -0.5, 1.5  (xmin, xmax, ymin, ymax)
     coord. ref. : NA 
     data source : in memory
     names       : l 
     values      : -2.311813, 0.921186  (min, max)
    

    Further, you may specify the CRS string. Detailed discussion is available here.

提交回复
热议问题