You want to read "Introduction to R". Your answer here is simple.
sum( x < 0 )
which works thanks to vectorization. The expression x < 0
returns a vector of Boolean elements that sum()
can work on (by converting Boolean values ββto standard 0/1 values).
source share