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
How to Find the Range in R?
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