Reputation: 2233
I found this code on :http://onertipaday.blogspot.co.il/search/label/descriptive%20statistic
The code is describing a workaround for finding the min
and max
in a dataset that has inf
and -inf
in a vector. However I don't understand the purpose of the [1]
and [2]
in the last two lines of code.
data <- c(-Inf, 1,2,3,4,5,6,7,8,9,10, Inf)
max(data)
# Return Inf
min(data)
# Return -Inf
# To solve the problem I went to:
range(data, finite=TRUE)
# Then you can do
myMinimum <- range(data, finite=TRUE)[1]
myMaximum <- range(data, finite=TRUE)[2]
Upvotes: 0
Views: 62
Reputation: 51640
The range function returns a vector of length 2, with the first being the minimum and the second being the maximum.
For instance:
> a <- 15:30
> range(a)
[1] 15 30
Using the []
operator you extract the desired element
> range(a)[1]
[1] 15
> range(a)[2]
[1] 30
Or you can also do:
r <- range(a)
my.min <- r[1]
my.max <- r[2]
For more information read ?range
.
Also, you can directly use the min
and max
functions.
Upvotes: 1