[英]Extract strings from character vector in R from/to specific words
Here is an example of a character vector that I have, I need to extract all strings between "## Code:" and "## My table".这是我拥有的字符向量示例,我需要提取“## Code:”和“## 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[4:8]
的位置来执行此操作,但是要提取的代码长度会有所不同,因此我需要一个不依赖于固定位置的解决方案。
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,))", "}")
新的字符向量应该包含:
c("temp(){", "x = a1 + b1", "a1(b1 = 3)", "tf(cov = c(,1,))", "}")
Thanks for helping me arrive at a solution.感谢您帮助我找到解决方案。
We can create a logical index with ==
, and then wrap with which
我们可以用
==
创建一个逻辑索引,然后用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或者用
which
找到开始和结束位置并使用:
来获取提取元素的位置序列
text[(which(text == "## Code:")[1] +1):(which(text == "## My table")-1)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.