R删除分隔符前的字符串

R remove string before delimiter

我有数据框中的一列,我想删除第 5 个分隔符“.”之前的部分字符串。和最后一个“。”对于 .txt,我不知道该怎么做。

输入:

jhu-usc.edu_GBM.HumanMethylation450.6.lvl-3.TCGA-06-5415-01A-01D-1481-05.txt
jhu-usc.edu_BCD.HumanMethylation450.6.lvl-3.TCGA-06-5415-01A-01D-1482-05.txt
jhu-usc.edu_LGG.HumanMethylation450.6.lvl-3.TCGA-06-5415-01A-01D-1483-05.txt
jhu-usc.edu_LUAD.HumanMethylation450.6.lvl-3.TCGA-06-5415-01A-01D-1484-05.txt
jhu-usc.edu_LUAD.HumanMethylation450.6.lvl-3.TCGA-06-5415-01A-01D-1485-05.txt
jhu-usc.edu_BRCA.HumanMethylation450.6.lvl-3.TCGA-06-5415-01A-01D-1486-05.txt
jhu-usc.edu_GBM.HumanMethylation450.6.lvl-3.TCGA-06-5415-01A-01D-1487-05.txt
jhu-usc.edu_PRCA.HumanMethylation450.6.lvl-3.TCGA-06-5415-01A-01D-1488-05.txt

期望的输出:

TCGA-06-5415-01A-01D-1481-05
TCGA-06-5415-01A-01D-1482-05
TCGA-06-5415-01A-01D-1483-05
TCGA-06-5415-01A-01D-1484-05
TCGA-06-5415-01A-01D-1485-05
TCGA-06-5415-01A-01D-1486-05
TCGA-06-5415-01A-01D-1487-05
TCGA-06-5415-01A-01D-1488-05

我试过: sapply(strsplit(as.character(df$V1), "."), '[', 1:5)

请指教。谢谢。

假设文本是固定的

sub(".*(TCGA[^.]+).*", "\1", str1)

如果它们都以 .txt 结尾,那么您可以

sub(".+\.([^.]+).txt", "\1", as.character(df$V1))