本文介绍了从字符串向量中获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串向量

ids <- c("NM_006690.2_PROBE1","333212.1_PROBE1","7602049CB1_PROBE1","NM_018065.1_PROBE1","1539036CB1_PROBE1","NM_021019.1_PROBE1","1440608CB1_PROBE1","NM_031270.1_PROBE1","613678CB1_PROBE1")

这里已经有很多讨论了:提取子串在 R 根据模式.

A lot of discussing is already here: extract a substring in R according to a pattern.

我想删除 dot(.) 之后的所有内容,并希望删除 PROBE 之前的 _ 之后的所有内容.我设法通过

I want to remove everything after a dot(.) and want to remove all after _ before PROBE. i managed to remove . by

read.table(text = ids, sep = ".", as.is = TRUE, fill=TRUE)$V1

我现在介意在像 613678CB1_PROBE1 这样的情况下删除 PROBE 之前的 _ 并且我想要的输出是 613678CB1 .怎么做.

I now mind to remove the _ before PROBE in cases like 613678CB1_PROBE1 and the output i want is 613678CB1 . How to do it.

输出:

"NM_006690", "333212"  , "7602049CB1"  "NM_018065","1539036CB1"  "NM_021019" "1440608CB1"  "NM_031270","613678CB1")

注意:有两个_,一个用NM,另一个用PROBE.我希望删除所有内容 _PROBE

Note: There are two _'s one attached with NM and other with PROBE. I want the one every thing to be removed _PROBE

推荐答案

你好像在问:

gsub("\\..*|_PROBE.*", "", ids)

演示:

gsub("\\..*|_PROBE.*", "", ids)
# [1] "NM_006690"  "333212"     "7602049CB1" "NM_018065"  "1539036CB1"
# [6] "NM_021019"  "1440608CB1" "NM_031270"  "613678CB1"

这篇关于从字符串向量中获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 19:58