Here is a more flexible option for tests like < or ==. The outer function CountPerCol accepts the data frame to be tested, the comparison function, marked with back ticks, and the value to test for. The use of sapply is as follows. sapply takes the data frame to be processed and a function to apply to each column. All sapply does is step through the data frame, passing each column sequentially to the function. I define that function within the call to sapply. The function takes each column as its argument Col. Whatever function was passed in as FUNC is then applied to the Col and the value passed in as Val. This may seem weird to pass in == and apply it as FUNC(Col, Val) but it works. For example, you can test whether 4 == 2+2 like this
`==`(4,2+2)
[1] TRUE
The final step of CountPerCol removes from the answer any columns where the answer is zero.
DF <- data.frame(A=c(1,2,NA,sqrt(-1),4,NA),
B=c(1:6),
Z=c("A","B","C","D","E","F"),
C=c(2,-99,1,2,3,4),
D=c(NA,NA,1,-99,3,4))
Warning message:
In sqrt(-1) : NaNs produced
CountPerCol <- function(Data, FUNC, Val) {
tmp <- sapply(Data, function(Col) sum(FUNC(Col, Val), na.rm = TRUE))
tmp[tmp > 0]
}
CountPerCol(DF, `==`, 2)
A B C
1 1 2
CountPerCol(DF, `<`, 0)
C D
1 1
CountPerCol(DF, `==`, "C")
Z
1