# How to find the extremes of a data frame column in R if infinity exists in the column?

R ProgrammingServer Side ProgrammingProgramming

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.

## Example

Live Demo

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

## Output

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 −

## Output

[1] 1.529547 3.003426
Published on 08-Feb-2021 11:57:38