Search code examples
rvectorabsolute-value

In R, How to count the total number of items in a vector greater than the absolute value of 1


I want to count the total number of items in a vector greater than (>) the absolute value of 1.

vec <- c(5,3,-7,0,0,0,-1,0,3,1,-3,4,7)

the result should exclude 0, 1 and -1 in the count and return the total count of 7

attempt

sum(vec >abs(1))
# this returns '5' instead of '7'

Thanks


Solution

  • The abs should be on the 'vec' and not on 1

    sum(abs(vec) > 1)
    [1] 7