Reputation: 23
I'd like to create a raster of 480*480 resolution. The cell value should be the mean intensity value of all points falling inside the same cell. For example, the first point (138.42133 457.4437) should go to cell (139, 458) because of round up. If no other points located in the same cell, the cell value is then 160, otherwise the mean.
I have thousands of points that have XY and intensity values as the sample below (sorry I don't know how to share the entire data here...). I think this could be done easily in ArcMap using spatial join but how could it be done in R? Thanks a lot.
> sample
imx imy Intensity
1 138.42133 457.4437 160
2 131.22405 453.9341 133
3 115.80057 445.3643 4
4 122.90073 449.4941 33
5 129.89724 453.2543 3
6 145.50787 425.8380 90
7 143.82309 459.8863 43
8 156.35612 439.8159 13
9 152.25119 463.3834 42
10 159.86740 466.2272 61
Upvotes: 0
Views: 302
Reputation: 174546
You can get the average of each cell by using ave
on the floor
of the x, y values. Then you can convert the data frame to a raster using rasterFromXYZ
from raster
:
sample$Intensity <- ave(sample$Intensity, floor(sample$imx), floor(sample$imy)
sample$imx <- floor(sample$imx)
sample$imy <- floor(sample$imy)
raster::rasterFromXYZ(sample)
#> class : RasterLayer
#> dimensions : 42, 45, 1890 (nrow, ncol, ncell)
#> resolution : 1, 1 (x, y)
#> extent : 114.5, 159.5, 424.5, 466.5 (xmin, xmax, ymin, ymax)
#> crs : NA
#> source : memory
#> names : Intensity
#> values : 3, 160 (min, max)
Upvotes: 0