Search code examples
rdataframedplyrplyr

FIlter the top 50 values from multiple colums in R


I got an R dataframe with several hundred rows and a total of 6 columns. The first column is my row description, the second one a numeric variable and the columns 3 to 6 are my scientific measurements of my four different treatments (thus four different columns).

I would now like to get the highest 50 value across all rows of all four columns containing the numeric measurements with the corresponding first two columns attached.

So far I've tried:

mat.df <- slice_max(mat.df, mat.df[,c(3:6)], n=50, prop, with_ties = TRUE)

but this does not give my the highest 50 results from all four columns.

Any ideas?

Any help would be greatly appreciated. Thanks in advance! :)

Max


Solution

  • EDITED to generate some data like yours more rows and similar values. Notice that order_by is actually a formula and you can adjust whether you want absolute values or something else

    library(dplyr)
    
    mat.df %>% 
      slice_max(order_by = ControlAliette + ControlLuna + ControlMovento + ControlSerenade, 
                n = 50, 
                with_ties = FALSE)
    #>    Genus Intercept ControlAliette ControlLuna ControlMovento ControlSerenade
    #> 1      w  6.435287    0.056526489 -0.08267739    0.079620300      1.09842783
    #> 2      c  6.079201    0.558397153 -0.16290829    0.554822034      0.11534424
    #> 3      m  6.192000    0.241815318 -0.11150510    0.088925592      0.82798422
    #> 4      x  5.392598    0.105206847  0.11572941    0.043688356      0.73782691
    #> 5      s  6.446660    0.650486353 -0.02469053    0.191842753      0.17521118
    #> 6      n  5.296171    0.366777627 -0.20989214    0.749322631      0.05003869
    #> 7      q  5.282046   -0.218363044  0.13508214    0.720711704      0.26279334
    #> 8      r  6.149206    0.581825377 -0.17562109   -0.090843851      0.58309241
    #> 9      h  6.925687    0.417453587 -0.20572095    0.321314117      0.36169818
    #> 10     m  5.691742    0.285472121 -0.05085905    0.137731401      0.51481684
    #> 11     c  5.145111    0.002638383  0.28201133    0.049834715      0.54417127
    #> 12     c  5.307666    0.073090636  0.06610125    0.426498084      0.30129010
    #> 13     w  5.260718    0.048086123  0.07908467    0.358431527      0.35255379
    #> 14     m  6.211825    0.180772350  0.44305053    0.445286789     -0.26957951
    #> 15     x  5.227344    0.225538161  0.16067200    0.002541409      0.37038387
    #> 16     u  5.723938   -0.332155838 -0.12333414    0.454581607      0.75518647
    #> 17     t  5.152110   -0.282558292 -0.03498565    0.531423513      0.53623335
    #> 18     a  6.526487    0.229463088  0.36144873    0.377937318     -0.23017497
    #> 19     k  6.757341    0.516884599  0.49018540   -0.021555273     -0.25513704
    #> 20     v  5.446486   -0.064045063  0.04287848    0.563663731      0.15906521
    #> 21     o  5.070087    0.170030563  0.10599479    0.398073757      0.02670369
    #> 22     t  5.586618   -0.162407261  0.30959608    0.784672641     -0.23755880
    #> 23     w  6.184066    0.295550746  0.23812025   -0.117498910      0.26837228
    #> 24     d  5.848479    0.033128367  0.12548966    0.114049621      0.38727744
    #> 25     b  5.544807    0.007965195  0.00866282    0.335334827      0.28700183
    #> 26     n  5.251765    0.444732965 -0.09725644   -0.387806585      0.67485325
    #> 27     x  6.123222    0.158880473 -0.40684437   -0.110981338      0.99074654
    #> 28     g  5.259382   -0.154187237  0.30824362   -0.365527503      0.84178062
    #> 29     g  6.496157   -0.053255524  0.18689232    0.181498712      0.31483353
    #> 30     q  6.770110   -0.162705871  0.03580742    0.513900306      0.24142638
    #> 31     r  5.901752   -0.062622025 -0.05764049   -0.101135091      0.81935090
    #> 32     d  6.549394    0.380371776  0.02137438   -0.131768823      0.29908691
    #> 33     b  6.675606   -0.130378318 -0.07211543    0.397251508      0.37068326
    #> 34     s  5.110820    0.229168985  0.24135752    0.049987817     -0.01836014
    #> 35     j  5.944663    0.251562062 -0.09888048    0.237664053      0.10494435
    #> 36     m  6.547734    0.112588977 -0.12150620   -0.149065617      0.64185923
    #> 37     r  6.560016    0.010965496 -0.26502303    0.162376862      0.55101225
    #> 38     h  5.922249    0.398995408  0.31074350   -0.164695748     -0.09995690
    #> 39     t  6.708726   -0.081686217  0.32626594    0.487625193     -0.32319336
    #> 40     e  6.483050    0.455495878 -0.15995120   -0.014532759      0.12664553
    #> 41     q  6.945471    0.032406517  0.02001101    0.652315538     -0.29794823
    #> 42     s  5.800246    0.081368176  0.44097023    0.359698727     -0.49149988
    #> 43     h  6.186769   -0.127186068  0.10296196   -0.117877929      0.52542571
    #> 44     t  5.799786    0.359494314  0.30821149    0.213104574     -0.50623447
    #> 45     o  5.592515   -0.072610242 -0.09783531    0.496258542      0.04371708
    #> 46     x  5.605788    0.095883060  0.18309971    0.247253420     -0.15940040
    #> 47     l  5.124376   -0.082027992  0.29219376    0.245457652     -0.09446580
    #> 48     x  5.993785    0.081067454 -0.31210067    0.180427362      0.40128945
    #> 49     i  6.691422   -0.169410111  0.33240079   -0.238838985      0.41678546
    #> 50     q  6.706284    0.460427014 -0.34032290   -0.285672344      0.50624378
    
    mat.df %>% 
      slice_max(order_by = abs(ControlAliette) + abs(ControlLuna) + abs(ControlMovento) + abs(ControlSerenade), 
                n = 50, 
                with_ties = FALSE)
    #>    Genus Intercept ControlAliette  ControlLuna ControlMovento ControlSerenade
    #> 1      w  6.163855    -0.01602246 -0.139432692     1.11086916     -0.66393207
    #> 2      z  6.344662    -0.56700045  0.132946953    -0.64180401      0.57921853
    #> 3      v  6.388810    -0.77792884 -0.187690390    -0.26208884      0.64532993
    #> 4      g  5.179393     0.12719675  0.488018448    -0.94983749      0.15642056
    #> 5      d  5.475559    -0.10763699 -0.251141467    -0.32858552     -0.99127322
    #> 6      g  5.259382    -0.15418724  0.308243618    -0.36552750      0.84178062
    #> 7      x  6.123222     0.15888047 -0.406844369    -0.11098134      0.99074654
    #> 8      u  5.723938    -0.33215584 -0.123334142     0.45458161      0.75518647
    #> 9      v  6.623464    -0.51131603  0.201377070     0.30133355     -0.59963009
    #> 10     n  5.251765     0.44473297 -0.097256441    -0.38780658      0.67485325
    #> 11     q  6.706284     0.46042701 -0.340322896    -0.28567234      0.50624378
    #> 12     p  6.111853    -0.27896210  0.104354182    -0.68234909     -0.52491266
    #> 13     i  5.024287     0.01636362 -0.392316890     0.68981739     -0.40043887
    #> 14     t  5.586618    -0.16240726  0.309596080     0.78467264     -0.23755880
    #> 15     r  6.424762    -0.27752663  0.263024479     0.55815724     -0.37006130
    #> 16     l  6.219644     0.47414209 -0.133076705    -0.16541590     -0.68872026
    #> 17     p  5.256076    -0.30643889 -0.364943892    -0.39541292     -0.37877951
    #> 18     r  6.149206     0.58182538 -0.175621089    -0.09084385      0.58309241
    #> 19     f  6.971580     0.14013934  0.252297801    -0.67945341      0.35465739
    #> 20     c  6.079201     0.55839715 -0.162908293     0.55482203      0.11534424
    #> 21     t  5.799786     0.35949431  0.308211494     0.21310457     -0.50623447
    #> 22     t  5.152110    -0.28255829 -0.034985649     0.53142351      0.53623335
    #> 23     n  5.296171     0.36677763 -0.209892141     0.74932263      0.05003869
    #> 24     s  5.800246     0.08136818  0.440970230     0.35969873     -0.49149988
    #> 25     m  5.734308    -0.50240458 -0.294817028    -0.27977801     -0.29563500
    #> 26     m  6.211825     0.18077235  0.443050531     0.44528679     -0.26957951
    #> 27     q  5.282046    -0.21836304  0.135082142     0.72071170      0.26279334
    #> 28     k  6.724188    -0.21118584  0.313290229    -0.20714810     -0.60087592
    #> 29     a  5.937355     0.27092841  0.089738018    -0.37856526     -0.57958569
    #> 30     w  6.435287     0.05652649 -0.082677388     0.07962030      1.09842783
    #> 31     x  6.437799     0.63411108 -0.330220416    -0.06455209     -0.28624028
    #> 32     h  6.925687     0.41745359 -0.205720947     0.32131412      0.36169818
    #> 33     q  6.912286     0.27655245 -0.285097288     0.24735970     -0.48033045
    #> 34     k  6.757341     0.51688460  0.490185399    -0.02155527     -0.25513704
    #> 35     h  5.782539    -0.12631548 -0.053706678    -0.48881997     -0.61186837
    #> 36     a  5.053820     0.21205072 -0.532491520     0.29227655      0.23504310
    #> 37     m  6.192000     0.24181532 -0.111505098     0.08892559      0.82798422
    #> 38     e  5.241864    -0.63895794 -0.244007611     0.24505811      0.13904614
    #> 39     m  5.494797    -0.17277764 -0.002460733    -0.41205105     -0.66710689
    #> 40     n  5.178992    -0.50196726 -0.123846277     0.38787254     -0.21968941
    #> 41     m  6.469092    -0.47968618 -0.161308327    -0.54584551     -0.04310575
    #> 42     t  6.708726    -0.08168622  0.326265943     0.48762519     -0.32319336
    #> 43     r  6.076358     0.22051300 -0.293593812     0.22617724     -0.47817442
    #> 44     a  6.526487     0.22946309  0.361448730     0.37793732     -0.23017497
    #> 45     j  6.292186    -0.65160300  0.201960978    -0.07772339     -0.25767368
    #> 46     j  5.818572     0.04629336 -0.108579717    -0.36723803      0.66664097
    #> 47     k  5.048105    -0.33966657  0.246071452    -0.47745520     -0.11651867
    #> 48     o  6.491382    -0.16081949  0.091642534     0.31591590     -0.59354074
    #> 49     v  6.795716    -0.03606983  0.008426413     0.26267357     -0.85435700
    #> 50     x  6.265645    -0.41563103  0.165907772    -0.50745799      0.07073063
    

    Your data simulated

    set.seed(2020)
    mat.df <- data.frame(
    Genus = sample(letters, size = 200, replace = TRUE),
    Intercept = runif(n = 200, min = 5, max = 7),
    ControlAliette = rnorm(n = 200, mean = 0, sd = .25),
    ControlLuna = rnorm(n = 200, mean = 0, sd = .2),
    ControlMovento = rnorm(n = 200, mean = 0, sd = .3), 
    ControlSerenade = rnorm(n = 200, mean = 0, sd = .4)
    )