I have a matrix(raster) that I am computing the the mean of each row in this raster as:
library (raster)
r <- raster(nrows=10, ncols=10);r <- setValues(r, 1:ncell(r))
# The x-values will be the mean of each row in the raster:
xvals = rowMeans(as.matrix(r))
What I need is to know how many values were considered when computing the mean for each row (N)? Some pixels may have NA so the number of values will not be the same in each row.
rowSums(!is.na(r)). – whuber Apr 14 '15 at 16:13rowSumssolution in this case, though: the opportunities to capitalize on the type-punning that equatesTRUEwith 1 andFALSEwith 0 are numerous and frequently overlooked. Here it amounts to the difference between counting objects (length(x[is.na(x)]))) and summing indicator functions (rowSums(!is.na(r)))). – whuber Apr 14 '15 at 21:00