简体   繁体   English

在一行中找到最大值,然后与其他行中的最大值进行比较

[英]Find maximum in one row and compare with max in other rows

I am looking for the easiest way to filter my data: 我正在寻找过滤数据的最简单方法:

Let's use that one as an example: 让我们以其中一个为例:

structure(list(mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1, 14.3, 
                       24.4, 22.8, 19.2), cyl = c(6, 6, 4, 6, 8, 6, 8, 4, 4, 6), disp = c(160, 
                                                                                          165, 108, 258, 360, 225, 360, 146.7, 140.8, 167.6), hp = c(310, 
                                                                                                                                                     110, 93, 110, 475, 105, 245, 62, 95, 223), drat = c(3.9, 3.9, 
                                                                                                                                                                                                         3.85, 3.08, 3.15, 2.76, 633.21, 3.69, 3.92, 3.92), wt = c(2.62, 
                                                                                                                                                                                                                                                                 2.875, 2.32, 3.215, 3.44, 3.46, 3.57, 3.19, 3.15, 3.44), qsec = c(16.46, 
                                                                                                                                                                                                                                                                                                                                   17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20, 22.9, 18.3), vs = c(0, 
                                                                                                                                                                                                                                                                                                                                                                                                     0, 1, 1, 0, 1, 0, 1, 1, 1), am = c(1, 1, 1, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                                                                                                                                                                                                                                                        0), gear = c(4, 4, 4, 3, 3, 3, 3, 4, 4, 4), carb = c(4, 4, 1, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             1, 2, 1, 4, 2, 2, 4)), .Names = c("mpg", "cyl", "disp", "hp", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "drat", "wt", "qsec", "vs", "am", "gear", "carb"), row.names = c("Mark_1", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                "Mark_2", "Mark_3", "Tom_1", "Tom_2", "Tim_1", "Greg_1", "Greg_2", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                "Greg_3", "Greg_4"), class = "data.frame")

I would like to filter rows with "the same" name and keep only one. 我想过滤具有“相同”名称的行,并只保留一个。 Some of the names differ only in a number after _ . 一些名称的区别仅在于_之后的数字。 These ones should be compared to each other. 这些应该相互比较。

The criteria for filter: 过滤条件:

I would like to first of all find the maximum in each row and than take these maximas and compare them between rows with same name. 首先,我想在每一行中找到最大值,然后将这些最大值与具有相同名称的行进行比较。 The row with highest maxima should be kept 最大值最高的行应保留

Exptected output if I am not mistaken: 如果我没记错的话,可以得到预期的输出:

structure(list(mpg = c(21, 18.7, 18.1, 14.3), cyl = c(6, 8, 6, 
8), disp = c(160, 360, 225, 360), hp = c(310, 475, 105, 245), 
    drat = c(3.9, 3.15, 2.76, 633.21), wt = c(2.62, 3.44, 3.46, 
    3.57), qsec = c(16.46, 17.02, 20.22, 15.84), vs = c(0, 0, 
    1, 0), am = c(1, 0, 0, 0), gear = c(4, 3, 3, 3), carb = c(4, 
    2, 1, 4)), .Names = c("mpg", "cyl", "disp", "hp", "drat", 
"wt", "qsec", "vs", "am", "gear", "carb"), row.names = c("Mark_1", 
"Tom_2", "Tim_1", "Greg_1"), class = "data.frame")

> datas
        mpg cyl disp  hp   drat   wt  qsec vs am gear carb
Mark_1 21.0   6  160 310   3.90 2.62 16.46  0  1    4    4
Tom_2  18.7   8  360 475   3.15 3.44 17.02  0  0    3    2
Tim_1  18.1   6  225 105   2.76 3.46 20.22  1  0    3    1
Greg_1 14.3   8  360 245 633.21 3.57 15.84  0  0    3    4

We split the dataset into a list of data.frame by creating a grouping variable with the row names after removing the substring _ followed by numbers ( \\\\d+ ), then loop through the list , find the max of each row ( pmax ), get the index of the maximum value ( which.max ), use it to subset the rows, and rbind the rows together 我们split数据集splitdata.frame listdata.frame是在删除子字符串_后再加上数字( \\\\d+ ),然后使用行名称创建分组变量,然后遍历list ,找到每一行的最大值( pmax ),获得最大值(指数which.max ),用它来子集行, rbind行一起

do.call(rbind, setNames(lapply(split(df1, sub("_\\d+", "", rownames(df1))),
             function(x) x[which.max(do.call(pmax, x)),]), NULL))
#        mpg cyl disp  hp   drat   wt  qsec vs am gear carb
#Greg_1 14.3   8  360 245 633.21 3.57 15.84  0  0    3    4
#Mark_1 21.0   6  160 310   3.90 2.62 16.46  0  1    4    4
#Tim_1  18.1   6  225 105   2.76 3.46 20.22  1  0    3    1
#Tom_2  18.7   8  360 475   3.15 3.44 17.02  0  0    3    2

Or with ave , we create a logical index. 或使用ave ,我们创建一个逻辑索引。 Get the max of each row ( do.call(pmax, df1) ), use the row names after substring as grouping variable, get the max value, do a comparison ( == ) with the elements, convert to logical and subset the rows 获取每行的maxdo.call(pmax, df1) ),使用子字符串后的行名称作为分组变量,获取max ,与元素进行比较( == ),将行转换为逻辑行并对其进行子集化

df1[with(df1, as.logical(ave(do.call(pmax, df1), sub("_\\d+", "", rownames(df1)), 
                          FUN = function(x) x==max(x)))),]
#        mpg cyl disp  hp   drat   wt  qsec vs am gear carb
#Mark_1 21.0   6  160 310   3.90 2.62 16.46  0  1    4    4
#Tom_2  18.7   8  360 475   3.15 3.44 17.02  0  0    3    2
#Tim_1  18.1   6  225 105   2.76 3.46 20.22  1  0    3    1
#Greg_1 14.3   8  360 245 633.21 3.57 15.84  0  0    3    4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM