stopifnot Function in R (2 Examples) | Ensure the Truth of Expressions

 

In this article you’ll learn how to check whether all expressions are TRUE using the stopifnot function in the R programming language.

The article will consist of two examples for the usage of the stopifnot function. More precisely, the content of the post looks as follows:

Let’s jump right to the examples.

 

Exemplifying Expressions

The following data will be used as basement for this tutorial:

expr_1 <- 5 < 7                      # Create first expression
expr_1                               # Print output of first expression
# [1] TRUE
expr_2 <- 2 * 3 > 4                  # Create second expression
expr_2                               # Print output of second expression
# [1] TRUE
expr_3 <- "a" == "A"                 # Create third expression
expr_3                               # Print output of third expression
# [1] FALSE

Have a look at the previous R code and their outputs in the RStudio console. It shows three different expressions that are either TRUE or FALSE.

 

Example 1: Apply stopifnot() Function with Only TRUE Expressions

In this example, I’ll demonstrate how to use the stopifnot function to evaluate the truth of multiple expressions.

In the present example, we’ll only evaluate our expressions expr_1 and expr_2:

stopifnot(expr_1, expr_2)            # Apply stopifnot function

Nothing has happened after executing the previous R code. The reason is that both expressions are TRUE.

 

Example 2: Apply stopifnot() Function with FALSE Expressions

Example 2 demonstrates how to apply the stopifnot function when some expressions are FALSE.

For this, we’ll use all of our three expressions:

stopifnot(expr_1, expr_2, expr_3)    # Apply stopifnot function
# Error: expr_3 is not TRUE

As you can see, the previous R code has returned the error message “Error: expr_3 is not TRUE” to the RStudio console, i.e. our third expression is FALSE.

 

Video & Further Resources

I have recently released a video on my YouTube channel, which explains the R programming code of this tutorial. You can find the video below:

 

The YouTube video will be added soon.

 

Besides the video, you may want to have a look at some of the related tutorials on this website. You can find some interesting articles here.

 

To summarize: On this page you have learned how to apply the stopifnot function to ensure the truth of expressions in R programming. Don’t hesitate to let me know in the comments below, if you have further questions. Furthermore, please subscribe to my email newsletter in order to receive regular updates on new articles.

 

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.


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.

Menu
Top