# How to calculate average value of points located in each pixels in R?

I have a raster(.tif) and point(.shp) layer. To calculate the average value of points located in each pixels:
(1) For each image pixel that include multiple points, the average value of all points located in the pixel were calculated.
(2) For each image pixel that include single point, the point value was chose directly.
How can I calculate it in R ?

• Do you know how to read a raster or a shapefile in? Or do we have to start from zero? Please give some code to show where you are so far. – Spacedman Dec 22 '18 at 17:37
• I do this in ArcGIS before, but it is time consuming and limited to 2GB data in ArcGIS. I get basic grammar and simple spatial analysis about R. So, I have no idea to calculate it so far. – changkx Dec 22 '18 at 17:49

• After learning and practicing R for a while, I found it's easy to solve this problem in R. Similar as the second method in ArcGIS, I could use `rasterize` (`fun=mean`) function to get the average values of points for each grid cell. Then extract the raster values using the `extract` function. – changkx Dec 12 '19 at 23:59