how to count no.of vector values in range with R

 

Count number of vector values in range with R

 

To achieve this functionality we can follow the following approach.

Approach

·         Create vector

·         Set range

·         Iterate through the vector

·         Check for elements that are within the range

·         Add them

·         Display sum

Implementation using this approach is given below.

Example 1:

# declaring a integer point vector

vec <- c(1,12,3,14,-1,-3)

 # specifying the range to check the element in

min_range = -2

max_range = 8

 # computing the size of the vector

size = length(vec)

 # declaring sum =0 as the count of elements in range

sum = 0

 # looping over the vector elements

for(i in 1:size)

 {

     # check if elements lies in the range provided

  if(vec[i]>=min_range && vec[i]<=max_range)

     # incrementing count of sum if condition satisfied

  sum =sum+1

      }

 print ("Sum of elements in range : ")

print (sum)

 

Example 2:

# declaring a integer point vector

vec <- c(1,12,3,14,-1,-3,0.1)

 # specifying the range to check the element in

min_range = -2

max_range = 8

print ("Sum of elements in specified range : ")

 # and operator check if the element is less than

# max range and greater than min range

sum(vec>min_range & vec<max_range)

 

Example 3

# declaring a integer point vector

vec <- c(1,12,3,14,NA,-3,0.1)

 # specifying the range to check the element in

min_range = -2

max_range = 8

print ("Sum of elements in specified range without ignoring NA: ")

 # and operator check if the element is less than

# max range and greater than min range

sum(vec>min_range & vec<max_range)

 print ("Sum of elements in specified range ignoring NA: ")

sum(vec>min_range & vec<max_range,na.rm=TRUE)

 

Count the specific value in a given vector in R

For finding the frequency of a given value two approaches can be employed and both of them are discussed below.

Method 1: Naive method 

We can iterate over the vector in R using a for loop and then check if the element is equivalent to the given value. A counter is maintained, and it is increased by 1, each time the value matches. In case, the element is not present, counter returns a value 0. The time complexity of the approach is O(n), where n is the size of the vector. 

Example:

 

# declaring a vector

vec = c(1,2,3,4,2,1,4,6)

  # printing original vector

print("Original Vectors:")

print(vec)

  # declaring count = 0 

count = 0

  # given value

x = 4

  # looping over vector values

for( i in vec){

        # check if the value is equal to x

    if(vec[i]==x){

                # increment counter by 1 

        count= count + 1

    }

}

  print("Count given value in above vector:")

  # check which values are equal to the given 

# value and calculate sum of it

print (count)

 

Method 2: Using sum() method in R

The sum() method can be used to calculate the summation of the values appearing in the function argument. Here, we specify a logical expression as an argument of the sum() function which calculates the sum of values which are equivalent to the specified value. In case the value is not present, the sum method returns 0 as an output. The time complexity of the approach is O(n), where n is the size of the vector. 

Syntax:

sum(vec == given_val)  

where, vec is the vector and given_val is the given value to check the presence for in the vector. 

Example:

# declaring a vector

vec = c("a","g","a","y","s","a","abcs")

  # printing original vector

print("Original Vectors:")

print(vec)

  print("Count given value in above vector:")

  # check which values are equal to the given 

# value and calculate sum of it

print(sum(vec=="a"))

 

 

Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction