Open In App

How to Find the Range in R?

Last Updated : 05 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to find the Range in R Programming Language.

The range can be defined as the difference between the maximum and minimum elements in the given data, the data can be a vector or a dataframe. So we can define the range as the difference between maximum_value – minimum_value

Method 1: Find range in a vector using min and max functions

We can find the range by performing the difference between the minimum value in the vector and the maximum value in the given vector. We can find the maximum value using the max() function and the minimum value by using the min() function.

Syntax:

max(vector)-min(vector)

If a vector contains NA values then we should use the na.rm function to exclude NA values

Example:

R




# create vector
data = c(12, 45, NA, NA, 67, 23, 45, 78, NA, 89)
 
# display
print(data)
 
# find range
print(max(data, na.rm=TRUE)-min(data, na.rm=TRUE))


Output:

[1] 12 45 NA NA 67 23 45 78 NA 89
[1] 77

Method 2: Get range in the dataframe column

We can get the range in a particular column in the dataframe. Similarly, like a vector, we can get the maximum value from a column using the max function excluding NA values and we can get the minimum value from a column using the min function excluding NA values and finally, we can find the difference.

Syntax:

max(dataframe$column_name,na.rm=TRUE)-min(dataframe$column_name,na.rm=TRUE)

where

  • dataframe is the input dataframe
  • column_name is the column in the dataframe

Example:

R




# create dataframe
data = data.frame(column1=c(12, 45, NA, NA, 67, 23, 45, 78, NA, 89),
                  column2=c(34, 41, NA, NA, 27, 23, 55, 78, NA, 73))
 
# display
print(data)
 
# find range in column1
print(max(data$column1, na.rm=TRUE)-min(data$column1, na.rm=TRUE))
 
# find range in column2
print(max(data$column2, na.rm=TRUE)-min(data$column2, na.rm=TRUE))


Output:

   column1 column2
1       12      34
2       45      41
3       NA      NA
4       NA      NA
5       67      27
6       23      23
7       45      55
8       78      78
9       NA      NA
10      89      73

[1] 77

[1] 55

Method 3: Get range from entire dataframe

we can get a range from the entire dataframe. Here we are getting the maximum value and minimum value by directly using min and max functions in the dataframe, then subtracting the minimum value from the maximum value.

Syntax:

max(dataframe,na.rm=TRUE)-min(dataframe,na.rm=TRUE)

Example:

R




# create dataframe
data = data.frame(column1=c(12, 45, NA, NA, 67, 23, 45, 78, NA, 89),
                  column2=c(34, 41, NA, NA, 27, 23, 55, 78, NA, 73))
 
# display
print(data)
 
 
# find range in entire dataframe
print(max(data, na.rm=TRUE)-min(data, na.rm=TRUE))


Output:

   column1 column2
1       12      34
2       45      41
3       NA      NA
4       NA      NA
5       67      27
6       23      23
7       45      55
8       78      78
9       NA      NA
10      89      73

[1] 77

Method 4: Using range() function

We can use the range() function to get maximum and minimum values. Here we are calculating range values

Syntax:

range(vector/dataframe)

Example;

R




# create vector
data = c(12, 45, NA, NA, 67, 23, 45, 78, NA, 89)
 
# display
print(data)
 
 
# find range in vector
print(range(data, na.rm=TRUE))


Output:

[1] 12 45 NA NA 67 23 45 78 NA 89
[1] 12 89


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads