How to use range() in R?

Filed Under: R Programming
Range In R

Let’s learn to use range() in R returns the minimum and maximum values present in a vector or a data frame as well.

In this tutorial, we are going to see how the range() function works.

Basic Syntax of range() in R

range(x, na.rm=FALSE)

Where:

  • x = vector or a data frame.
  • na.rm = removes the NA values if mentioned TURE and considers the NA values if mentioned FALSE.

Min and max values using range() in R

R is an efficient language for data analysis. We have already discussed how to find the min and max values using functions min() and max() in R. In this tutorial, we are using range() function to find the minimum and maximum values.

Finding the min and max values present in a vector or a data frame has numerous real-world usage. These values will be really helpful in the process of visualization and the analysis to get better accuracy in the result.


Finding Min and Max Values in a Vector using range() in R

In this section, we are going to find the range of a vector. Let’s see how it works.

#creates a vector of numerical values
df<- c(13,65,123,7,3,87,34,98)

#returns the range of the vector 
range(df)

Output = 3, 123

Note: In the output of the range() function, the first value represents the minimum values and the second value represents the maximum value present in the vector.


Finding the range of a vector having NA values

Some times you have to deal with data having NA values. When the range() function encounters the NA values it returns NA instead of min and max values of the vector.

You can see how range returns the NA values in the below output.

#creates a vector having NA values
df<- c(45,76,89,NA,67,34,NA,56,78)

#returns the range of the vector
range(df)

Output = NA NA

Now to avoid this we use na.rm function which eliminates the NA values as shown below.

#creates a vector with NA values
df<- c(45,76,89,NA,67,34,NA,56,78)

#returns the range of the vector by eliminating the NA values 
range(df,na.rm = T)

Output = 34 89


Finding the range of a character vector

Till now, we dealt with the range of numerical values. Now let’s find the range of a character vector.

Let’s see how it works.

#creates a character vector
df<- c('Hayato','Antonio','Randrew','Jota','Adam','Commander')

#returns the range of the vector 
range(df)

Output = “Adam” “Randrew”


Finding the range of a data frame

In this section, let’s apply range() function to a data frame to get max and min values.

For this purpose, let’s use ‘Quakes’ datasets which is provided by default in R studio.

Quakes

Let’s find the range of the values present in the column ‘depth’.

#returns the range of the values present in column 'depth'
range(quakes$depth)

Output = 40 680

Range of the values present in the column ‘magnitude’.

#returns the range of the values present in the column 'mag'.
range(quakes$mag)

Output = 4.0 6.4


Wrapping up

The min and max values in a data frame will be very helpful in the real-world data analysis. Using range() function, you can easily find the minimum and maximum values present in a vector or a data frame as well.

We can also eliminate the NA values present in a vector or a data frame using na.rm function.

The min() and max() functions in R also capable of doing the same job as range() function. Both functions are very similar but effective.

That’s all for now. Stay connected for more R programming articles. Happy learning!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages