我正在尝试str_extract_all从stringr包中使用R中的某些文本提取值,并且我想使用perl的regexp中的非匹配组(?:...)来提取和清除一行中的相关值.
运行此代码时:
library(stringr)
## Example string.
## Not the real string, but I get the same results with this one.
x <- 'WIDTH 4\nsome text that should not be matched.\n\nWIDTH 46 some text.'
## extract values
str_extract_all(x, perl('(?:WIDTH\\s+)[0-9]+'))
Run Code Online (Sandbox Code Playgroud)
我想得到这个结果:
[[1]]
[1] "4" "46"
Run Code Online (Sandbox Code Playgroud)
但我明白了:
[[1]]
[1] "WIDTH 4" "WIDTH 46"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我在一个大型数据集中有一个字符串变量,我希望根据设置的字符串列表进行清理.恩.pattern < - c("dog","cat")但我的列表大约有400个元素.
vector_to_clean == a
black Dog
white dOG
doggie
black CAT
thatdamcat
Run Code Online (Sandbox Code Playgroud)
然后我想应用一个函数来产生
新
dog
dog
dog
cat
cat
Run Code Online (Sandbox Code Playgroud)
我已经尝试过str_extract,grep,grepl等.因为我可以一次选择一个基于一个字符串的模式.我想我想要的是使用其中一个文本清理功能.不幸的是,我被困住了.以下是我最近的尝试.谢谢您的帮助!
new <- vector()
lapply(pattern, function(x){
where<- grep(x,a,value = FALSE, ignore.case = TRUE)
new[where]<-x
})
Run Code Online (Sandbox Code Playgroud) 给定一个字符串,
mystr = "Average student score 88"
Run Code Online (Sandbox Code Playgroud)
如果有超过1个空格,我希望拆分.我希望获得以下内容:
"Average student score" "88"
Run Code Online (Sandbox Code Playgroud)
我搜索过"\ s +"会被任意数量的空格分开.
strsplit(mystr, "\\s+")
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的.在strsplit中是否有任何选项可以根据一定数量的空格(比如空格= k)或空格上的规则(比如空格> 1)来拆分字符串?
我在数据帧(df)中有一堆文本,通常在1列中包含三行地址,我的目标是提取区域(文本的中心部分),例如:
73 Greenhill Gardens, Wandsworth, London
22 Acacia Heights, Lambeth, London
Run Code Online (Sandbox Code Playgroud)
幸运的是,在95%的情况下,输入数据的人使用逗号分隔我想要的文本,其中100%的时间结束",伦敦"(即逗号空间伦敦).为了清楚地说明事情,我的目标是在",伦敦"和之前的逗号之后提取文本
我想要的输出是:
Wandsworth
Lambeth
Run Code Online (Sandbox Code Playgroud)
我之前可以设法提取部分:
df$extraction <- sub('.*,\\s*','',address)
Run Code Online (Sandbox Code Playgroud)
之后
df$extraction <- sub('.*,\\s*','',address)
Run Code Online (Sandbox Code Playgroud)
但不是我需要的中间部分.有人可以帮忙吗?
非常感谢!
我想在两个其他字符串之间提取一个字符串.一个字符串是回车符,而另一个字符串是几乎相似字符的变体:
dput(head(decisions$Title))
c("Zinaida Shumilina et al. v. Belarus \r\n
CCPR/C/120/D/2142/2012",
"K.E.R. vs. Canada \r\n
CCPR/C/120/D/2196/2012",
"Lounis Khelifati v Algeria \r\n
CCPR/C/120/D/2267/2013",
"Hibaq Said Hash v. Denmark \r\n
CCPR/C/120/D/2470/2014",
"Anton Batanov v. Russian Federation \r\n
CCPR/C/120/D/2532/2015",
"S. Z. v. Denmark \r\n
CCPR/C/120/D/2625/2015"
)
Run Code Online (Sandbox Code Playgroud)
我基本上想要在"v."之间提取国家名称.和回车\ r.但是,"v." 有时是"v","vs.","vs"和"v:".
基于相关SO问题的答案,我尝试了以下方法:
res <- str_match(decisions$Title, "(v\\.|vs\\.|v)(.*?)\\r")
res[,3]
Run Code Online (Sandbox Code Playgroud)
不幸的是,这并没有得到所有的变化,或者在某些情况下,当试图从"Navruz Tahirovich Nasyrlayev诉土库曼斯坦CCPR/C/117/D /"中提取国名时,它会返回诸如"ruz Tahirovich Nasyrlayev诉土库曼斯坦"之类的数据.二千零十二分之二千二百十九" .
还有另一种方法来实现这一目标吗?
如何计算字符串向量中的尾随零.例如,如果我的字符串向量是:
x = c('0000','1200','1301','X230','9900')
Run Code Online (Sandbox Code Playgroud)
答案应该是
> numZeros
[1] 4 2 0 1 2
Run Code Online (Sandbox Code Playgroud)
我不想使用多个,ifelse因为我认为应该存在更优雅和更快的解决方案.我尝试使用模数,就像这样
y = as.integer(x)
numZeros = (!(y%%10000))+(!(y%%1000))+(!(y%%100))+(!(y%%10))
Run Code Online (Sandbox Code Playgroud)
但这需要两个条件才能成真.
然后使用stringr包并创建了一个解决方案,但它非常冗长.
library(stringr)
numZeros =
4*str_detect(x,"0000") +
3*str_detect(x,"[1-9 A-Z]000") +
2*str_detect(x,"[1-9 A-Z]{2}00") +
str_detect(x,"[1-9 A-Z]{3}0")
Run Code Online (Sandbox Code Playgroud)
另外,我无法通过查看定义来弄清楚是否str_detect使用.ifelsestr_detect
考虑这个简单的例子
library(stringr)
library(dplyr)
dataframe <- data_frame(text = c('how is the biggest ??',
'really amazing stuff'))
# A tibble: 2 x 1
text
<chr>
1 how is the biggest ??
2 really amazing stuff
Run Code Online (Sandbox Code Playgroud)
我需要基于regex表达式提取一些术语,但仅提取最长的术语。
到目前为止,我只能使用提取第一个匹配项(不需要最长的匹配项)str_extract。
> dataframe %>% mutate(mymatch = str_extract(text, regex('\\w+')))
# A tibble: 2 x 2
text mymatch
<chr> <chr>
1 how is the biggest ?? how
2 really amazing stuff really
Run Code Online (Sandbox Code Playgroud)
我尝试一起玩,str_extract_all但是找不到有效的语法。输出应为:
# A tibble: 2 x 2
text …Run Code Online (Sandbox Code Playgroud) 假设我有一个字符串向量,如下所示:
vector<-c("hi, how are you doing?",
"what time is it?",
"the sky is blue",
"hi, how are you doing today? You seem tired.",
"walk the dog",
"the grass is green",
"the sky is blue during the day")
vector
[1] "hi, how are you doing?"
[2] "what time is it?"
[3] "the sky is blue"
[4] "hi, how are you doing today? You seem tired."
[5] "walk the dog"
[6] "the grass is green"
[7] "the sky is blue during the …Run Code Online (Sandbox Code Playgroud) 我知道我可以轻松编写一个,但是没有人知道stringer(或stringi)是否已经具有一个函数,该函数将一个或多个用逗号分隔的单词的向量连接起来,但在最后一个单词之前加上“ and”?
我有一个像下面的向量
id < c("1250.3000488281_-57.882898769379_OilA")
Run Code Online (Sandbox Code Playgroud)
我需要提取_之后的数字,即-57.882898769379。
我尝试过这样的事情
library(magrittr)
id_play %>%
stringr::str_extract(.,"(?<=[[:punct:]])([0-9]+)(?=_Oil)")
Run Code Online (Sandbox Code Playgroud)
而不是得到-57。----我在“。”之后得到了所有东西。,即““ 882898769379”。
如何排除后缀不包含标点符号“。”?