![](/img/trans.png)
[英]R/Stringr Extract String after nth occurrence of "_" and end with first occurrence of "_"
[英]R: Extract letters from a string only one time after the first occurrence
我正在尝试做一些相对简单的事情,但是我没有尝试过。 我已经用Google搜索了任务,但似乎无法使我的代码正常工作。
我仅尝试在以下句子中首次出现时提取它们:
text <- 'i ride my bike'
我尝试了以下代码:
gsub('([[:alpha:]])\\1{1}', '\\1', text)
并没有达到我想要的。 我希望最终结果是:
i r d e m y b k
我只想要这封信的第一次出现。 我在Google上发现的东西更像是将“ hellllllo”减少为“ hello”,但我想要“ helo”。 我希望我有道理!
感谢大家!
基于Badger和Gabor的答案,并使用grep
删除第一个空格:
grep(" ",unique(strsplit(text,"")[[1]]),value = TRUE, invert = TRUE)
[1] "i" "r" "d" "e" "m" "y" "b" "k"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.