I am working on a raster image which is of 120m resolution. I need to make the raster divided into grids or windows of size 2 by 2 and then take the mean of the values in the grid and replace the cells by their mean by which the resolution will change to 240m (2 by 2). There should not be overlapping in the grids or window. The extremes can be padded to get the mean if the raster is having an odd number of rows or columns. The NaN values can be ignored and the results should be rasters.

How can I achieve this using R? I am quite new to it. I have converted it into a matrix and run a loop through it, Focal and sliding window and rollApply do not solve my problem.