# [R] Count non-zero values in excluding NA Values

Sun Oct 29 12:38:31 CET 2017

```Hello,

Your attachment didn't came through, R-Help strips off most types of
files, including CSV.
Anyway, the following will do what I understand of your question. Tested
with a fake dataset.

set.seed(3026)    # make the results reproducible
data <- matrix(1:100, ncol = 10)
data[sample(100, 15)] <- 0
data[sample(100, 10)] <- NA
data <- as.data.frame(data)

zero <- sapply(data, function(x) sum(x == 0, na.rm = TRUE))
na <- sapply(data, function(x) sum(is.na(x)))
totals <- nrow(data) - zero - na  # totals non zero per column
grand_total <- sum(totals)        # total non zero

totals
# V1  V2  V3  V4  V5  V6  V7  V8  V9 V10
#  6   8   8   8   8   7   7   8   6  10

grand_total
# 76

# another way
prod(dim(data)) - sum(zero + na)
# 76

Hope this helps,

Em 29-10-2017 10:25, Engin YILMAZ escreveu:
> Dear R Staff
>
> You can see my data.csv file in the annex.
>
> I try to count non-zero values in dataset but I need to exclude NA in this
> calculation
>
> My code is very long (following),
> How can I write this code more efficiently and shortly?
>
> ## [NA_Count] - Find NA values
>
> data.na =sapply(data[,3:ncol(data)], function(c) sum(length(which(is.na
> (c)))))
>
>
> ## [Zero] - Find zero values
>
> data.z=apply(data[,3:ncol(data)], 2, function(c) sum(c==0))
>
>
> ## [Non-Zero] - Find non-zero values
>
> data.nz=nrow(data[,3:ncol(data)])- (data.na+data.z)
>
>
> Sincerely
> Engin YILMAZ
>
> Virus-free.
> www.avast.com