R删除分隔符前的字符串

问题描述:

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

输入:

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