[R] Frequency: NA values returns zero
Muhammad Rahiz
muhammad.rahiz at ouce.ox.ac.uk
Tue Feb 23 16:31:51 CET 2010
Hi all,
I created the following script to make a frequency count for
multi-dimenstioanl array. There is no problem with the results except
for the NA values which returns 0.
Now, I don't want NA to return 0 but to return as NA. This is because
I'm dealing with gridded data in which the NODATA_VALUE = NA. If NA
returns 0, it will create confusion when I create color maps.
Any suggestions how I should modify the script?
Thanks,
Muhammad
rm(list=ls()) # Cleans workspace
# ---------- ORIGINAL INPUT PARAMETERS---------- #
# x0 <- dir(pattern="txt") ## List files
# fl <- length(x0) # File count
# arr <- array(0, dim=c(2,2,fl)) ## Create dummy array
# x1 <- arr
# for (a in 1:fl){
# x1[,,a] <- as.matrix(read.table(x0[a]))} # Read files
# ---------- START ---------- #
x1 <- array(c(1,2,3,NA),dim=c(2,2,5))
fl <- 5
# ---------- PROCESS ---------- #
count <- function(x) # Create count() function
{
length(x[!is.na(x) & x < 4]) ## Two conditions specified.
Add more if necessary
}
x2 <- apply(x1, c(1,2), count) # Frequency count
x3 <- round((x2 / fl) *100, digits=2) # Express frequency count as
percentage of total count
More information about the R-help
mailing list