我有一个混合字母和数字的字符串:
"The sample is 22mg"
Run Code Online (Sandbox Code Playgroud)
我想分割字符串,其中一个数字紧跟着这样的字母:
"The sample is 22 mg"
Run Code Online (Sandbox Code Playgroud)
我试过这个:
gsub('[0-9]+[[aA-zZ]]', '[0-9]+ [[aA-zZ]]', 'This is a test 22mg')
Run Code Online (Sandbox Code Playgroud)
但是没有得到预期的结果.
有什么建议?
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".
有这种奇怪的行为stringr
,这真让我烦恼.stringr
在没有警告的情况下更改包含异国字符的某些字符串的编码,在我的情况下ø,å,æ,é和其他一些......如果你str_trim
是一个字符矢量,那么具有异国情调字母的字符将被转换为新的编码.
letter1 <- readline('Gimme an ASCII character!') # try q or a
letter2 <- readline('Gimme an non-ASCII character!') # try ø or é
Letters <- c(letter1, letter2)
Encoding(Letters) # 'unknown'
Encoding(str_trim(Letters)) # mixed 'unknown' and 'UTF-8'
Run Code Online (Sandbox Code Playgroud)
这是一个问题,因为我使用data.table来快速合并大表,而data.table不支持混合编码,因为我找不到回归统一编码的方法.
任何解决方法?
编辑:我以为我可以回到基本功能,但他们不保护编码.paste
保留它,但不是sub
例如.
Encoding(paste(' ', Letters)) # 'unknown'
Encoding(str_c(' ', Letters)) # mixed
Encoding(sub('^ +', '', paste(' ', Letters))) # mixed
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) 我想从字符串中提取年份.这总是在"X"之后和"之前".然后是一串其他字符.
使用stringr的str_extract我正在尝试以下方法:
year = str_extract(string = 'X2015.XML.Outgoing.pounds..millions.'
, pattern = 'X(\\d{4})\\.')
Run Code Online (Sandbox Code Playgroud)
我认为括号会定义捕获组,返回stringr
,但实际上我得到完全匹配str_extract
我这样做了吗?为什么我不修剪"X"和"."?
假设我有一个长字符串:pneumonoultramicroscopicsilicovolcanoconiosis。我想stringr::str_replace_all
用其他字母替换某些字母。根据文档,str_replace_all
可以采用命名向量并用值替换名称。这适用于 1 次替换,但对于多次替换,它似乎是迭代进行的,因此结果是对上一次迭代的替换。我不确定这是预期的行为。
library(tidyverse)
text_string = "developer"
text_string %>%
str_replace_all(c(e ="X")) #this works fine
[1] "dXvXlopXr"
text_string %>%
str_replace_all(c(e ="p", p = "e")) #not intended behaviour
[1] "develoeer"
Run Code Online (Sandbox Code Playgroud)
想要的结果:
[1] "dpvploepr"
Run Code Online (Sandbox Code Playgroud)
我通过引入一个新角色得到:
text_string %>%
str_replace_all(c(e ="X", p = "e", X = "p"))
Run Code Online (Sandbox Code Playgroud)
这是一个可用的解决方法,但很难推广。这是一个错误还是我的期望错误?
我还希望能够同时用n 个其他字母替换n个字母,最好使用两个向量(如“旧”和“新”)或命名向量作为输入。
reprex 已编辑以便于人类阅读
我觉得这很奇怪:
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的新手)
我有以下数据:
dat <- data.frame(x = c("this is my example text", "and here is my other text example", "my other text is short"),
some_other_cols = c(1, 2, 2))
Run Code Online (Sandbox Code Playgroud)
此外,我有以下模式向量:
my_patterns <- c("my example", "is my", "my other text")
Run Code Online (Sandbox Code Playgroud)
我想实现的是,以消除任何文本my_patterns
发生在dat$x
。
我尝试了下面的解决方案,但问题是,一旦我从文本中删除第一个模式(此处:“我的示例”),我的解决方案就无法检测到第二个模式的出现(此处:“是我的”) ) 或第三种模式了。
错误的解决方法:
library(tidyverse)
my_patterns_c <- str_c(my_patterns, collapse = "|")
dat_new <- dat %>%
mutate(short_x = str_replace_all(x, pattern = my_patterns_c, replacement = ""))
Run Code Online (Sandbox Code Playgroud)
我想我可以做某事。就像遍历所有模式一样,收集 dat$x 中与我的模式匹配的字符串位置,然后将它们组合成一个范围并从文本中删除该范围。例如,我将列添加到我dat
喜欢的数据帧start_pattern_1
和end_pattern_1
等。因此,对于第一行 1,我得到第一个模式的 9(开始)和 18(结束),第二个模式的 6/10。然后我需要检查是否有任何end …
r ×10
stringr ×10
regex ×7
data.table ×1
encoding ×1
gsub ×1
paste ×1
str-replace ×1
string ×1
string-split ×1