如何从向量中删除某些项目? [英] How to remove certain items from a vector?
本文介绍了如何从向量中删除某些项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例载体(基因转录本 ID):
Example vector (gene transcript ids):
a <- c('MSTRG.7176.1', 'MSTRG.7176.2', 'AT2G26340.2', 'AT2G26355.1')
这是一个长向量的子集,我如何删除以'MS'开头的项目,然后剪掉左边项目的最后2位数字?
This is subset of a long vector, how can I remove item begin with 'MS', then cut off the end 2 digit of left items?
推荐答案
如果我们想像@sindri_baldur 提到的那样完全避免使用正则表达式,我们可以使用
If we want to avoid regex completely as @sindri_baldur mentions we can use
string <- a[!startsWith(a, "MS")]
substr(string, 1, nchar(string) - 2)
<小时>
或者使用 grep
和 substr
string <- grep('^MS',a, invert = TRUE, value = TRUE)
substr(string, 1, nchar(string) - 2)
#[1] "AT2G26340" "AT2G26355"
<小时>
因为我们有很多新答案添加了基准,包括所有长度为 400k 的向量.
Since we have quite a few new answers adding benchmark including all of them with a vector of length 400k.
a <- c('MSTRG.7176.1', 'MSTRG.7176.2', 'AT2G26340.2', 'AT2G26355.1')
a <- rep(a, 100000)
library(microbenchmark)
microbenchmark(
ronak1 = {string <- a[!startsWith(a, "MS")];substr(string, 1, nchar(string) - 2)},
ronak2 = {string <- grep('^MS',a, invert = TRUE, value = TRUE);substr(string, 1, nchar(string) - 2)},
sotos = {word(a[!str_detect(a, '^MS')], 1, sep = fixed('.'))},
thothal = {b1 <- a[!grepl("^MS", a)];gsub("\\.[0-9]$", "", b1)},
zx8754 = tools::file_path_sans_ext(a[ !grepl("^MS", a) ]),
tmfmnk = dirname(chartr(".", "/", a[!grepl("^MS", a)])),
NelSonGon = {b<-stringi::stri_replace_all(stringi::stri_sub(a,1,-3),regex="^M.*","");b[grepl('\\w+',b)]}
)
#Unit: milliseconds
# expr min lq mean median uq max neval
# ronak1 34.75928 38.58217 45.63393 40.32845 44.24355 225.2581 100
# ronak2 94.10687 96.72758 110.83819 99.26914 105.98822 938.2969 100
# sotos 1926.21112 2500.27209 2852.43240 2861.61699 3173.10420 4478.7890 100
# thothal 155.95328 160.62800 169.02275 164.46494 169.32770 218.5033 100
# zx8754 172.96970 179.03618 186.12374 183.96887 188.06251 234.1895 100
# tmfmnk 189.29085 195.14593 208.89245 199.47172 204.40604 547.7497 100
# NelSonGon 186.54426 198.29856 226.19221 206.54542 217.92970 948.2535 100
这篇关于如何从向量中删除某些项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文