minimum value in an r vector of integers 1, 2, 3, and 4

Get the Minimum Value in an R Vector

A vector is a one-dimensional data structure used to store data of the same type in R. Numeric vectors are commonly used to store a sequence of numbers. In this tutorial, we will look at how to get the minimum value in an R vector with the help of some examples.

How to get the min value in a vector in R?

minimum value in an r vector of integers 1, 2, 3, and 4

You can use the R min() function to get the minimum value in a vector. Pass the vector as an argument to the function. The following is the syntax –

# get the max value in a vector
max(x, na.rm=FALSE)

The following are the arguments that you can give to the min() function in R.

  • x – The vector for which you want to compute the min value.
  • na.rm – (Optional argument) Indicates whether to remove missing values before computing the minimum. It is FALSE by default.

The function returns the min value in the passed vector.

Examples

Let’s look at some examples of using the above method to get the min value in a vector.

Minimum value in a numeric vector

Let’s create a vector of numbers (and without any NA values) and apply the min() function.

# create a vector of numbers
vec <- c(3, 2, 5, 4)
# min value in vector
min(vec)

Output:

2

We get 2 as the max value in the above vector, which is the correct answer.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

What would happen if there are some NA present values in the vector?

Let’s find out.

First, we will create a vector with some NA values and then apply the min() function without any additional arguments.

# create a vector with NA values
vec <- c(3, 2, NA, 5, NA, 4, NA)
# min value in the vector
min(vec)

Output:

<NA>

You can see that we get NA as the output. This is because comparing a value with NA results in an NA in R.

Minimum in a vector with NA values

You can pass TRUE to the na.rm parameter of the max() function to exclude missing values when computing the minimum value in a vector.

# create a vector with NA values
vec <- c(3, 2, NA, 5, NA, 4, NA)
# min value in the vector
min(vec, na.rm=TRUE)

Output:

2

Now we get the minimum value in the above vector as 2.

Minimum value in a character vector

The min() function also works similarly on a vector of characters. For example, let’s see what we get on applying the min() function on a vector of characters.

# create a vector of chracters
vec <- c("a", "b", "c", "d")
# min value in the vector
min(vec)

Output:

'a'

Here, we get ‘a’ as the minimum value in the above vector which contains the values ‘a’, ‘b’, ‘c’, and ‘d’.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top