How to add a new column to an R data frame with largest value in each row?


When we have a data frame that contains all numerical columns then we might want to find the largest value in each row. For example, if we have a sales data set in which each row represents a customer and columns represent the products with quantities of values as values then we might want to find the maximum of each row to find out who buys which product the most. This can be done by using max with apply function for rows.

Example

Consider the below data frame −

 Live Demo

> x1<-rnorm(20,0.5)
> x2<-rnorm(20,1)
> x3<-rnorm(20,1.5)
> x4<-rnorm(20,2)
> x5<-rnorm(20,2.5)
> df1<-data.frame(x1,x2,x3,x4,x5)
> df1

Output

      x1          x2          x3       x4       x5
1 1.18034186 0.49284328 -0.08718758 2.1347878 1.5268073
2 -1.40782804 1.42663693 1.32881022 2.6669878 1.0841468
3 -0.97983604 1.44169070 1.61071046 1.2565825 2.4606037
4 0.66278043 -0.92167522 1.83864561 2.8141814 2.9662971
5 -0.28894011 2.48075526 1.12914164 1.9516229 2.1376559
6 0.99524440 0.08957948 2.26064272 2.4830815 2.6360470
7 -0.14063209 2.26533708 2.69280376 2.5909366 0.6680764
8 1.42980597 0.53201969 2.20666891 1.0486360 1.5426519
9 -1.56460610 1.28753103 0.27476707 2.8115892 2.0032481
10 -0.54612472 3.08304670 0.49416424 2.5654104 2.2844867
11 1.65764450 0.43096784 1.58808994 1.8602615 2.6050931
12 -0.75804168 1.37431560 2.44018038 2.1144182 3.4949352
13 -0.03532605 1.81643566 2.62136897 2.3025602 2.8444211
14 1.79233771 0.56249057 1.40411296 2.8285359 2.1802467
15 -0.26371033 1.76775985 1.12580463 2.3279574 1.4959805
16 -0.96034739 2.27838134 0.88127663 0.7533895 1.9305621
17 -0.38665902 1.05886571 0.57545650 1.2139412 2.6353083
18 0.18425157 -0.70984826 1.36148886 2.8257107 3.8666039
19 -0.55344371 -0.57471267 2.00483613 1.5374169 1.6765151
20 0.69122904 0.62235483 1.44516323 3.3455113 2.4330382

Adding a new column Maximum containing largest of each row −

> df1$Maximum<-apply(df1,1,max)
> df1

Output

      x1          x2          x3          x4       x5    Maximum
1 1.18034186 0.49284328 -0.08718758 2.1347878 1.5268073 2.134788
2 -1.40782804 1.42663693 1.32881022 2.6669878 1.0841468 2.666988
3 -0.97983604 1.44169070 1.61071046 1.2565825 2.4606037 2.460604
4 0.66278043 -0.92167522 1.83864561 2.8141814 2.9662971 2.966297
5 -0.28894011 2.48075526 1.12914164 1.9516229 2.1376559 2.480755
6 0.99524440 0.08957948 2.26064272 2.4830815 2.6360470 2.636047
7 -0.14063209 2.26533708 2.69280376 2.5909366 0.6680764 2.692804
8 1.42980597 0.53201969 2.20666891 1.0486360 1.5426519 2.206669
9 -1.56460610 1.28753103 0.27476707 2.8115892 2.0032481 2.811589
10 -0.54612472 3.08304670 0.49416424 2.5654104 2.2844867 3.083047
11 1.65764450 0.43096784 1.58808994 1.8602615 2.6050931 2.605093
12 -0.75804168 1.37431560 2.44018038 2.1144182 3.4949352 3.494935
13 -0.03532605 1.81643566 2.62136897 2.3025602 2.8444211 2.844421
14 1.79233771 0.56249057 1.40411296 2.8285359 2.1802467 2.828536
15 -0.26371033 1.76775985 1.12580463 2.3279574 1.4959805 2.327957
16 -0.96034739 2.27838134 0.88127663 0.7533895 1.9305621 2.278381
17 -0.38665902 1.05886571 0.57545650 1.2139412 2.6353083 2.635308
18 0.18425157 -0.70984826 1.36148886 2.8257107 3.8666039 3.866604
19 -0.55344371 -0.57471267 2.00483613 1.5374169 1.6765151 2.004836
20 0.69122904 0.62235483 1.44516323 3.3455113 2.4330382 3.345511

Let’s have a look at another example −

Example

 Live Demo

> y1<-rpois(20,1)
> y2<-rpois(20,2)
> y3<-rpois(20,5)
> y4<-rpois(20,10)
> df2<-data.frame(y1,y2,y3,y4)
> df2

Output

  y1 y2 y3 y4
1 4 3 5 9
2 0 4 6 6
3 0 2 4 11
4 0 1 8 9
5 2 2 5 14
6 0 3 6 13
7 1 2 12 10
8 1 3 6 14
9 3 3 3 5
10 3 2 4 7
11 1 2 10 6
12 1 2 4 9
13 1 1 3 11
14 0 2 4 17
15 1 3 4 9
16 1 3 5 12
17 1 2 6 6
18 1 0 6 10
19 0 5 1 8
20 1 4 3 11
> df2$Maximum<-apply(df2,1,max)
> df2

Output

y1 y2 y3 y4 Maximum
1 4 3 5 9 9
2 0 4 6 6 6
3 0 2 4 11 11
4 0 1 8 9 9
5 2 2 5 14 14
6 0 3 6 13 13
7 1 2 12 10 12
8 1 3 6 14 14
9 3 3 3 5 5
10 3 2 4 7 7
11 1 2 10 6 10
12 1 2 4 9 9
13 1 1 3 11 11
14 0 2 4 17 17
15 1 3 4 9 9
16 1 3 5 12 12
17 1 2 6 6 6
18 1 0 6 10 10
19 0 5 1 8 8
20 1 4 3 11 11

Updated on: 04-Sep-2020

547 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements