如何将多个后缀添加到矢量字符串

时间:2017-07-18 13:44:36

标签: r paste

我希望将后缀“-r1i1p1_rcp45”和“-r1i1p1_rcp85”添加到字符向量字符串中,但我希望输出为每个model.list值末尾带有后缀的model.list值,每个后缀的单独值。这可能令人困惑,所以......

我的矢量字符串是:

model.list <- c("ACCESS1-0", "ACCESS1-3", "bcc-csm1-1", "bcc-csm1-1-m")

现在,我附加这些后缀的代码为:

gcm<- paste(model.list, "-r1i1p1_rcp45", "-r1i1p1_rcp85", sep = "")

但输出(如预期的那样)给出:

> gcm
[1] "ACCESS1-0-r1i1p1_rcp45-r1i1p1_rcp85"    "ACCESS1-3-r1i1p1_rcp45-
r1i1p1_rcp85"   
[3] "bcc-csm1-1-r1i1p1_rcp45-r1i1p1_rcp85"   "bcc-csm1-1-m-r1i1p1_rcp45-
r1i1p1_rcp85"

我希望输出看起来如下:

> gcm
[1] "ACCESS1-0-r1i1p1_rcp45"    "ACCESS1-0-r1i1p1_rcp85"
[3] "ACCESS1-3-r1i1p1_rcp45"    "ACCESS1-3-r1i1p1_rcp85"
[5] "bcc-csm1-1-r1i1p1_rcp45"    "bcc-csm1-1-r1i1p1_rcp85"
[7] "bcc-csm1-1-m-r1i1p1_rcp45"    "bcc-csm1-1-m-r1i1p1_rcp85"

1 个答案:

答案 0 :(得分:2)

你必须分开进行:

> model.list2=c(paste0(model.list, "-r1i1p1_rcp45"), paste0(model.list, "-r1i1p1_rcp85"))
> model.list2
[1] "ACCESS1-0-r1i1p1_rcp45"    "ACCESS1-3-r1i1p1_rcp45"    "bcc-csm1-1-r1i1p1_rcp45"   "bcc-csm1-1-m-r1i1p1_rcp45"
[5] "ACCESS1-0-r1i1p1_rcp85"    "ACCESS1-3-r1i1p1_rcp85"    "bcc-csm1-1-r1i1p1_rcp85"   "bcc-csm1-1-m-r1i1p1_rcp85"