【发布时间】:2016-02-29 02:55:17
【问题描述】:
我有一个这样的字符串列表(省略 58*5 例):
participant_01_Bullpup_1.xml
participant_01_Bullpup_2.xml
participant_01_Bullpup_3.xml
participant_01_Bullpup_4.xml
participant_01_Bullpup_5.xml
#...Through to...
participant_60_Bullpup_1.xml
participant_60_Bullpup_2.xml
participant_60_Bullpup_3.xml
participant_60_Bullpup_4.xml
participant_60_Bullpup_5.xml
我想在这些上使用 gsub 以便最终得到(仅示例):
01_1
60_5
目前,我的代码如下:
fileNames <- Sys.glob("part*.csv")
for (fileName in fileNames) {
sample <- read.csv(fileName, header = FALSE, sep = ",")
part <- gsub("[^0-9]+", "", substring(fileName, 5, last = 1000000L))
print(part)
}
这会产生以下字符串(示例):
011
605
但是,我不知道如何在这些字符串之间保留一个下划线。
【问题讨论】: