-2

I want to grid a data frame averaging all the points in a grid box only if the number of points are more than 10. I can do the average and count the points separately with

        pts <- tmp[,c("lon","lat","z")]
        coordinates(pts) <- ~ lon+lat
        r <- raster(ncol = 8, nrow = 3)
        extent(r) <- extent(pts)

        tmp_mean <- as.matrix(rasterize(pts, r, pts$sst, fun = mean))
        tmp_count <- as.matrix(rasterize(pts, r, pts$sst, fun = 'count'))

but the only way I found to constrain the mean on the number of observation in each grid cell is using a for loop

        for(k in 1:3){
          for(l in 1:8) tmp_mean[k,l] <- ifelse(tmp_count[k,l] < 10, NA,tmp_mean[k,l])
         }

Thanks

user3910073
  • 511
  • 1
  • 6
  • 23

3 Answers3

0

You can try simple indexing. Something like this

tmp_mean[tmp_count < 10] <- NA
Istrel
  • 2,508
  • 16
  • 22
0

Lacking a minimally reproducible example from the OP I provided a data.table example based on mtcars instead.

 #Set up example:
 library(data.table)   
 mydat <- data.table(mtcars)
 setkey(mydat,  carb, gear)  

Now it's a simple data.table operation: The call below only returns values where there are at least 3 observations for a given combination of carb and gear

mydat <-  mydat[,count:=.N, by=.(carb, gear)][count>3,] 

With data not having at least 3 observations out of the dataset, mean() operations can now be performed. Below, all other columns' means are calculated, grouping by carb and gear

mydat[,lapply( .SD, mean), by=.(carb, gear)][,count := NA]

Results in:

   carb gear   mpg cyl   disp    hp   drat      wt   qsec  vs  am
1:    1    4 29.10   4  84.20  72.5 4.0575 2.07250 19.220 1.0 1.0
2:    2    3 17.15   8 345.50 162.5 3.0350 3.56000 17.060 0.0 0.0
3:    2    4 24.75   4 121.05  79.5 4.1625 2.68375 20.005 1.0 0.5
4:    4    3 12.62   8 416.40 228.0 3.2200 4.68580 16.894 0.0 0.0
5:    4    4 19.75   6 163.80 116.5 3.9100 3.09375 17.670 0.5 0.5
Serban Tanasa
  • 3,592
  • 2
  • 23
  • 45
0

You can do the entire operation within the rasterize command, by extending the definition of the function. I have invented a data file to demonstrate it

pts<-data.frame(lon=c(52,52,52,53,54),
                lat=c(3,3,3,4,5),
                z=c(1.1,2.9,3.2,4.0,5.1))
coordinates(pts) <- ~ lon+lat
r <- raster(ncol = 8, nrow = 3)
extent(r) <- extent(pts)
tt<-rasterize(pts,r,field="z",
              fun=function(x,...)ifelse(length(x)>2,mean(x),NA))

This gives you the desired raster at once. Check it by

ttm<-as.matrix(tt)
ttm

which yields

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]   NA   NA   NA   NA   NA   NA   NA   NA
[2,]   NA   NA   NA   NA   NA   NA   NA   NA
[3,]  2.4   NA   NA   NA   NA   NA   NA   NA
Peter Herman
  • 162
  • 4