这些表达是什么意思?我在哪里可以了解它们的用法?
\\d
\\D
\\s
\\S
\\w
\\W
\\t
\\n
^
$
\
| etc..
Run Code Online (Sandbox Code Playgroud)
我需要使用这个stringr包,我完全不知道如何使用这些。
我正在尝试删除包含特定字符模式的字符串。我的数据看起来像这样:
places <- c("copenhagen", "copenhagens", "Berlin", "Hamburg")
Run Code Online (Sandbox Code Playgroud)
我想删除所有包含“哥本哈根”的元素,即"copenhagen"和"copenhagens"。但我只能想出以下代码:
library(stringr)
replacement.vector <- c("copenhagen", "copenhagens")
for(i in 1:length(replacement.vector)){
places = lapply(places, FUN=function(x)
gsub(paste0("\\b",replacement.vector[i],"\\b"), "", x))
我正在寻找一个函数,该函数使我能够删除包含“哥本哈根”的所有元素,而无需指定该元素是否还包含其他字母。
最佳剂量
我有看起来像这样的数据:
df <- tribble(
~name, ~value,
"Jake Lake MLP", 10,
"Bay May CE", 5,
"Drake Cake Jr. DSF", 9.1,
"Sam Ram IR QQQZ", 1
)
Run Code Online (Sandbox Code Playgroud)
我想修剪所有的名字,使它们是:
"Jake Lake",
"Bay May",
"Drake Cake Jr.",
"Sam Ram IR"
Run Code Online (Sandbox Code Playgroud)
基本上删除最后一个空格之后的所有内容。
我试过:
df %>% mutate(name = str_replace(name, "\\s.*$", ""))
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的!
我在这里搜索了很多正则表达式的答案,但找不到解决这类问题的方法.
我的数据集是维基百科链接:
library(tidytext)
library(stringr)
text.raw <- "Berthold Speer was een [[Duitsland (hoofdbetekenis)|Duits]] [[architect]]."
Run Code Online (Sandbox Code Playgroud)
我正在尝试从链接中清理我的文本.这个:
str_extract_all(text.raw, "[a-zA-Z\\s]+(?=\\])")
# [1] "Duits" "architect"
Run Code Online (Sandbox Code Playgroud)
从括号中选择我需要的单词.
这个:
str_replace_all(text.raw, "\\[\\[.*?\\]\\]", str_extract(text.raw, "[a-zA-Z\\s]+(?=\\])"))
# [1] "Berthold Speer was een Duits Duits."
Run Code Online (Sandbox Code Playgroud)
按预期工作,但不是我需要的.这个:
str_replace_all(text.raw, "\\[\\[.*?\\]\\]", str_extract_all(text.raw, "[a-zA-Z\\s]+(?=\\])"))
# Error: `replacement` must be a character vector
Run Code Online (Sandbox Code Playgroud)
给出了我预期的错误 "Berthold Speer was een Duits architect"
目前我的代码看起来像这样:
text.clean <- data_frame(text = text.raw) %>%
mutate(text = str_replace_all(text, "\\[\\[.*?\\]\\]", str_extract_all(text, "[a-zA-Z\\s]+(?=\\])")))
Run Code Online (Sandbox Code Playgroud)
我希望有人知道解决方案,或者如果存在问题,我可以指出一个重复的问题.我想要的输出是"Berthold Speer was een Duits architect".
我有一个data_frame看起来像这样的东西.
df <- data_frame(name = c('john','bill','amy'),
name.2 = c('johhn','ball','ammy'))
df
# A tibble: 3 x 2
name name.2
<chr> <chr>
1 john johhn
2 bill ball
3 amy ammy
Run Code Online (Sandbox Code Playgroud)
我想添加一个列,显示两个名称(.2)列之间的差异.像这样:
df %>%
mutate(diff = c('h','a','m'))
# A tibble: 3 x 3
name name.2 diff
<chr> <chr> <chr>
1 john johhn h
2 bill ball a
3 amy ammy m
Run Code Online (Sandbox Code Playgroud)
我宁愿发现使用的元素的溶液tidyverse和stringr可能的话,但我还是要像我得到它.
我在尝试编辑dplyr管道中的某些字符串的这段代码时遇到麻烦。这是一些数据,它引发以下错误。有任何想法吗?
data_frame(id = 1:5,
name = c('this and it pretty long is a',
'name is a',
'so and so and so and so and so',
'this is a',
'this is a variabel name'))
%>%
str_trunc(.,
string = .$name,
width = 10,
side='right',
ellipsis = '')
Run Code Online (Sandbox Code Playgroud)
给我这个错误:Error in str_trunc(., string = .$name, width = 10, side = "right", ellipsis = ". . . ") : unused argument (.)。
谢谢。
我有一个数据库,整个文本中都有一些用引号引起来的引号。我想删除所有的点“。” 在文本中用引号引起来。
我有将标点符号放在文本中的代码,但是如果有多个引号或多个点,则仅删除第一个。
# Simple phrase:
string <- '"é preciso olhar para o futuro. vou atuar" no front '
# Code that works for a simple 1-point sentence:
str_replace_all(string, '(\".*)\\.(.*\")','\\1\\2')
# Sentence with more than one point and more than one quote:
string <- '"é preciso olhar para o futuro. vou atuar" no front em que posso
fazer alguma coisa "para .frente", disse jose.'
# it doesn't work as i would like
str_replace_all(string, '(\".*)\\.(.*\")','\\1\\2')
Run Code Online (Sandbox Code Playgroud)
我希望删除引号中的所有要点,但是从示例中可以看出,我开发的正则表达式不适用于更一般的情况。
Suppose I have two rather long (>100k character) strings which are mostly identical but differ in some locations.
Git has the concept of a 'diff', which shows only the differences between two (text) files.
Is there anything similar in R, where I can provide two strings and have it return a very 'human readable' excerpt showing only the differences for easily and clear visual inspection?
Preferably a simple function call that accepts two (similar) strings as arguments, looks …
我想将字符串中的每个字母或符号分开,以组成一个data.frame尺寸等于字母数的新字母。我想使用的功能separate,从tidyr包装,但结果不理想。
df <- data.frame(x = c('house', 'mouse'), y = c('count', 'apple'), stringsAsFactors = F)
Run Code Online (Sandbox Code Playgroud)
df[1, ] %>% separate(x, c('A1', 'A2', 'A3', 'A4', 'A5'), sep ='')
A1 A2 A3 A4 A5 y
1 <NA> <NA> <NA> <NA> <NA> count
Run Code Online (Sandbox Code Playgroud)
A1 A2 A3 A4 A5
h o u s e
m o u s e
Run Code Online (Sandbox Code Playgroud)
stringr欢迎使用解决方案。
我正在尝试创建一个新列,它为我获取数据框中列表的最大值。我想知道如何从 df$value 列创建名为 maxvalue 的列,即,我想在列中获取该列表的最大值。
x <- c( "000010011100011111001111111100", "011110", "0000000")
y<- c(1, 2,3)
df<- data.frame(x,y)
library(stringr)
df$value <- strsplit(df$x, "[^1]+", perl=TRUE)
# expected output ( I have tried the following)
df$maxvalue<- max(df$value)
df$maxvalue
8
4
0
Run Code Online (Sandbox Code Playgroud)