我有一个字符串向量myStrings
- 在R中看起来像:
[1] download file from `http://example.com`
[2] this is the link to my website `another url`
[3] go to `another url` from more info.
Run Code Online (Sandbox Code Playgroud)
哪里another url
是有效的http网址,但stackoverflow不会让我插入多个网址,这就是我写作的原因another url
.我想删除所有的网址myStrings
,如下所示:
[1] download file from
[2] this is the link to my website
[3] go to from more info.
Run Code Online (Sandbox Code Playgroud)
我在stringr
包中尝试了很多功能,但没有任何作用.
我需要他们的第一个字符缩写部门名称,因此strDept="Department of Justice"
成为strDeptAbbr = "DoJ".
如何使用来缩写字符串stringr
?
谢谢
R中一个令人难以置信的基本问题但解决方案尚不清楚.
如何将一个字符向量分成单独的字符,即与paste(..., sep='')
或相反stringr::str_c()
?
比这更笨重的东西:
sapply(1:26, function(i) { substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,i) } )
"A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
Run Code Online (Sandbox Code Playgroud)
是否可以以其他方式完成,例如strsplit()
,stringr::*
或其他任何事情?
我想展平从HTML表中提取的列表.下面介绍一个最小的工作示例.该示例取决于stringr
R中的包.第一个示例表现出所需的行为.
years <- c("2005-", "2003-")
unlist(str_extract_all(years,"[[:digit:]]{4}"))
[1] "2005" "2003"
Run Code Online (Sandbox Code Playgroud)
当我尝试匹配一系列其他数字中的最后4位数时,下面的示例会产生不良结果.
years1 <- c("2005-", "2003-", "1984-1992, 1996-")
unlist(str_extract_all(years1,"[[:digit:]]{4}$"))
character(0)
Run Code Online (Sandbox Code Playgroud)
据我理解文档,我应该包含$
在模式的末尾,以便在字符串的末尾请求匹配.我更愿意从第二个例子中匹配数字,"2005","2003"和"1996".
我有以下数据集
> head(names$SAMPLE_ID)
[1] "Bacteria|Proteobacteria|Gammaproteobacteria|Pseudomonadales|Moraxellaceae|Acinetobacter|"
[2] "Bacteria|Firmicutes|Bacilli|Bacillales|Bacillaceae|Bacillus|"
[3] "Bacteria|Proteobacteria|Gammaproteobacteria|Pasteurellales|Pasteurellaceae|Haemophilus|"
[4] "Bacteria|Firmicutes|Bacilli|Lactobacillales|Streptococcaceae|Streptococcus|"
[5] "Bacteria|Firmicutes|Bacilli|Lactobacillales|Streptococcaceae|Streptococcus|"
[6] "Bacteria|Firmicutes|Bacilli|Lactobacillales|Streptococcaceae|Streptococcus|"
Run Code Online (Sandbox Code Playgroud)
我想提取||
作为新变量之间的最后一个词即
Acinetobacter
Bacillus
Haemophilus
Run Code Online (Sandbox Code Playgroud)
我试过用
library(stringr)
names$sample2 <- str_match(names$SAMPLE_ID, "|.*?|")
Run Code Online (Sandbox Code Playgroud) 我已经看到关于这个主题的SO上发布了几个类似的问题,但它们似乎措辞不当(例子)或者用不同的语言(例子).
在我的场景中,我认为白色空间所包围的一切都是一个词.表情符号,数字,字母串不是真正的单词,我不在乎.我只想获得一些关于找到的字符串的上下文,而不必读取整个文件来确定它是否是有效匹配.
我尝试使用以下内容,但如果您有一个很长的文本文件,则需要一段时间才能运行:
text <- "He served both as Attorney General and Lord Chancellor of England. After his death, he remained extremely influential through his works, especially as philosophical advocate and practitioner of the scientific method during the scientific revolution. Bacon has been called the father of empiricism.[6] His works argued for the possibility of scientific knowledge based only upon inductive and careful observation of events in nature. Most importantly, he argued this could be achieved …
Run Code Online (Sandbox Code Playgroud) 特定
str1 <- "0 1 1 2 2 3 3 4 0 4"
Run Code Online (Sandbox Code Playgroud)
我想要:
str2 <- "0 1\n1 2\n2 3\n3 4\n0 4"
Run Code Online (Sandbox Code Playgroud)
使用stringr的方法是什么?
我觉得这很奇怪:
pattern <- "[[:punct:][:digit:][:space:]]+"
string <- "a . , > 1 b"
gsub(pattern, " ", string)
# [1] "a b"
library(stringr)
str_replace_all(string, pattern, " ")
# [1] "a > b"
str_replace_all(string, "[[:punct:][:digit:][:space:]>]+", " ")
# [1] "a b"
Run Code Online (Sandbox Code Playgroud)
这是预期的吗?
我有一个字符变量(companies
),其观察结果如下所示:
我试图将这些字符串分成3部分:
"."
,"."
和下一个数字之间的所有内容(格式一致#.##
),以及#.##
).以第一个障碍为例,我想:"612","Grt.Am.CMt&Inv","5.01"
我尝试过定义模式rebus
并使用str_match
,但下面的代码仅适用于像obs#2和#3这样的情况.它并不反映字符串中间部分的所有变化以捕获其他障碍物.
pattern2 <- capture(one_or_more(DGT)) %R% DOT %R% SPC %R%
capture(or(one_or_more(WRD), one_or_more(WRD) %R% SPC
%R% one_or_more(WRD))) %R% SPC %R% capture(DGT %R% DOT
%R% one_or_more(DGT))
str_match(companies, pattern = pattern2)
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法将字符串分成这3个部分?
我不熟悉regex
,但我已经看到了很多建议(我是R和Stack Overflow的新手)
我想知道如何从文本字符串中获取唯一的字符数.假设我正在寻找重复单词中的苹果,香蕉,菠萝,葡萄的重复计数.
A<- c('I have a lot of pineapples, apples and grapes. One day the pineapples person gave the apples person two baskets of grapes')
df<- data.frame(A)
Run Code Online (Sandbox Code Playgroud)
假设我想获得文本中列出的所有水果的独特计数.
library(stringr)
df$fruituniquecount<- str_count(df$A, "apples|pineapples|grapes|bananas")
Run Code Online (Sandbox Code Playgroud)
我尝试了这个,但我得到了所有的计数.我希望答案为'3'.请提出您的想法.