Count Number of Values in Range in R (Example)

 

In this R article you’ll learn how to get the number of observations within a certain range of values.

The page is structured as follows:

Let’s dig in.

 

Introducing Exemplifying Data

Have a look at the following example data:

x <- c(3, 1, 6, 5, 6, 3, 4, 4, 8, 2)    # Create example vector
x                                       # Print example vector
#  [1] 3 1 6 5 6 3 4 4 8 2

Have a look at the previous RStudio console output. It shows that our example data is a numeric vector containing several integer values.

 

Example: Get Number of Observations in Certain Range Using > & <

In this example, I’ll explain how to count the number of values in a particular range.

For this, we can use the larger than (i.e. “>”) and smaller than (i.e. “<”) operators as shown below:

sum(x > 3 & x < 7)                      # Count cases in range
# [1] 5

The RStudio console returns the result: Five elements of our vector lie in the range between 3 and 7.

 

Video & Further Resources

Do you need further info on the R programming code of the present article? Then you could watch the following video of the Statistics Globe YouTube channel. In the video, I explain the R programming codes of this tutorial:

 

 

Besides that, you may have a look at the related tutorials of this website. A selection of articles about ranges and vectors can be found below:

 

On this page, I have illustrated how to count the number of cases in a specific numeric range (i.e. greater than particular value A and smaller than particular value B) in R. Don’t hesitate to let me know in the comments section, if you have additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


6 Comments. Leave new

  • Thank you for this example. I’m wondering how to apply the sum function to make a new row in a data frame that counts if a certain range of values are in say rows 1-10 are less than the value in say row 12. And then apply this for all columns. This is analogous to the “countif” function in excel. Thanks.

    Reply
  • Yes that would be great. Thx so much. I’m learning a lot from you.

    Reply
    • Hi Jason,

      Thank you for the very kind words!

      You could take a conditional sum as demonstrated in the R syntax below:

      set.seed(246234) # Create example data frame
      data <- data.frame(x = round(rnorm(15, 10, 3)))
      data
      #     x
      # 1  14
      # 2  13
      # 3   9
      # 4  12
      # 5   9
      # 6  10
      # 7   8
      # 8   9
      # 9  10
      # 10 10
      # 11  8
      # 12 11
      # 13 14
      # 14  7
      # 15 14
       
      sum(data$x[1:10][data$x[1:10] < data$x[12]]) # Conditional sum
      # [1] 65

      I hope this helps!

      Joachim

      Reply
  • Hello! If I have a dateset with the name of the actors of the movie and the number of the scenes these actors appeared in, what code can I use to discover how many scenes did a precises acotr (i.e Tom) appear in?

    Thank you!

    Reply
    • Hello Carola,

      Library dplyr is usually helpful for such statistical calculations by the group. If I understood your example correctly, the actors will be the grouping variable in your case. Check this link for some examples of the application.

      Regards,
      Cansu

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top