如何将新列添加到R数据帧中每行具有最大值?
当我们有一个包含所有数字列的数据框时,我们可能希望在每一行中找到最大值。例如,如果我们有一个销售数据集,其中每一行代表一个客户,列代表具有值数量作为值的产品,那么我们可能希望找到每一行的最大值,以找出谁购买的产品最多。这可以通过对行使用max和apply函数来完成。
示例
请看以下数据帧-
> 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
输出结果
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
添加新列最大值包含每行的最大值-
> df1$Maximum<-apply(df1,1,max) > df1
输出结果
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
让我们看另一个例子-
示例
> y1<-rpois(20,1) > y2<-rpois(20,2) > y3<-rpois(20,5) > y4<-rpois(20,10) > df2<-data.frame(y1,y2,y3,y4) > df2
输出结果
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
输出结果
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