To find the extremes of a data frame column that is numeric can be done with the help of min and max function but if we want to get the same in single line code then range function can be used. If there are some infinity values in the column then range.default function will be used as shown in the below example.
Consider the below data frame −
set.seed(214) x1<−sample(c(1,5,Inf,10),20,replace=TRUE) x2<−rpois(20,5) df1<−data.frame(x1,x2) df1
x1 x2 1 Inf 0 2 10 5 3 1 3 4 10 7 5 10 3 6 5 5 7 1 4 8 Inf 2 9 10 3 10 Inf 9 11 1 4 12 Inf 3 13 5 3 14 5 5 15 1 6 16 Inf 1 17 10 6 18 Inf 3 19 10 4 20 10 8
Finding the extremes of column x1 −
range.default(df1$x1,finite=TRUE)
[1] 1 10
y1<−rnorm(20,5,2.3) y2<−sample(c(Inf,rnorm(5,2)),20,replace=TRUE) df2<−data.frame(y1,y2) df2
y1 y2 1 5.711206 2.367679 2 3.891755 2.367679 3 8.345217 1.529547 4 4.316907 1.888925 5 5.945366 Inf 6 8.204934 1.888925 7 9.634025 Inf 8 5.096354 Inf 9 8.186749 2.367679 10 6.808151 Inf 11 6.310553 2.586410 12 5.207341 2.586410 13 4.190045 1.888925 14 6.570371 Inf 15 1.553658 2.367679 16 5.869204 2.367679 17 7.211524 2.586410 18 7.517553 1.888925 19 7.224195 3.003426 20 4.105865 1.888925
Finding the extremes of column x1 −
range.default(df2$y2,finite=TRUE)
[1] 1.529547 3.003426