2017-10-10 104 views
0

我有一個在數據框中的列,我想在第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)

請指教。謝謝。

回答

0

假設文本固定

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

如果他們與.txt全部結束,那麼你可以做

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