dplyr - 查找y列的所有最大值和相应的x值

时间:2017-12-20 08:29:09

标签: r dplyr summary

我有这个数据集:

> head(df)
# A tibble: 6 x 3
# Groups:   type [1]
       x        y  type
   <dbl>    <dbl> <dbl>
1  70.00 400.4200     1
2  72.00 400.4200     1
3  94.10 400.4310     1
4 105.60 400.4330     1
5 122.65 400.4360     1
6 132.20 400.4375     1

我按类型对数据进行了分组,并使用dplyrs总结计算了每个组的最大y值:

library(magrittr)
library(dplyr)

df %<>% group_by(type)

df_summary <- df %>% summarize(maximum = max(y))

根据预期给出了每组的最大值。

> df_summary
# A tibble: 14 x 2
    type  maximum
   <dbl>    <dbl>
 1     1 400.4390
 2     2 400.4395
 3     3 400.4470
 4     4 400.4450
 5     5 400.4485
 6     6 400.4360
 7     7 400.4350
 8     8 400.4250
 9     9 400.4230
10    10 400.4235
11    11 400.4230
12    12 400.4260
13    13 400.4200
14    14 400.4145

我的第一个问题是:有没有办法使用汇总来获得属于最大y值的x值?我使用计算出的最大值来利用for循环从原始数据中得到我需要的东西:

for (i in unique(df_summary$type)){
  if (i == 1){
    aux <- df[(df$type == i) & (df$y == df_summary$maximum[i]),]
    aux$value <- "maximum"
    df_summary2 <- aux
  } else {
    aux <- df[(df$type == i) & (df$y == df_summary$maximum[i]),]
    aux$value <- "maximum"
    df_summary2 <- rbind(df_summary2, aux)
  }
}

> df_summary2
# A tibble: 16 x 4
# Groups:   type [14]
        x        y  type   value
    <dbl>    <dbl> <dbl>   <chr>
 1 139.70 400.4390     1 maximum
 2 143.30 400.4395     2 maximum
 3 426.70 400.4470     3 maximum
 4 151.50 400.4450     4 maximum
 5 146.20 400.4485     5 maximum
 6 151.20 400.4485     5 maximum
 7  99.90 400.4360     6 maximum
 8 445.40 400.4350     7 maximum
 9 323.40 400.4250     8 maximum
10 320.20 400.4230     9 maximum
11  97.90 400.4235    10 maximum
12  95.00 400.4230    11 maximum
13 108.55 400.4230    11 maximum
14  98.90 400.4260    12 maximum
15 108.70 400.4200    13 maximum
16 111.50 400.4145    14 maximum

有没有办法在不使用循环的情况下获得相同的结果?

数据:

structure(list(x = c(70, 72, 94.1, 105.6, 122.65, 132.2, 149.2, 
    162.2, 183.3, 188.8, 207.4, 222.9, 239.4, 247.4, 264.5, 275.5, 
    298.6, 302.6, 324.6, 336.6, 353.7, 360.7, 377.8, 392.8, 411.8, 
    417.8, 438.9, 451.9, 77.1, 86.65, 103.7, 115.2, 137.75, 139.3, 
    162.3, 173.4, 190.4, 200.4, 217.4, 230.5, 251.5, 256.6, 275.6, 
    290.6, 307.7, 315.7, 332.7, 343.8, 366.8, 369.8, 392.9, 403.9, 
    421, 429, 446, 70.3, 88.9, 94.4, 115.4, 128.5, 145.5, 154.6, 
    171.6, 183.6, 205.7, 207.7, 230.7, 241.8, 258.8, 267.8, 284.9, 
    297.9, 319, 325, 344, 359, 376.1, 383.1, 400.2, 411.2, 434.2, 
    438.2, 70.3, 81.4, 98.4, 105.9, 122.95, 138, 156.6, 162.6, 183.6, 
    198.6, 215.7, 224.7, 241.8, 252.8, 275.8, 277.8, 299.9, 311.45, 
    330, 340, 357, 370.1, 390.2, 396.2, 415.2, 430.2, 447.3, 454.3, 
    82, 93, 115.6, 119.1, 142.2, 153.2, 170.2, 177.75, 194.8, 209.85, 
    228.4, 234.4, 255.4, 268.5, 285.5, 294.6, 311.6, 322.6, 345.7, 
    347.7, 369.8, 381.8, 398.8, 407.8, 424.9, 437.9, 459, 75.8, 94.9, 
    109.9, 127, 134, 151, 162.1, 185.1, 189.1, 211.2, 223.2, 239.75, 
    247.3, 264.3, 279.4, 298.4, 303.9, 324.5, 337.5, 356.6, 367.1, 
    384.6, 396.6, 422.7, 424.7, 447.8, 458.8, 85.5, 95.05, 111.6, 
    125.6, 145.7, 151.7, 170.7, 185.8, 202.8, 209.8, 226.9, 238.4, 
    261, 265, 287, 299, 316.1, 323.1, 342.2, 357.2, 380.2, 385.3, 
    406.3, 419.4, 436.4, 445.4, 72.8, 84.3, 106.9, 108.9, 131.9, 
    143, 160, 169, 186.1, 199.1, 220.2, 226.2, 244.2, 259.75, 276.3, 
    284.3, 301.4, 312.4, 335.4, 338.95, 361.5, 372.6, 389.6, 397.6, 
    414.6, 429.7, 448.7, 453.8, 86.6, 99.7, 116.7, 126.25, 142.8, 
    154.8, 176.9, 178.9, 201.9, 213, 230, 239.5, 256.1, 270.1, 290.2, 
    296.2, 315.2, 332.2, 349.3, 356.3, 377.4, 388.4, 411.4, 414.5, 
    437.5, 448.6, 76.9, 84.9, 101.9, 117, 136, 141, 162.1, 175.1, 
    192.2, 201.7, 218.7, 230.2, 252.3, 254.3, 277.4, 288.4, 305.4, 
    314.95, 331.5, 345.5, 365.6, 371.6, 390.6, 405.7, 422.7, 429.8, 
    446.8, 457.8, 90, 93.5, 116.1, 127.1, 144.2, 152.2, 169.2, 184.2, 
    202.8, 208.3, 229.4, 242.4, 259.4, 268.5, 285.5, 297.5, 319.6, 
    321.6, 344.6, 355.7, 372.7, 381.8, 398.8, 412.3, 432.9, 438.9, 
    457.9, 82.3, 98.9, 106.4, 123.4, 134.5, 157.5, 161.05, 183.6, 
    194.6, 211.7, 219.7, 236.7, 251.8, 270.8, 275.8, 296.9, 309.9, 
    331, 340, 357, 368.55, 391.1, 393.1, 417.2, 429.2, 447.75, 457.3, 
    84.2, 97.2, 118.2, 123.75, 142.3, 157.85, 174.9, 182.4, 199.4, 
    210.5, 233.5, 237.5, 259.6, 271.6, 288.6, 295.7, 312.7, 327.8, 
    346.8, 352.8, 375.35, 390.4, 407.45, 421, 439.5, 451, 88.5, 90.5, 
    113.5, 124.6, 141.6, 150.6, 167.7, 180.7, 201.8, 206.8, 225.8, 
    240.9, 257.9, 265.9, 282.5, 294, 316.1, 320.1, 342.65, 354.2, 
    371.2, 378.2, 395.3, 410.3, 429.4, 434.4, 455.4), y = c(400.42, 
    400.42, 400.431, 400.433, 400.436, 400.4375, 400.4375, 400.4315, 
    400.42, 400.4175, 400.4075, 400.406, 400.409, 400.409, 400.411, 
    400.4075, 400.4065, 400.4065, 400.4105, 400.4135, 400.4215, 400.4215, 
    400.4265, 400.4295, 400.431, 400.434, 400.435, 400.4325, 400.4245, 
    400.428, 400.435, 400.4375, 400.439, 400.438, 400.432, 400.4285, 
    400.417, 400.411, 400.408, 400.4075, 400.4105, 400.41, 400.409, 
    400.4085, 400.412, 400.412, 400.4145, 400.4185, 400.4265, 400.427, 
    400.432, 400.4325, 400.4375, 400.4375, 400.436, 400.4155, 400.4205, 
    400.423, 400.4285, 400.4325, 400.441, 400.444, 400.436, 400.43, 
    400.4145, 400.4145, 400.4155, 400.4185, 400.4185, 400.4195, 400.4185, 
    400.419, 400.421, 400.421, 400.4255, 400.4325, 400.437, 400.4375, 
    400.4405, 400.443, 400.445, 400.445, 400.417, 400.422, 400.4265, 
    400.431, 400.4335, 400.4395, 400.443, 400.4415, 400.4295, 400.419, 
    400.416, 400.4145, 400.419, 400.421, 400.4195, 400.419, 400.42, 
    400.422, 400.4215, 400.421, 400.424, 400.426, 400.4285, 400.4295, 
    400.436, 400.439, 400.436, 400.433, 400.432, 400.4355, 400.436, 
    400.435, 400.445, 400.4465, 400.442, 400.442, 400.432, 400.425, 
    400.425, 400.426, 400.4285, 400.4265, 400.424, 400.424, 400.428, 
    400.4255, 400.422, 400.422, 400.422, 400.4235, 400.425, 400.428, 
    400.4345, 400.4345, 400.426, 400.4305, 400.434, 400.4345, 400.4325, 
    400.4335, 400.434, 400.433, 400.43, 400.4305, 400.4255, 400.424, 
    400.4265, 400.427, 400.4285, 400.424, 400.424, 400.425, 400.424, 
    400.421, 400.418, 400.417, 400.4215, 400.422, 400.4295, 400.43, 
    400.428, 400.424, 400.421, 400.4235, 400.424, 400.421, 400.425, 
    400.423, 400.422, 400.421, 400.421, 400.42, 400.423, 400.423, 
    400.426, 400.425, 400.422, 400.426, 400.428, 400.4275, 400.427, 
    400.426, 400.423, 400.4245, 400.425, 400.429, 400.433, 400.435, 
    400.4135, 400.414, 400.417, 400.4175, 400.416, 400.4175, 400.415, 
    400.414, 400.4155, 400.4185, 400.416, 400.4165, 400.421, 400.4225, 
    400.42, 400.419, 400.421, 400.423, 400.4225, 400.4205, 400.418, 
    400.4175, 400.4155, 400.4125, 400.413, 400.4185, 400.421, 400.419, 
    400.4165, 400.4175, 400.414, 400.4125, 400.416, 400.417, 400.413, 
    400.413, 400.414, 400.4145, 400.4155, 400.421, 400.42, 400.4195, 
    400.4175, 400.419, 400.421, 400.421, 400.4205, 400.4185, 400.4155, 
    400.413, 400.4105, 400.411, 400.4175, 400.4185, 400.4195, 400.42, 
    400.4215, 400.42, 400.419, 400.4175, 400.415, 400.4175, 400.4165, 
    400.4165, 400.415, 400.414, 400.416, 400.4165, 400.4145, 400.416, 
    400.416, 400.416, 400.414, 400.4115, 400.408, 400.4085, 400.4045, 
    400.4015, 400.406, 400.4065, 400.4065, 400.405, 400.4225, 400.4225, 
    400.42, 400.419, 400.422, 400.419, 400.4185, 400.417, 400.4185, 
    400.4155, 400.4185, 400.4175, 400.42, 400.4185, 400.416, 400.417, 
    400.42, 400.42, 400.417, 400.4125, 400.4105, 400.4105, 400.405, 
    400.4065, 400.409, 400.411, 400.409, 400.4215, 400.426, 400.4235, 
    400.4195, 400.4215, 400.4185, 400.42, 400.42, 400.4195, 400.416, 
    400.4165, 400.4175, 400.4195, 400.4195, 400.4185, 400.4175, 400.4185, 
    400.4185, 400.415, 400.414, 400.409, 400.4065, 400.406, 400.4065, 
    400.4095, 400.41, 400.4075, 400.415, 400.4165, 400.416, 400.413, 
    400.417, 400.414, 400.412, 400.411, 400.414, 400.4125, 400.413, 
    400.414, 400.4155, 400.4155, 400.414, 400.413, 400.415, 400.416, 
    400.4125, 400.411, 400.4065, 400.4065, 400.402, 400.4045, 400.4085, 
    400.407, 400.4125, 400.4125, 400.4115, 400.4085, 400.411, 400.409, 
    400.4025, 400.4035, 400.4055, 400.4055, 400.408, 400.404, 400.4085, 
    400.4085, 400.402, 400.4055, 400.4085, 400.41, 400.4045, 400.4035, 
    400.405, 400.4045, 400.3995, 400.4025, 400.403, 400.405, 400.4045
    ), type = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
    3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
    4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 
    6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
    7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 
    9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 
    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
    10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 
    11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
    11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 
    13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 
    13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 
    14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 
    14, 14, 14, 14)), .Names = c("x", "y", "type"), row.names = c(NA, 
    -379L), class = c("tbl_df", "tbl", "data.frame"))

2 个答案:

答案 0 :(得分:1)

我们可以使用FontDatawhich.max slice值为&#39; y&#39;对于每种类型&#39;

max

如果有联系,请使用df %>% group_by(type) %>% slice(which.max(y)) %>% mutate(value = "maximum") 来比较&#39; y&#39;使用==的&#39; y&#39;,max行或filter

slice

答案 1 :(得分:1)

arrangeslice的组合:

df %>% 
  group_by(type) %>% 
  arrange(y) %>% 
  slice(n())

结果:

# A tibble: 14 x 3
# Groups:   type [14]
        x        y  type
    <dbl>    <dbl> <dbl>
 1 149.20 400.4375     1
 2 137.75 400.4390     2
 3 438.20 400.4450     3
 4 156.60 400.4430     4
 5 153.20 400.4465     5
 6 109.90 400.4345     6
 7 445.40 400.4350     7
 8 312.40 400.4230     8
 9 332.20 400.4210     9
10 101.90 400.4215    10
11  93.50 400.4225    11
12  98.90 400.4260    12
13 142.30 400.4170    13
14  90.50 400.4125    14