Here are two ways to do it. The difference between them is how NAs are handled. In the first function, NAs are ignored completely, so you get the proportion of non-NA values that are zero. In the second function, the denominator includes NA rows, so you get the proportion of all values that are zero.
df <- data.frame(
x = c(NA, sample(0:1, 99, replace = TRUE)),
y = sample(0:4, 100, replace = TRUE),
z = sample(0:9, 100, replace = TRUE)
)
pct_zero_1 <- function(x) {
mean(x == 0, na.rm = TRUE)
}
lapply(df, pct_zero_1)
#> $x
#> [1] 0.5454545
#>
#> $y
#> [1] 0.13
#>
#> $z
#> [1] 0.08
pct_zero_2 <- function(x) {
sum(x == 0, na.rm = TRUE) / length(x)
}
lapply(df, pct_zero_2)
#> $x
#> [1] 0.54
#>
#> $y
#> [1] 0.13
#>
#> $z
#> [1] 0.08
By the way, it’s better not to post screenshots of code. They can be hard to read, and are invisible to search. If you want to post an error message, you can copy and paste it from the console. To format your code properly, select your pasted code (or console output) and use the little </> button at the top of the posting box.