字符串从第n个最后一个分隔符移到结尾

时间:2017-04-06 07:58:56

标签: r string split tail separator

我有以下字符串:

data_string = c("Aa_Bbbbb_0_ID1",
                "Aa_Bbbbb_0_ID2",
                "Aa_Bbbbb_0_ID3",
                "Ccccc_D_EEE_0_ID1")

我只想拆分所有字符串以获得这些结果:

"Aa_Bbbbb"
"Aa_Bbbbb"
"Aa_Bbbbb"
"Ccccc_D_EEE"

所以基本上,我正在寻找一个函数,它接受data_string,设置一个分隔符,并采取分割位置:

remove_tail(data_table, sep = '_', del = 2)

仅将尾部从第二个最后一个分隔符移到字符串的末尾(不拆分所有字符串)

3 个答案:

答案 0 :(得分:1)

使用gsub("_0_.*","",data_string)

# split on "_" then paste back removing last 2 sapply(strsplit(data_string, "_", fixed = TRUE), function(i) paste(head(i, -2), collapse = "_"))

答案 1 :(得分:1)

尝试以下:

# custom function
remove_tail <- function(x, sep = "_", del = 2){
  sapply(strsplit(x, split = sep, fixed = TRUE),
         function(i) paste(head(i, -del), collapse = sep))
  }

remove_tail(data_string, sep = '_', del = 2)
# [1] "Aa_Bbbbb"    "Aa_Bbbbb"    "Aa_Bbbbb"    "Ccccc_D_EEE"

我们可以自己创作:

if statements

答案 2 :(得分:1)

我们还可以使用sub tp匹配_后跟一个或多个数字(\\d+)和其他字符,将其替换为空白(""

sub("_\\d+.*", "", data_string)
#[1] "Aa_Bbbbb"    "Aa_Bbbbb"    "Aa_Bbbbb"    "Ccccc_D_EEE"