# 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
#   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
#  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:

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

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

• Jason
September 10, 2022 5:36 pm

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.

• September 19, 2022 11:07 am

Hi Jason,

I’m sorry for the late response, I just came back from holidays. Do you still need help with this?

Regards,
Joachim

• Jason Goldberg
September 19, 2022 11:09 am

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

• September 19, 2022 11:25 am

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]) # Conditional sum
#  65```

I hope this helps!

Joachim

• Carola
December 8, 2022 7:25 pm

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!

• December 13, 2022 3:09 pm