假设一个向量:
xx.1 <- c("zz_ZZ_uu_d", "II_OO_d")
Run Code Online (Sandbox Code Playgroud)
我想从最右边分割出一个新的矢量,只分裂一次.预期结果将是:
c("zz_ZZ_uu", "d", "II_OO", "d").
Run Code Online (Sandbox Code Playgroud)
这就像python的rsplit()功能.我现在的想法是扭转字符串,并与分裂str_split()在stringr.
更好的解决方案?
我有一些字符串
string <- "abbccc"
Run Code Online (Sandbox Code Playgroud)
我想把同一个字母的链子替换成一个字母和这封信的出现次数.所以我想要这样的东西:
"ab2c3"
我使用stringi包来做到这一点,但它并不像我想要的那样工作.假设我已经有部件的矢量用于替换:
vector <- c("b2", "c3")
stri_replace_all_regex(string, "([a-z])\\1{1,8}", vector)
Run Code Online (Sandbox Code Playgroud)
输出:
[1] "ab2b2" "ac3c3"
Run Code Online (Sandbox Code Playgroud)
我想要的输出: [1] "ab2c3"
我也试过这种方式
stri_replace_all_regex(string, "([a-z])\\1{1,8}", vector, vectorize_all=FALSE)
Run Code Online (Sandbox Code Playgroud)
但我得到错误
Error in stri_replace_all_regex(string, "([a-z])\\1{1,8}", vector, vectorize_all = FALSE) :
vector length not consistent with other arguments
Run Code Online (Sandbox Code Playgroud) 我想将一个字符串分成两组.字符串的结构非常简单,但我无法使其工作.
txt <- "text12-01-2016"
Run Code Online (Sandbox Code Playgroud)
它总是一些字母,后跟一个日期和日期,显然是以数字开头.我在https://regex101.com/尝试了以下正则表达式,并有效地将字符串正确分开:
([a-zA-Z]*)([0-9].*)
1. "text"
2. "12-01-2016"
Run Code Online (Sandbox Code Playgroud)
但是当我在R中尝试它失败时:
strsplit(a[1],split = "([a-zA-Z]*)([0-9]*)")
[[1]]
[1] "" " " "" "." " " "" " " "" "-" "" "-" ""
Run Code Online (Sandbox Code Playgroud)
如果我引入双方括号,那么它"吃掉"第一组的最后一个字符,第二组的第一个字符:
strsplit(txt,split = "([[a-zA-Z]]*)([[0-9]]*)")
[[1]]
[1] "tex" "2-01-2016"
Run Code Online (Sandbox Code Playgroud)
如果我使用它并不重要perl=TRUE.如果我使用结果也是一致的stringi::stri_split,所以这是我的正则表达式中的一个问题.
在这种情况下使用的正确正则表达式是什么?
所有这些看似非常相似的功能之间有什么区别?
我想使用 R 函数在给定路径()newPack的父文件夹()中创建一个新文件夹()。pathPospath
path <- "/m/home/user/unix/R/3.5/stringi"
newPack <- "stringr"
pathPos <- stringi::stri_locate_last_fixed(path, '/')[-1]
pathNew <- paste(stringi::stri_sub(path, 1, pathPos), newPack, sep = '')
dir.create(pathNew)
Run Code Online (Sandbox Code Playgroud)
我可以使用上面的代码实现这一点,但我强烈认为有更好的选择。如果你知道任何,请告诉我。
给出下表
df <- structure(list(V1 = c("Prodigal_2|LOCUS_00010", "Prodigal_2|LOCUS_00010",
"Prodigal_2|LOCUS_00010", "Prodigal_2|LOCUS_00010", "Prodigal_2|LOCUS_00010",
"Prodigal_2|LOCUS_00010"), V2 = c("WP_001212884.1", "WP_042596810.1",
"WP_131250681.1", "WP_001212880.1", "WP_016079538.1", "WP_086396124.1"
), V3 = c(100, 99.7, 99.7, 99.7, 99.7, 99.7), V4 = c(381L, 381L,
381L, 381L, 381L, 381L), V5 = c(0L, 1L, 1L, 1L, 1L, 1L), V6 = c(0L,
0L, 0L, 0L, 0L, 0L), V7 = c(1L, 1L, 1L, 1L, 1L, 1L), V8 = c(381L,
381L, 381L, 381L, 381L, 381L), V9 = c(1L, 1L, 1L, 1L, 1L, 1L),
V10 = c(381L, 381L, …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”。
如何排除后缀不包含标点符号“。”?
在 Windows 10 上,RStudio。我尝试过使用命令 install.packages,但它总是出现以下消息:
\n> install.packages ("stringi")\n There is a binary version available but the source version is later:\n binary source needs_compilation\nstringi 1.4.6 1.5.3 TRUE\n\ninstalling the source package \xe2\x80\x98stringi\xe2\x80\x99\n\ntrying URL \'https://cran.rstudio.com/src/contrib/stringi_1.5.3.tar.gz\'\nContent type \'application/x-gzip\' length 7293930 bytes (7.0 MB)\ndownloaded 7.0 MB\n\n* installing *source* package \'stringi\' ...\n** package \'stringi\' successfully unpacked and MD5 sums checked\n** using staged installation\n** libs\nC:/Users/Natusha/Google Drive (natusha.alirr@gmail.com)/R/Rtools/mingw_64/bin/g++ -std=gnu++11 -I"C:/Users/Natusha/GOOGLE~2.COM/R/R-36~1.1/include" -DNDEBUG -I. -Iicu61/ -Iicu61/unicode -Iicu61/common -Iicu61/i18n -DU_STATIC_IMPLEMENTATION -DU_COMMON_IMPLEMENTATION -DU_I18N_IMPLEMENTATION -DUCONFIG_USE_LOCAL -DU_TOOLUTIL_IMPLEMENTATION -DNDEBUG -DWINVER=0x0600 -D_WIN32_WINNT=0x0600 -DU_USE_STRTOD_L=0 -O2 -Wall …Run Code Online (Sandbox Code Playgroud) 我想在字符串中的三个字符后添加空格。我使用了以下代码,效果很好。我想知道是否还有其他简单的方法来完成相同的任务
library(stringi)
Test <- "3061660217"
paste(
stri_sub(str = Test, from = 1, to = 3)
, stri_sub(str = Test, from = 4)
, sep = " "
)
[1] "306 1660217"
Run Code Online (Sandbox Code Playgroud) r ×10
stringi ×10
stringr ×5
regex ×3
string ×2
data.table ×1
directory ×1
installation ×1
split ×1
string-split ×1
tidyverse ×1