T-T - 10 months ago 39

R Question

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`

`0.00`

`# 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)))`

`length(which(x !=0))`

Answer Source

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
```