Skip to content
Prev 309598 / 398506 Next

subset a defined row plus the aforegoing

On Thu, Nov 1, 2012 at 10:28 AM, Hermann Norpois
<hnorpois at googlemail.com> wrote:
With enough money and manpower, everything is possible.

This one is possible even without a whole lot of manpower or money :)
First, get rid of all rows that are neither expression not DNase since
you don't seem to want those:

df1 = df[ df$type %in% c("Expresssion", "DNase HS"), ];

#Then select all Expression rows and the immediately preceding DNase HS rows:

keep.expr = df1$type=="Expresssion";
n = nrow(df1)
keep.DNase = c(df1$type[-1]=="Expresssion" & df1$type[-n]=="DNase HS", FALSE)

# This is the result you want
result = df1[keep.expr | keep.DNase, ];

# Applied to your example:
   start.ens fc.trans        type end.ens peak end.grcm38 dpeak
1    9191942   0.9379 Expresssion      NA   NA         NA    NA
2    9191942   0.9741 Expresssion      NA   NA         NA    NA
3    9191942   0.9748 Expresssion      NA   NA         NA    NA
7   11113787       NA    DNase HS      NA   NA   11114262   279
8   11114744   0.9803 Expresssion      NA   NA         NA    NA
9   11114744   0.9904 Expresssion      NA   NA         NA    NA
12  11461513       NA    DNase HS      NA   NA   11462571   508
13  11462408   1.0129 Expresssion      NA   NA         NA    NA
14  11462408   1.0074 Expresssion      NA   NA         NA    NA
15  11489266   1.0019 Expresssion      NA   NA         NA    NA


I have to say though, the programming would be easier if you didn't
spell expression with a triple s :)

HTH,

Peter