stringlines <- as.list(c(
"Total des actifs immobilisés 350 952", "Total des actifs non courants 357 268",
"Total des actifs courants 4 324 646",
"Total des actifs 4 682 115"
))
# if index of last element is known
stringlines[[4]]
#> [1] "Total des actifs 4 682 115"
# otherwise
stringlines[[length(stringlines)]]
#> [1] "Total des actifs 4 682 115"
# use to return index match
grep("Total des actifs",stringlines[[4]],fixed = TRUE)
#> [1] 1
# use to extract that match only
stringlines[[4]][grep("Total des actifs",stringlines[[3]],fixed = TRUE)]
#> [1] "Total des actifs 4 682 115"
# simpler
library(magrittr)
if(stringlines[[4]] %>% grep("Total des actifs",.,fixed = TRUE)) stringlines[[4]] else print("not found")
#> [1] "Total des actifs 4 682 115"
As of R4;.10.0, the new |> operator can be used in lieu of %>% and the magrittr library will be unnecessary in this example.