R Func Range
# R range() Function - Calculate Numeric Range
[ R Language Examples](https://example.com/r/r-examples.html)
The R range() function is used to return the minimum and maximum values of elements in a vector.
It returns a vector containing two values at once, which is more convenient than calling min() and max() separately.
The syntax of the range() function is as follows:
range(x, na.rm = FALSE)
**Parameter Description:**
* **x** Input vector.
* **na.rm** Boolean value, default is FALSE, specifies whether to remove missing values NA.
## Example
# Create a vector
x <-c(12, 27, 3, 4.2, 2, 2, 54, -21, 4, -2)
# Calculate the range
result.range<-range(x)
print(result.range)
# Calculate the range (max minus min)
print(diff(result.range))
The output of executing the above code is:
-21 54 75
range() is particularly useful when plotting, and can be used to set the axis range:
## Example
# Create data
x <-1:10
y <-c(3, 7, 2, 9, 15, 8, 12, 5, 18, 10)
# View the range of y
y_range <-range(y)
print(y_range)
# Use range to extend the axis range
plot(x, y, type ="o",
ylim =c(y_range-1, y_range+1),
main ="Line chart with range()")
The output of executing the above code is:
2 18
[ R Language Examples](https://example.com/r/r-examples.html)
YouTip