Count number of vector values in range with R
There are also the %<% and %<=% comparison operators in the TeachingDemos package which allow you to do this like:
sum( 2 %<% x %<% 5 )sum( 2 %<=% x %<=% 5 )
which gives the same results as:
sum( 2 < x & x < 5 )sum( 2 <= x & x <= 5 )
Which is better is probably more a matter of personal preference.
Use which:
set.seed(1) x <- sample(10, 50, replace = TRUE) length(which(x > 3 & x < 5)) # [1] 6