0

I have a command like this which finds a minimum value in column e$V2.

EQTLs<-s[s$POS==as.numeric(e[tail(which(e$V2 == min(e$V2)), 1),1]),]

Instead I would like to find the first 10 minimum values as oppose to this one.

I tried this but it didn't work:

EQTLs<-s[s$POS==as.numeric(e[tail(which(head(sort(e$V2),10), 1),1]),]

> head(e,20)
       V1           V2       V3       V4           V5           V6
 1: 179872750 2.176972e-01 34.75615 35.92840 -0.022011224 3.066414e-10
 2: 179873140 9.124538e-01 48.50275 48.52209 -0.001859901 1.702842e-12
 3: 179873673 4.592087e-05 33.15652 42.61323 -0.049702451 5.753929e-11
 4: 179873686 7.770025e-02 38.12148 40.26651 -0.025660347 1.234509e-09
 5: 179874581 9.088407e-01 51.70045 51.72127 -0.001846114 4.557570e-11
 6: 179875714 4.771981e-01 38.22935 38.70188 -0.011592859 1.319859e-10
 7: 179876164 8.203991e-02 35.42617 37.51915 -0.027269625 8.727276e-11
 8: 179876351 2.082161e-01 34.87287 36.08634 -0.022352006 3.967887e-11
 9: 179876509 2.079911e-01 34.87456 36.08902 -0.022389988 9.564772e-11
 10: 179876514 2.080461e-01 34.87454 36.08876 -0.022363648 1.265952e-11
 11: 179877324 3.353371e-02 37.78757 40.74353 -0.030421570 1.221123e-11
 12: 179877461 8.841961e-01 51.54780 51.57990 -0.002320621 1.055949e-10
 13: 179877493 8.521198e-01 51.74138 51.79103 -0.002830585 1.418604e-11
 14: 179877963 8.551982e-01 51.71526 51.76309 -0.002781394 1.221663e-10
 15: 179879790 5.040575e-01 42.38667 42.81348 -0.010884511 4.571836e-11
 16: 179879792 5.859217e-01 41.06875 41.37379 -0.009007441 3.462620e-10
 17: 179879794 6.934742e-01 41.10470 41.28307 -0.009437604 8.207510e-04
 18: 179879828 9.281022e-01 45.52744 45.54092  0.001941842 2.656116e-10
 19: 179879936 6.349573e-01 39.32128 39.56432  0.008749797 6.662635e-10
 20: 179880025 9.328145e-03 43.41904 47.62383  0.037273686 6.820883e-11

    > head(s,20)
                ID CHROM   POS
 1:    1_30923_G_T_b37     1 30923
 2:    1_51479_T_A_b37     1 51479
 3:    1_52058_G_C_b37     1 52058
 4:    1_52238_T_G_b37     1 52238
 5:    1_54490_G_A_b37     1 54490
 6:    1_54753_T_G_b37     1 54753
 7:    1_55164_C_A_b37     1 55164
 8:    1_55299_C_T_b37     1 55299
9:    1_55326_T_C_b37     1 55326
10:    1_57952_A_C_b37     1 57952
11:    1_58814_G_A_b37     1 58814
12:    1_61442_A_G_b37     1 61442
13:    1_61462_T_A_b37     1 61462
14:    1_63671_G_A_b37     1 63671
15: 1_63735_CCTA_C_b37     1 63735
16:    1_66162_A_T_b37     1 66162
17:    1_66331_A_C_b37     1 66331
18:    1_67181_A_G_b37     1 67181
19:    1_69511_A_G_b37     1 69511
20: 1_72297_G_GTAT_b37     1 72297

But you can disregard matching in s table for a minute as it is unlikely to work with this data subset.

user2458189
  • 93
  • 16
  • 2
    Please provide a [reproducible example in r](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). The link I provided, will tell you how. Cheers. – M-- Jan 02 '19 at 21:07
  • provide output of `dput(head(s,20))` and `dput(head(e,20))` instead of copy/pasting them. I strongly recommend reading the link I provided which can help you to ask better questions even in future. – M-- Jan 02 '19 at 21:24

1 Answers1

1

I cannot quite follow the logic of your first bit of code, but it appears to be aiming at extracting the rows of a dataframe, named s, in which a particular column, named V2, is at a minimum in a different dataframe named e. (I cannot figure out what role the POS column is supposed to be playing. So I'm going to assume you instead want the 10 rows (in s) where e$V2 assumes its ten lowest values. This would simple be:

 EQTLs <- s[ order(e$V2)[1:10] , ]

The order function returns positions of original values that would result in sorted vectors when applied to the original vector as a numeric index.

I'm further guessing that you cannot use the head of 2 much larger dataframes for testing of this strategy, so if you want a demonstration for testing you might want to display the results of:

  dput( s[ order(s$V2)[1:20]  , ] ) # rows of s with 20 lowest V2 values
  dput( e[ order(s$V2)[1:20]  , ] ) # reproducible version of e with same row nums
IRTFM
  • 258,963
  • 21
  • 364
  • 487