我说有一根绳子
fruit <- "()goodapple"
Run Code Online (Sandbox Code Playgroud)
我想删除字符串中的括号.我决定使用stringr包,因为它通常可以处理这类问题.我用 :
str_replace(fruit,"()","")
Run Code Online (Sandbox Code Playgroud)
但没有任何东西被替换,以下内容被替换:
[1] "()good"
Run Code Online (Sandbox Code Playgroud)
如果我只想更换右半支架,它可以工作:
str_replace(fruit,")","")
[1] "(good"
Run Code Online (Sandbox Code Playgroud)
但是,左半支架不起作用:
str_replace(fruit,"(","")
Run Code Online (Sandbox Code Playgroud)
并显示以下错误:
Error in sub("(", "", "()good", fixed = FALSE, ignore.case = FALSE, perl = FALSE) :
invalid regular expression '(', reason 'Missing ')''
Run Code Online (Sandbox Code Playgroud)
任何人都有想法为什么会这样?如何删除字符串中的"()"呢?
两个相关的问题.我有文本数据的向量,如
"a(b)jk(p)" "ipq" "e(ijkl)"
Run Code Online (Sandbox Code Playgroud)
并希望将其轻松分离为包含括号内的文本的向量:
"ajk" "ipq" "e"
Run Code Online (Sandbox Code Playgroud)
和包含括号中的文本的向量:
"bp" "" "ijkl"
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法来做到这一点?另一个困难是这些可能变得非常大并且具有大(无限)数量的括号.因此,我不能简单地在括号中"预先/发布"文本并需要更智能的解决方案.
我有一个两个变量的数据帧,其中一个是字符向量。"MyVector" 中的每一行都包含一个只有一个名字的字符串(即“Pete”)。名称在字符串中的位置可能会有所不同。我想创建将列表中的名称与字符串中的名称匹配的代码,并将该名称提取到数据框中的新变量中。如果名称始终位于向量“MyVector”中的相同位置,我将创建一个新变量作为 MyVector 的子字符串,将名称提取到新列中。我从 Stringr 尝试了各种版本的 str_detect 无济于事。
挑战:如果名称位于多个位置,我如何检测名称或将名称提取到新变量中并将其放入 MyDF?
#Create the data frame
var.1 <-rep(c(1,5,3),2)
MyVector <- c("I know Pete", "Jerry has a new job","Victor is an employee","How to work with Pete","Too Many Students","Bob is mean")
MyDF <-as.data.frame(cbind(var.1,MyVector))
#Create a vector of a list of names I want to extract into a new column in the dataframe.
Extract <- c("Jerry","Pete", "Bob", "Victor")
#Match would be perfect if I could use it on character vectors
MyDF$newvar <-match(MyDF$MyVector,Extract)
Run Code Online (Sandbox Code Playgroud)
我的最终 data.frame 应该类似于下面的输出。 …
我试图删除字符串中的括号,如下所示.
library(stringr)
x <- "(Verhoeff,1937)"
str_replace(string = x, pattern = "(\\()|(\\))", replacement = "")
[1] "Verhoeff,1937)"
gsub(pattern = "(\\()|(\\))", replacement = "", x = x)
[1] "Verhoeff,1937"
Run Code Online (Sandbox Code Playgroud)
str_replace似乎没有找到结束括号?有什么想法吗?
假设我有一个像这样的数据框,带有字符串向量var2
var1 var2
1 abcdefghi
2 abcdefghijklmnop
3 abc
4 abcdefghijklmnopqrst
Run Code Online (Sandbox Code Playgroud)
将每n个字符的var2拆分为新列的最有效方法是什么,直到每个字符串结束为止,
例如,如果每4个字符,输出将如下所示:
var1 var2 new_var1 new_var2 new_var3 new_var4 new_var5
1 abcdefghi abcd efgh i
2 abcdefghijklmnop abcd efgh ijkl mnop
3 abc abc
4 abcdefghijklmnopqrst abcd efgh ijkl mnop qrst
Run Code Online (Sandbox Code Playgroud)
stringr包?使用"str_split_fixed"
或使用正则表达式:
gsub("(.{4})", "\\1 ", "abcdefghi")
Run Code Online (Sandbox Code Playgroud)
根据var2的长度创建转到new_var_n的新列的容量,例如可以是10000个字符.
我有以下字符串向量.它包含两个元素.每个元素由两个折叠短语组成.
strings <- c("This is a phrase with a NameThis is another phrase",
"This is a phrase with the number 2019This is another phrase")
Run Code Online (Sandbox Code Playgroud)
我想将这些短语拆分为向量中的每个元素.我一直在尝试这样的事情:
library(stringr)
str_split(strings, "\\B(?=[a-z|0-9][A-Z])")
Run Code Online (Sandbox Code Playgroud)
几乎给了我正在寻找的东西:
[[1]]
[1] "This is a phrase with a Nam" "eThis is another phrase"
[[2]]
[1] "This is a phrase with the number 201" "9This is another phrase"
Run Code Online (Sandbox Code Playgroud)
我想在模式之后进行拆分,但无法弄清楚如何做到这一点.
我想我接近一个解决方案,并希望得到任何帮助.
我正在尝试使用R的stringr包从推文中提取Twitter句柄。例如,假设我要获取向量中所有以“ A”开头的单词。我可以这样
library(stringr)
# Get all words that begin with "A"
str_extract_all(c("hAi", "hi Ahello Ame"), "(?<=\\b)A[^\\s]+")
[[1]]
character(0)
[[2]]
[1] "Ahello" "Ame"
Run Code Online (Sandbox Code Playgroud)
大。现在,让我们使用“ @”而不是“ A”尝试相同的操作
str_extract_all(c("h@i", "hi @hello @me"), "(?<=\\b)\\@[^\\s]+")
[[1]]
[1] "@i"
[[2]]
character(0)
Run Code Online (Sandbox Code Playgroud)
为什么此示例给出的结果与我预期的相反,我该如何解决?
我想替换字符串的一部分(在前 2 个下划线之间,第一组始终为“i”),如下面的基本 R 示例所示:
library(dplyr)
library(stringr)
d <- tibble(txt = c("i_0000_GES", "i_0000_OISO", "i_0000_ASE1333"),
repl = c("1111", "1111", "2222"))
str_sub(d$txt, 3, 6) <- d$repl
d
# A tibble: 3 x 2
# txt repl
# <chr> <chr>
# 1 i_1111_GES 1111
# 2 i_1111_OISO 1111
# 3 i_2222_ASE1333 2222
Run Code Online (Sandbox Code Playgroud)
我如何使用str_sub<-或其他字符串函数来做到这一点?
我有一个我似乎无法解决的小问题。给定两列:
dt <- data.table(ColumnA = c("A,B,C,A,A,A", "A,B,C"), ColumnB = c("A,C,A", "C"))
Run Code Online (Sandbox Code Playgroud)
我想从 columnA 中“减去”columnB,这将导致:
data.table(Result = c("B,A,A", "A,B"))
Run Code Online (Sandbox Code Playgroud)
如果不首先将其转换为列表,然后尝试减去该列表,如何实现这一事实?此外,由于数据集相当大,无法在 R 中使用 for 循环来完成。
逗号分隔字符串中的每一项都应视为一项,如果出现一次,则应仅减去一次。因此,并非所有 A 都在第一行中消失了。
我希望gsub并stringr::str_replace_all在下面返回相同的结果,但只gsub返回预期的结果。我正在开发一个课程来演示,str_replace_all所以我想知道为什么它会在这里返回不同的结果。
txt <- ".72 2.51\n2015** 2.45 2.30 2.00 1.44 1.20 1.54 1.84 1.56 1.94 1.47 0.86 1.01\n2016** 1.53 1.75 2.40 2.62 2.35 2.03 1.25 0.52 0.45 0.56 1.88 1.17\n2017** 0.77 0.70 0.74 1.12 0.88 0.79 0.10 0.09 0.32 0.05 0.15 0.50\n2018** 0.70 0"
gsub(".*2017|2018.*", "", txt)
stringr::str_replace_all(txt, ".*2017|2018.*", "")
Run Code Online (Sandbox Code Playgroud)
gsub返回预期的输出(之前和包括2017,之后和包括的所有内容2018都已被删除)。
gsub 的输出(预期)
[1] "** 0.77 0.70 0.74 1.12 0.88 0.79 0.10 0.09 0.32 0.05 0.15 0.50\n"
Run Code Online (Sandbox Code Playgroud)
然而, …