T-T T-T - 5 days ago 5
R Question

Count number of rows for each column in a data frame/table

This is a simple question, but I'm not sure what I did wrong.

df1 <- data.frame(
A = c(NA, 0.57, 0.60, 0.51),
B = c(NA, 0, 0.09,0.19),
C = c(0., 0.05, 0.07, 0.05),
D = c(0.23, 0.26, 0.23, 0.26)
)
View(df1)
# A B C D
# 1 NA NA 0.00 0.23
# 2 0.57 0.00 0.05 0.26
# 3 0.60 0.09 0.07 0.23
# 4 0.51 0.19 0.05 0.26


I need to get the sum of each column and divide by the number of rows in each column that are neither
NA
nor
0.00
The result should be:

# A B C D
# 1 0.56 0.14 0.06 0.25


I tried
df2 <- apply(df1, 2, function(x) colSums(df1, na.rm = T) /length(which(x !=0)))
but it returns a 4x4 matrix. I suspect the problem is with
length(which(x !=0))
. Please advice.

Answer

We can do

round(colSums(df1, na.rm = TRUE)/colSums(df1!=0, na.rm=TRUE), 2)
#   A    B    C    D 
# 0.56 0.14 0.06 0.24