Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Remove rows between two specific rows

$
0
0

I have a small issue regarding removing specific rows. In this example, I would like to remove the rows from the word "5055" in the column "power" until the word "Exer" in the column "fr". Importantly, I would like to apply this function in both id (Here, LM01-PRD-S1 and LB02-PRD-S1).

                   time   power        hr     fr          id

 1                  <NA>  5055       Zoti      E LM01-PRD-S1
 2              747 mmHg  <NA>       09/0   2016 LM01-PRD-S1
 3 9.7222222222222224E-3     0         76     20 LM01-PRD-S1
 4  2.013888888888889E-2     0         77     16 LM01-PRD-S1
 5 2.9861111111111113E-2     0         77     17 LM01-PRD-S1
 6                  <NA>  <NA>       <NA>   Exer LM01-PRD-S1
 7 1.0416666666666666E-2    25         90     24 LM01-PRD-S1
 8 1.9444444444444445E-2    25         92     23 LM01-PRD-S1
 9 3.0555555555555555E-2    25         93     22 LM01-PRD-S1
10                  <NA>  5055       Zoti      E LB02-PRD-S1
11              750 mmHg  <NA>       11/0   2016 LB02-PRD-S1
12 8.3333333333333332E-3     0         81     14 LB02-PRD-S1
13 1.6666666666666666E-2     0         96     15 LB02-PRD-S1
14 2.8472222222222222E-2     0         71     14 LB02-PRD-S1
15                  <NA>  <NA>       <NA>   Exer LB02-PRD-S1
16 1.0416666666666666E-2    35        102     16 LB02-PRD-S1
17 1.9444444444444445E-2    35        101     17 LB02-PRD-S1
18 3.0555555555555555E-2    35        105     15 LB02-PRD-S1

I tried this function, but I removed rows 1 to 15, while I would like to remove only rows 1 to 6 and 10 to 15.

df[-c(min(grep("5055",df[,power])):max(grep("Exer",df[,fr]))),]

Here is the final result I would like to obtain.

                   time power    hr    fr          id
1 1.0416666666666666E-2    25    90    24 LM01-PRD-S1
2 1.9444444444444445E-2    25    92    23 LM01-PRD-S1
3 3.0555555555555555E-2    25    93    22 LM01-PRD-S1
4 1.0416666666666666E-2    35   102    16 LB02-PRD-S1
5 1.9444444444444445E-2    35   101    17 LB02-PRD-S1
6 3.0555555555555555E-2    35   105    15 LB02-PRD-S1

I hope I explained well. Thank you for your help!


Viewing all articles
Browse latest Browse all 201839

Trending Articles