Home > Mobile >  Is there a way to filter out the row that has the highest of three different columns simultaneously?
Is there a way to filter out the row that has the highest of three different columns simultaneously?

Time:11-29

Is there a way to filter out the row that has the highest of three different columns simultaneously? I am trying to filter out the row that has the best accuracy, specificity, and sensitivity in a data frame.

Pic of first few rows of data

pic of first few rows of data

in the data provided the highest for all 3 should be (aka the desired output)

"thresh_info.59 0.60 83.39 83.27684 83.557047"

enter image description here

data<- structure(list(threshold = c(0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 
                             0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 
                             0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 
                             0.29, 0.3, 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 
                             0.4, 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5, 
                             0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6, 0.61, 
                             0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7, 0.71, 0.72, 
                             0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8, 0.81, 0.82, 0.83, 
                             0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9, 0.91, 0.92, 0.93, 0.94, 
                             0.95, 0.96, 0.97, 0.98, 0.99), accuracy = c(61.72, 63.67, 65.29, 
                                                                         66.58, 67.86, 69.01, 69.75, 70.83, 71.51, 72.79, 73.87, 74.54, 
                                                                         75.02, 75.29, 75.83, 76.3, 76.7, 77.25, 77.65, 77.92, 78.33, 
                                                                         79, 79.14, 79.07, 79.41, 79.61, 79.68, 80.28, 80.69, 80.82, 80.89, 
                                                                         81.16, 81.3, 81.77, 81.9, 81.97, 82.17, 82.31, 82.44, 82.65, 
                                                                         82.58, 82.92, 82.98, 83.59, 83.52, 83.59, 83.25, 83.46, 83.39, 
                                                                         83.46, 83.66, 83.73, 83.52, 83.66, 83.93, 83.46, 83.25, 83.32, 
                                                                         83.32, 83.39, 83.39, 82.92, 82.24, 82.04, 81.77, 81.3, 81.5, 
                                                                         81.23, 81.03, 80.89, 80.49, 80.35, 80.01, 80.01, 79.2, 79.14, 
                                                                         78.87, 78.93, 78.6, 77.92, 77.25, 76.91, 76.37, 75.56, 74.81, 
                                                                         73.94, 73.13, 72.79, 71.84, 71.51, 69.89, 68.4, 66.44, 64.82, 
                                                                         63.13, 61.44, 59.08, 55.77, 52.8), sensitivity = c(100, 100, 
                                                                                                                            100, 99.8870056497175, 99.8870056497175, 99.6610169491526, 99.6610169491526, 
                                                                                                                            99.5480225988701, 99.3220338983051, 99.2090395480226, 99.0960451977401, 
                                                                                                                            98.7570621468927, 98.6440677966102, 98.5310734463277, 98.1920903954802, 
                                                                                                                            97.9661016949153, 97.7401129943503, 97.6271186440678, 96.8361581920904, 
                                                                                                                            96.3841807909604, 96.045197740113, 95.9322033898305, 95.5932203389831, 
                                                                                                                            95.2542372881356, 94.9152542372881, 94.6892655367232, 94.2372881355932, 
                                                                                                                            94.1242937853107, 94.1242937853107, 93.6723163841808, 93.1073446327684, 
                                                                                                                            92.8813559322034, 92.6553672316384, 92.4293785310735, 92.316384180791, 
                                                                                                                            91.9774011299435, 91.864406779661, 91.5254237288136, 91.2994350282486, 
                                                                                                                            90.8474576271186, 90.0564971751412, 89.9435028248588, 89.7175141242938, 
                                                                                                                            89.2655367231638, 89.0395480225989, 88.8135593220339, 88.135593220339, 
                                                                                                                            87.909604519774, 87.3446327683616, 87.0056497175141, 86.5536723163842, 
                                                                                                                            86.3276836158192, 85.6497175141243, 85.5367231638418, 85.5367231638418, 
                                                                                                                            84.7457627118644, 84.180790960452, 83.8418079096045, 83.6158192090395, 
                                                                                                                            83.2768361581921, 83.0508474576271, 82.0338983050847, 80.6779661016949, 
                                                                                                                            80.225988700565, 79.3220338983051, 78.1920903954802, 77.9661016949153, 
                                                                                                                            76.9491525423729, 76.1581920903955, 75.4802259887006, 74.3502824858757, 
                                                                                                                            73.7853107344633, 72.8813559322034, 72.4293785310735, 70.8474576271186, 
                                                                                                                            70.1694915254237, 69.1525423728814, 68.8135593220339, 68.135593220339, 
                                                                                                                            66.8926553672316, 65.6497175141243, 64.4067796610169, 63.3898305084746, 
                                                                                                                            61.9209039548023, 60.4519774011299, 58.6440677966102, 57.2881355932203, 
                                                                                                                            56.1581920903955, 54.3502824858757, 53.3333333333333, 50.6214689265537, 
                                                                                                                            48.135593220339, 44.8587570621469, 41.9209039548023, 38.6440677966102, 
                                                                                                                            35.7062146892655, 31.638418079096, 26.1016949152542, 21.1299435028249
                                                                         ), specificity = c(4.86577181208054, 9.73154362416107, 13.758389261745, 
                                                                                            17.1140939597315, 20.3020134228188, 23.489932885906, 25.3355704697987, 
                                                                                            28.1879194630872, 30.2013422818792, 33.5570469798658, 36.4093959731544, 
                                                                                            38.5906040268456, 39.9328859060403, 40.7718120805369, 42.6174496644295, 
                                                                                            44.1275167785235, 45.4697986577181, 46.9798657718121, 49.1610738255034, 
                                                                                            50.503355704698, 52.0134228187919, 53.8590604026846, 54.6979865771812, 
                                                                                            55.0335570469799, 56.3758389261745, 57.2147651006711, 58.0536912751678, 
                                                                                            59.7315436241611, 60.738255033557, 61.744966442953, 62.751677852349, 
                                                                                            63.758389261745, 64.4295302013423, 65.9395973154362, 66.4429530201342, 
                                                                                            67.1140939597315, 67.7852348993289, 68.6241610738255, 69.2953020134228, 
                                                                                            70.4697986577181, 71.4765100671141, 72.4832214765101, 72.9865771812081, 
                                                                                            75.1677852348993, 75.3355704697987, 75.8389261744966, 76.006711409396, 
                                                                                            76.8456375838926, 77.5167785234899, 78.1879194630873, 79.3624161073825, 
                                                                                            79.8657718120805, 80.3691275167785, 80.8724832214765, 81.5436241610738, 
                                                                                            81.5436241610738, 81.8791946308725, 82.5503355704698, 82.8859060402685, 
                                                                                            83.5570469798658, 83.8926174496644, 84.2281879194631, 84.5637583892617, 
                                                                                            84.7315436241611, 85.4026845637584, 85.9060402684564, 86.744966442953, 
                                                                                            87.5838926174497, 88.255033557047, 88.9261744966443, 89.5973154362416, 
                                                                                            90.1006711409396, 90.6040268456376, 91.2751677852349, 91.6107382550336, 
                                                                                            92.4496644295302, 93.2885906040269, 93.9597315436242, 94.1275167785235, 
                                                                                            94.2953020134228, 94.4630872483222, 95.4697986577181, 95.6375838926175, 
                                                                                            95.8053691275168, 96.1409395973154, 96.6442953020134, 96.6442953020134, 
                                                                                            97.4832214765101, 97.8187919463087, 98.489932885906, 98.489932885906, 
                                                                                            98.489932885906, 98.489932885906, 98.8255033557047, 99.496644295302, 
                                                                                            99.6644295302013, 99.8322147651007, 99.8322147651007, 99.8322147651007
                                                                         )), row.names = c("thresh_info", "thresh_info.1", "thresh_info.2", 
                                                                                           "thresh_info.3", "thresh_info.4", "thresh_info.5", "thresh_info.6", 
                                                                                           "thresh_info.7", "thresh_info.8", "thresh_info.9", "thresh_info.10", 
                                                                                           "thresh_info.11", "thresh_info.12", "thresh_info.13", "thresh_info.14", 
                                                                                           "thresh_info.15", "thresh_info.16", "thresh_info.17", "thresh_info.18", 
                                                                                           "thresh_info.19", "thresh_info.20", "thresh_info.21", "thresh_info.22", 
                                                                                           "thresh_info.23", "thresh_info.24", "thresh_info.25", "thresh_info.26", 
                                                                                           "thresh_info.27", "thresh_info.28", "thresh_info.29", "thresh_info.30", 
                                                                                           "thresh_info.31", "thresh_info.32", "thresh_info.33", "thresh_info.34", 
                                                                                           "thresh_info.35", "thresh_info.36", "thresh_info.37", "thresh_info.38", 
                                                                                           "thresh_info.39", "thresh_info.40", "thresh_info.41", "thresh_info.42", 
                                                                                           "thresh_info.43", "thresh_info.44", "thresh_info.45", "thresh_info.46", 
                                                                                           "thresh_info.47", "thresh_info.48", "thresh_info.49", "thresh_info.50", 
                                                                                           "thresh_info.51", "thresh_info.52", "thresh_info.53", "thresh_info.54", 
                                                                                           "thresh_info.55", "thresh_info.56", "thresh_info.57", "thresh_info.58", 
                                                                                           "thresh_info.59", "thresh_info.60", "thresh_info.61", "thresh_info.62", 
                                                                                           "thresh_info.63", "thresh_info.64", "thresh_info.65", "thresh_info.66", 
                                                                                           "thresh_info.67", "thresh_info.68", "thresh_info.69", "thresh_info.70", 
                                                                                           "thresh_info.71", "thresh_info.72", "thresh_info.73", "thresh_info.74", 
                                                                                           "thresh_info.75", "thresh_info.76", "thresh_info.77", "thresh_info.78", 
                                                                                           "thresh_info.79", "thresh_info.80", "thresh_info.81", "thresh_info.82", 
                                                                                           "thresh_info.83", "thresh_info.84", "thresh_info.85", "thresh_info.86", 
                                                                                           "thresh_info.87", "thresh_info.88", "thresh_info.89", "thresh_info.90", 
                                                                                           "thresh_info.91", "thresh_info.92", "thresh_info.93", "thresh_info.94", 
                                                                                           "thresh_info.95", "thresh_info.96", "thresh_info.97", "thresh_info.98"
                                                                         ), class = "data.frame")

CodePudding user response:

You can filter by the minimum variance across the 3 columns:

library(dplyr)

data |> 
  tibble::rownames_to_column() |> 
  rowwise() |> 
  mutate(var = var(c_across(3:5))) |> 
  ungroup() |> 
  filter(var == min(var))

# A tibble: 1 × 6
  rowname        threshold accuracy sensitivity specificity    var
  <chr>              <dbl>    <dbl>       <dbl>       <dbl>  <dbl>
1 thresh_info.59       0.6     83.4        83.3        83.6 0.0199
  •  Tags:  
  • r
  • Related