Home > Mobile >  extracting a row below a conditional statement (using a string search)
extracting a row below a conditional statement (using a string search)

Time:10-31

I was wondering if somebody could help me out with a line of code i've been having trouble with. I am trying to extract the row below every row that contains the string "=".

sagarin2 #my data

##output sample
#
# 2 Baylor =
# 93.44
# 91.43
# 3 Kansas =
# 90.86
# 00.00

I can extract the rows containing "=" by using the following code:

filter(sagarin2, grepl("=", ranking, fixed = TRUE))

##output sample
#
#2 Baylor =
#3 Kansas =

However, I am unsure how to call the row below Baylor and Kansas (93.44 and 90.86 respectively).

If anybody can help out, please let me know. I'm nearing the end of an undergraduate project and I could really use some help!

I tried using a the which() function and I also tried creating my own function with no luck. There are a couple of other posts that relate to this topic but I've been trying for a while and can't get any to work.

CodePudding user response:

vec <- c("2 Baylor =",
         "93.44",
         "91.43",
         "3 Kansas =",
         "90.86",
         "00.00")

output <- vec[dplyr::lag(grepl("=", vec))]
output[!is.na(output)]

"93.44" "90.86"
  • Related