标签: stringr

提取|之间的最后一个字 |

我有以下数据集

> 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)

regex r stringr

7
推荐指数
2
解决办法
137
查看次数

R - stringr每两个间隔数字添加换行符

特定

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的方法是什么?

r stringr

7
推荐指数
1
解决办法
54
查看次数

从文本字符串中获取唯一的字符串数

我想知道如何从文本字符串中获取唯一的字符数.假设我正在寻找重复单词中的苹果,香蕉,菠萝,葡萄的重复计数.

 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'.请提出您的想法.

r stringr tm dplyr

7
推荐指数
1
解决办法
170
查看次数

在R中用括号中的文本提取

两个相关的问题.我有文本数据的向量,如

"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)

有没有简单的方法来做到这一点?另一个困难是这些可能变得非常大并且具有大(无限)数量的括号.因此,我不能简单地在括号中"预先/发布"文本并需要更智能的解决方案.

string text r vector stringr

6
推荐指数
2
解决办法
3925
查看次数

检测字符串变量中的单词列表并将匹配的单词提取到数据框中的新变量

我有一个两个变量的数据帧,其中一个是字符向量。"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 应该类似于下面的输出。 …

r stringr dplyr

6
推荐指数
1
解决办法
3188
查看次数

str_replace不会替换所有出现,但gsub呢?

我试图删除字符串中的括号,如下所示.

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似乎没有找到结束括号?有什么想法吗?

string replace r gsub stringr

6
推荐指数
1
解决办法
1343
查看次数

每隔n个字符拆分字符串新列

假设我有一个像这样的数据框,带有字符串向量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个字符.

substring r gsub stringr

6
推荐指数
1
解决办法
208
查看次数

用 mutate 替换部分字符串(在管道中)

我想替换字符串的一部分(在前 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<-或其他字符串函数来做到这一点?

r stringr dplyr tidyverse

6
推荐指数
1
解决办法
2145
查看次数

如何在R中减去两个逗号分隔的列?

我有一个我似乎无法解决的小问题。给定两列:

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 都在第一行中消失了。

r stringr data.table

6
推荐指数
1
解决办法
172
查看次数

基本 R gsub 和 stringr::str_replace_all 的不同行为?

我希望gsubstringr::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)

然而, …

regex r string-substitution stringr

6
推荐指数
1
解决办法
107
查看次数

标签 统计

r ×10

stringr ×10

dplyr ×3

gsub ×2

regex ×2

string ×2

data.table ×1

replace ×1

string-substitution ×1

substring ×1

text ×1

tidyverse ×1

tm ×1

vector ×1