首页 > 解决方案 > 关于规则集的逻辑问题:在数据框中选择行值达到最大值和最小值的行

问题描述

我有以下带有 lan lon坐标、cloud_point_distance_to_ziel以及到点f0..f2的相对距离的数据框

     lon lat cloud_point_distance_to_ziel f0_distance_to_point f1_distance_to_point f2_distance_to_point
24  11.7  48                        32356                 6524                20802                35162
89  11.7  48                        32356                 6649                21219                34585
132 11.7  48                        33660                 6503                20698                35311
192 11.7  48                        32356                 6766                21589                34044
...

我希望能够在以下条件下有效地选择一行:

选择cloud_point_distance_to_ziel值达到最小值f0..f2值达到最大值的行。最大值和最小值在行内不必是绝对值,但应根据后续规则达到它们的 min/mx 值。

例子:

a has values (3,4,5,6,1,2,4,5,8,9,4)
b has values (2,5,6,3,2,9,7,4,2,4,3)
c has values (2,1,4,6,3,6,8,9,2,1,2)

possible min values of (a)->1,2,3,4
possible max values of (b)->9,7,6,5
possible max values of (c)->9,8,6,4

selected row with a,b,c values  2,9,6

结果应该选择一行

我尝试了以下方法,但我不相信结果。也许使用四分位数可以解决问题?

f0_mean<-mean(data$f0_distance_to_point)
f1_mean<-mean(data$f1_distance_to_point)
f2_mean<-mean(data$f2_distance_to_point)

point_result<-data[(which.min(data$cloud_point_distance_to_ziel) 
                              & (data$f0_distance_to_point > f0_mean)
                              & (data$f1_distance_to_point > f1_mean) 
                              & (data$f2_distance_to_point > f2_mean)),]

其他方法:

    data<-data[data$f0_distance_to_point>quantile(data$f0_distance_to_point,0.85),]
    
    data<-data[data$f1_distance_to_point>quantile(data$f1_distance_to_point,0.85),]
    
data<-data[data$f2_distance_to_point>quantile(data$f2_distance_to_point,0.85),]

point_result <- data[which.min(data$cloud_point_distance_to_ziel),]

数据代码:

data <-
  structure(
    list(
      lon = c(
        11.7397709792062,
        11.7448661049966,
        11.7384976841411,
        11.7492009896508,
        11.798531500208,
        11.7562989433643,
        11.7817986819213,
        11.7457818355317,
        11.7565528127003,
        11.775019447482,
        11.7990427748903,
        11.768883801117,
        11.7820267269428,
        11.7526821785921,
        11.7729644807609,
        11.7656290403773,
        11.7689881451332,
        11.7856195303942,
        11.7784078217926,
        11.7614946153811,
        11.7513483568678,
        11.773126221415,
        11.7836991580849,
        11.7650082634726,
        11.7480285610035,
        11.7598040660488,
        11.7673422104715,
        11.7549204850733,
        11.7725605176412,
        11.7671241745901,
        11.7668667395602,
        11.7900370162996,
        11.785291372675,
        11.7462798005881,
        11.7854305475734,
        11.7472521033403,
        11.7706277569342,
        11.7582629707936,
        11.7812623910872,
        11.7618026552496
      ),
      lat = c(
        48.0406345358279,
        48.0366692132014,
        48.0416864931132,
        48.0326126303325,
        48.01404507432,
        48.0250544655736,
        48.0127446150512,
        48.0310952221558,
        48.0191463660624,
        48.0141901000025,
        48.0173447367971,
        48.0125637323678,
        48.0259059854022,
        48.0300255676393,
        48.0149681602546,
        48.0205116689316,
        48.0132250539091,
        48.0155813575988,
        48.0163665355201,
        48.0221114178826,
        48.0277707588402,
        48.0251052314425,
        48.0143854199087,
        48.0128206848559,
        48.0294561032443,
        48.0274566855892,
        48.0222287044966,
        48.0308697960588,
        48.0263675936893,
        48.0222650935833,
        48.023463537079,
        48.0192912029751,
        48.0207717565613,
        48.0301403042609,
        48.0201970547079,
        48.0282788741312,
        48.019573189748,
        48.0260046046014,
        48.0251574108575,
        48.023237280448
      ),
      cloud_point_distance_to_ziel = list(
        32355.8059191835,
        32355.8059191835,
        33659.7804661781,
        32355.8059191835,
        32355.8059191835,
        31960.7024820624,
        33659.7804661781,
        32355.8059191835,
        31960.7024820624,
        33659.7804661781,
        32355.8059191835,
        33659.7804661781,
        32355.8059191835,
        32355.8059191835,
        31960.7024820624,
        32355.8059191835,
        31960.7024820624,
        32355.8059191835,
        33659.7804661781,
        32355.8059191835,
        33659.7804661781,
        31960.7024820624,
        32355.8059191835,
        33659.7804661781,
        31960.7024820624,
        32355.8059191835,
        31960.7024820624,
        33659.7804661781,
        31960.7024820624,
        32355.8059191835,
        32355.8059191835,
        31960.7024820624,
        33659.7804661781,
        32355.8059191835,
        33659.7804661781,
        31960.7024820624,
        32355.8059191835,
        33659.7804661781,
        31960.7024820624,
        31960.7024820624
      ),
      f0_distance_to_point = c(
        6523.56087201753,
        6649.28217687594,
        6503.46768539322,
        6765.95829619417,
        6442.12791798449,
        6671.40625006645,
        6230.34780000719,
        6468.47038347654,
        6252.13607978831,
        6432.22943583159,
        6810.39601414163,
        6325.85372739144,
        7691.27105446194,
        6881.83516439466,
        6538.07646927175,
        6878.62834481693,
        6396.4585747719,
        6539.5519957707,
        6647.70571839638,
        6745.04532140725,
        6629.26187444153,
        7630.64388285189,
        6408.02619767719,
        6367.71629639824,
        6565.05469806704,
        7044.18856439527,
        7094.89054298961,
        7062.73262801013,
        7682.87937043481,
        7084.41530641291,
        7150.67826425775,
        6960.02910152723,
        7116.38557806022,
        6466.92695660631,
        7052.50291443861,
        6469.64693739617,
        7070.06497164368,
        6850.02543111819,
        7610.26353022488,
        6842.4247208008
      ),
      f1_distance_to_point = c(
        20801.5164909635,
        21218.7363557505,
        20697.9752625597,
        21588.685952041,
        25527.6407705382,
        22226.5828888995,
        24333.8450858125,
        21357.567838855,
        22355.9495480481,
        23808.1236364208,
        25500.5889158094,
        23397.5147560411,
        24104.3477362433,
        21881.511455553,
        23641.4840149903,
        22993.0892224942,
        23390.0677717411,
        24552.7137121619,
        24009.6860310581,
        22660.185764715,
        21817.7744631735,
        23462.2419669878,
        24437.1899218789,
        23109.7140789518,
        21547.2569810623,
        22444.8013577727,
        23086.8031247124,
        22034.3678067779,
        23400.505352285,
        23070.1548188659,
        23030.0084246067,
        24804.4448056518,
        24429.6367079515,
        21408.2062843106,
        24450.1656200026,
        21508.3844081874,
        23377.0720196865,
        22354.8622851421,
        24059.8022609345,
        22662.3554001251
      ),
      f2_distance_to_point = c(
        35161.890114791,
        34585.1875379748,
        35310.6054979667,
        34043.6744250552,
        29954.8203279982,
        33090.0886089445,
        30765.5644251227,
        34120.3101266009,
        32643.0111946674,
        31247.8830306243,
        30183.3149564129,
        31471.1319336641,
        31753.0551465962,
        33658.9659164565,
        31419.4770485574,
        32236.5066464529,
        31513.466445502,
        30769.3037095555,
        31222.7894718709,
        32584.1630048325,
        33565.6468925473,
        32170.4412766329,
        30783.9567801719,
        31708.0000202251,
        33874.5024543128,
        33075.0473409777,
        32269.6466231121,
        33598.8050527318,
        32297.1668032941,
        32284.3666525881,
        32388.1365599397,
        30814.0868557006,
        31182.6177367083,
        34022.1959836496,
        31131.0603020996,
        33831.1039551706,
        31890.5101319178,
        33051.7003546685,
        31736.1029439456,
        32650.4643103276
      )
    ),
    row.names = c(
      24L,
      89L,
      132L,
      192L,
      403L,
      413L,
      418L,
      420L,
      422L,
      430L,
      432L,
      436L,
      447L,
      453L,
      458L,
      465L,
      470L,
      474L,
      477L,
      482L,
      483L,
      505L,
      515L,
      525L,
      538L,
      542L,
      544L,
      561L,
      574L,
      575L,
      578L,
      586L,
      597L,
      602L,
      618L,
      630L,
      634L,
      638L,
      642L,
      645L
    ),
    class = "data.frame"
  )

标签: rdataframetidyversetidyr

解决方案


推荐阅读