# missing Function in R (2 Examples)

In this R tutorial youâ€™ll learn how to **check whether a value was set as an argument to a function using the missing function**.

Table of contents:

Letâ€™s dive right into the examples:

## Example 1: Basic Application of missing() Function

This section explains the functionality of the missing command based on a very simplified application.

Consider the following user-defined function in R:

my_fun1 <- function(x) { # Create user-defined function return(missing(x)) }

As you can see in the previous R code, our user-defined function requires the specification of an input value x. Within the function, we use the missing command to test whether this value was specified properly.

Letâ€™s test this by applying our user-defined function without an x-value:

my_fun1() # Apply function without x argument # [1] TRUE

Our user-defined function returns the logical indicator TRUE, i.e. the formal argument x is missing.

Letâ€™s specify an x-value within our function:

my_fun1(x = 3) # Apply function with x argument # [1] FALSE

This time, our function returns the logical indicator FALSE, i.e. the x-value is not missing.

So how can we use this in practice? Thatâ€™s what Iâ€™ll explain next!

## Example 2: Advanced Application of missing() Function

Example 2 illustrates a more complex application of the missing function.

Letâ€™s assume that we want to manually create a function that returns a sentence in case the input value is missing, and returns the result of an equation in case the value is not missing.

Such a function could look as the function below:

my_fun2 <- function(x) { # Create user-defined function if(missing(x)) { print("x is missing.") } if(!missing(x)) { x^2 + 5 } }

If we miss out the x-value when we apply this function, the following output is returned:

my_fun2() # Apply function without x argument # [1] "x is missing."

However, if we apply our function to an actual x-value, the result of our equation is returned:

my_fun2(x = 3) # Apply function with x argument # [1] 14

For instance, this can be useful to avoid error messages when an input value for a function is missing.

## Video & Further Resources

Have a look at the following video on my YouTube channel. Iâ€™m demonstrating the R code of this article in the video.

*The YouTube video will be added soon.*

In addition, you might read the related articles on Statistics Globe.

- Return Multiple Objects from User-Defined Function
- Make Object Created within Function Usable Outside
- R Error in Function: Unused Argument
- R Commands List
- R Programming Language

To summarize: In this tutorial you have learned how to **apply the missing function** in R programming. If you have additional questions or comments, please let me know in the comments section below.

### Statistics Globe Newsletter