如何计算数据框中一列(逻辑值)的所有 "TRUE" 值?

How to count up all "TRUE" values from a column (of logical values) in a Data Frame?

这是 R 语言。逻辑值在第 5 列中。但是,它似乎并没有遍历该列的所有值。最简单的方法是什么?

count = 0

if (students[ , 5] == "TRUE") {
  count = count + 1
}

num_better_at_math = count
set.seed(1L)
df1 <- data.frame(a1 = sample(c(TRUE, FALSE), 5, TRUE),
                  a2 = sample(c(TRUE, FALSE), 5, TRUE),
                  a3 = sample(c(TRUE, FALSE), 5, TRUE),
                  a4 = sample(c(TRUE, FALSE), 5, TRUE),
                  a5 = sample(c(TRUE, FALSE), 5, TRUE))

colSums(df1)
# a1 a2 a3 a4 a5 
# 3  1  3  2  3

sum(df1[, 5])
# [1] 3