find the range of all elements in an array in r

To find the range of all elements in an array in R, you can use the range() function which returns a vector of the minimum and maximum values of the input data.

main.r
# create an example array
my_array <- array(data = c(2, 4, 6, 8, 10, 12), dim = c(3, 2))

# find the range of all elements in the array
range(my_array)
152 chars
6 lines

The output will be a vector with the minimum and maximum values of the array:

[1] 2 12

Alternatively, you can also use the min() and max() functions to find the minimum and maximum values, and then calculate the range by subtracting the minimum from the maximum:

main.r
my_min <- min(my_array)
my_max <- max(my_array)
my_range <- my_max - my_min

# print the range
my_range
104 chars
7 lines

This will also give you the range of all elements in the array:

[1] 10

related categories

gistlibby LogSnag