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

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 |

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:

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

- Count Number of Characters in String
- Select Data Frame Rows where Column Values are in Range
- Count TRUE Values in Logical Vector
- Count Number of Words in Character String
- R Programming Tutorials

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.

### Statistics Globe Newsletter

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

Hi Jason,

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

Regards,

Joachim

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

Hi Jason,

Thank you for the very kind words!

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

I hope this helps!

Joachim

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!

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