Reputation: 131
Here is an example of a character vector that I have, I need to extract all strings between "## Code:" and "## My table". I understand I can do this by position using text[4:8]
, however the length of code to extract will vary so I need a solution that doesn't depend on fixed position.
text <- c("## Author: user", "## Data = data.txt", "## Code:", "temp(){", "x = a1 + b1", "a1(b1 = 3)", "tf(cov = c(,1,))", "}", "## My table")
The new character vector should contain this:
c("temp(){", "x = a1 + b1", "a1(b1 = 3)", "tf(cov = c(,1,))", "}")
Thanks for helping me arrive at a solution.
Upvotes: 1
Views: 356
Reputation: 887118
We can create a logical index with ==
, and then wrap with which
text[which(cumsum(text == "## Code:"|text == "## My table") == 1)[-1]]
#[1] "temp(){" "x = a1 + b1" "a1(b1 = 3)" "tf(cov = c(,1,))" "}"
Or with which
find the start and end locations and use :
to get the sequence of positions for extracting the elements
text[(which(text == "## Code:")[1] +1):(which(text == "## My table")-1)]
Upvotes: 1