Stack Exchange
Extract row after specific string in column in R
I have a messy data frame, called clarity, from which I need to extract specific rows.
This is the data frame:
time<-c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
rate<-c("clarity: markerPos = None","clarity: markerPos = 0.99", "clarity: markerPos = 0.99", "clarity: markerPos = 0.99", "clarity: markerPos = None", "clarity: markerPos = None", "clarity: markerPos = 0.99", "clarity: markerPos = 0.99", "clarity: markerPos = 0.99", "clarity: markerPos = 0.99", "clarity: markerPos = None", "clarity: markerPos = None","clarity: markerPos = 0.97","clarity: markerPos = 0.97", "clarity: markerPos = None")
df <- data.frame(time, rate)
df

Output:
   time                      rate
#     1 clarity: markerPos = None
#     2 clarity: markerPos = 0.99
#     3 clarity: markerPos = 0.99
#     4 clarity: markerPos = 0.99
#     5 clarity: markerPos = None
#     6 clarity: markerPos = None
#     7 clarity: markerPos = 0.99
#     8 clarity: markerPos = 0.99
#     9 clarity: markerPos = 0.99
#    10 clarity: markerPos = 0.99
#    11 clarity: markerPos = None
#    12 clarity: markerPos = None
#    13 clarity: markerPos = 1.97
#    14 clarity: markerPos = 1.97
#    15 clarity: markerPos = None

What I want is this:
   time                      rate
2     2 clarity: markerPos = 0.99
7     7 clarity: markerPos = 0.99
13   13 clarity: markerPos = 1.97

Therefore, I want to select the row with the numerical value that follows the "clarity: markerPos = None". The rows with the numerical values may be differently often repeated repeated and are not unique.
I can select those rows with the numericals:
df_new<-df[(str_detect(df$rate, "clarity: markerPos = 0.")|str_detect(df$rate,, "clarity: markerPos = 1.")),]

df_new

Output:
   time                      rate
#     2 clarity: markerPos = 0.99
#     3 clarity: markerPos = 0.99
#     4 clarity: markerPos = 0.99
#     7 clarity: markerPos = 0.99
#     8 clarity: markerPos = 0.99
#     9 clarity: markerPos = 0.99
#    10 clarity: markerPos = 0.99
#    13 clarity: markerPos = 1.97
#    14 clarity: markerPos = 1.97

But I am still faced with the repetitions. I therefore want to first select the row after the occurrence of "clarity: markerPos = None" which would give me a dataframe like this:
   time                      rate
#     2 clarity: markerPos = 0.99
#     6 clarity: markerPos = None
#     7 clarity: markerPos = 0.99
#    12 clarity: markerPos = None
#    13 clarity: markerPos = 1.97

And then I could simply apply a selection as shown above and would receive what I want:
   time                      rate
2     2 clarity: markerPos = 0.99
7     7 clarity: markerPos = 0.99
13   13 clarity: markerPos = 1.97

How can I select a row following a particular string in a column?

You can use grepl to search for the pattern "None", and specify a condition where the previous row (lag from dplyr package) has to contain the pattern while the row itself does not.
library(dplyr)

with(df, df[grepl("None", lag(rate)) & !grepl("None", rate), ])

Or implement it with dplyr style:
df %>% filter(grepl("None", lag(rate)) & !grepl("None", rate))

Output
   time                      rate
2     2 clarity: markerPos = 0.99
7     7 clarity: markerPos = 0.99
13   13 clarity: markerPos = 0.97


Elegant way of using lag to make it, +1!

Here is a base R option using regex to capture the desired positions
> with(df, df[gregexpr("10", paste0(+grepl("None$", rate), collapse = ""))[[1]] + 1, ])
   time                      rate
2     2 clarity: markerPos = 0.99
7     7 clarity: markerPos = 0.99
13   13 clarity: markerPos = 0.97


Base R solution:
subset(
  df[grepl("None", df$rate)[-1],],
  !grepl("None", rate)
)

