How to find the maximum value in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

The maximum value is a part of summary statistics and we always need to understand the end limits of our data; therefore, it is highly required. If we have a data frame that contains numerical columns then the maximum value can be found by using max function and the data frame object name.

Example1

 Live Demo

Consider the below data frame −

set.seed(357)
x1<−1:20
x2<−rnorm(20)
x3<−rpois(20,5)
df1<−data.frame(x1,x2,x3)
df1

Output

   x1    x2      x3
1 1 −1.24111731   3
2 2 −0.58320499   1
3 3 0.39474705    6
4 4 1.50421107    4
5 5 0.76679974    5
6 6 0.31746044    4
7 7 −0.09997594   5
8 8 0.22703071    9
9 9 −0.46901506   2
10 10 0.47652129  5
11 11 −0.91164798 7
12 12 −0.34177516 4
13 13 0.54674134  4
14 14 −0.32720797 4
15 15 0.04108975  3
16 16 −0.27603366 2
17 17 −0.59349654 2
18 18 0.17646182  6
19 19 0.50575975  4
20 20 1.00851578  3

Finding the maximum value in df1 −

Example

max(df1)

Output

[1] 20

Example2

 Live Demo

y1<−rexp(20,2.24)
y2<−runif(20,2,10)
y3<−rpois(20,2)
df2<−data.frame(y1,y2,y3)
df2

Output

      y1       y2      y3
1 0.407887709 2.213538 1
2 1.740076315 8.314833 3
3 0.192889321 2.085092 1
4 0.166686518 2.295179 3
5 0.057761320 3.117768 5
6 0.001199064 8.917747 3
7 0.212810013 5.560138 1
8 0.295809473 6.418326 0
9 0.198128409 6.662312 3
10 0.711248734 4.899675 2
11 0.228512534 5.439995 1
12 0.614415810 7.231990 2
13 0.552638453 6.999130 3
14 1.181955638 2.650087 3
15 0.094478314 4.154438 4
16 0.114416419 2.687630 3
17 0.521020383 4.552469 2
18 0.145134708 3.100630 1
19 0.189475950 3.741155 1
20 0.055434073 7.268179 3

Finding the maximum value in df2 −

Example

max(df2)

Output

[1] 8.917747

Example3

 Live Demo

z1<−rnorm(20)
z2<−rnorm(20)
z3<−rnorm(20)
df3<−data.frame(z1,z2,z3)
df3

Output

      z1          z2          z3
1 0.25666831 −0.01903028   0.6416281
2 0.37078975 −0.88863057   1.3649943
3 0.75034126 1.78456003   −1.3705236
4 −1.17411909 −0.17708087  0.4413502
5 −0.51917037 2.70309602   0.6068021
6 −0.06786451 0.97958347   0.3195107
7 −0.45662528 0.31657666   1.4426260
8 0.06079389 0.59762732    1.8300967
9 0.32373216 −0.48484042   0.5149204
10 −0.80419025 −1.41713258 0.3394880
11 0.05776455 −0.08407858 −0.5394629
12 −0.98194207 −0.79692879 0.2427494
13 0.37626316 0.92851151   0.3595236
14 −0.51862537 −0.68906500 1.5214189
15 0.47366488 1.59535911   0.8477736
16 1.07751737 1.59310361  −1.6904989
17 −1.61338456 −0.87773672 −0.3547148
18 −1.53023659 0.70204809   1.1185562
19 −0.17439684 0.29133043  −0.1620996
20 −1.31023224 −0.94650432 −0.5271143

Example

Finding the maximum value in df3 −

max(df3)

Output

[1] 2.703096
raja
Published on 05-Feb-2021 08:30:08
Advertisements