我建议这种tidyverse 方法将字符串分成列,格式化所需的值并再次连接。代码如下:
library(tidyverse)
#Code
df %>%
#Separate by period
separate(V1,into = c(paste0('V',1:5)),sep = '\\.') %>%
#Remove text
mutate(V2=gsub('A|001','',V2)) %>%
#Format year
mutate(V2=paste0('A',as.numeric(V2)-1,'001')) %>%
rowwise() %>%
#Collapse all
mutate(V=paste(V1,V2,V3,V4,V5,sep = '.')) %>% select(V)
输出:
# A tibble: 17 x 1
# Rowwise:
V
<chr>
1 MYD11C3.A2002001.006.2015182092934_LST_Day_CMG_subregion.tif
2 MYD11C3.A2003001.006.2015213013933_LST_Day_CMG_subregion.tif
3 MYD11C3.A2004001.006.2015243211529_LST_Day_CMG_subregion.tif
4 MYD11C3.A2005001.006.2015274114332_LST_Day_CMG_subregion.tif
5 MYD11C3.A2006001.006.2015309201228_LST_Day_CMG_subregion.tif
6 MYD11C3.A2007001.006.2015338170025_LST_Day_CMG_subregion.tif
7 MYD11C3.A2008001.006.2016001145426_LST_Day_CMG_subregion.tif
8 MYD11C3.A2009001.006.2016035025512_LST_Day_CMG_subregion.tif
9 MYD11C3.A2010001.006.2016053231728_LST_Day_CMG_subregion.tif
10 MYD11C3.A2011001.006.2016106151313_LST_Day_CMG_subregion.tif
11 MYD11C3.A2012001.006.2016189231222_LST_Day_CMG_subregion.tif
12 MYD11C3.A2013001.006.2016198015925_LST_Day_CMG_subregion.tif
13 MYD11C3.A2014001.006.2016223172712_LST_Day_CMG_subregion.tif
14 MYD11C3.A2015001.006.2016242200237_LST_Day_CMG_subregion.tif
15 MYD11C3.A2016001.006.2017032230414_LST_Day_CMG_subregion.tif
16 MYD11C3.A2017001.006.2018032175447_LST_Day_CMG_subregion.tif
17 MYD11C3.A2018001.006.2019035162351_LST_Day_CMG_subregion.tif
使用的一些数据:
#Data
df <- structure(list(V1 = c("MYD11C3.A2003001.006.2015182092934_LST_Day_CMG_subregion.tif",
"MYD11C3.A2004001.006.2015213013933_LST_Day_CMG_subregion.tif",
"MYD11C3.A2005001.006.2015243211529_LST_Day_CMG_subregion.tif",
"MYD11C3.A2006001.006.2015274114332_LST_Day_CMG_subregion.tif",
"MYD11C3.A2007001.006.2015309201228_LST_Day_CMG_subregion.tif",
"MYD11C3.A2008001.006.2015338170025_LST_Day_CMG_subregion.tif",
"MYD11C3.A2009001.006.2016001145426_LST_Day_CMG_subregion.tif",
"MYD11C3.A2010001.006.2016035025512_LST_Day_CMG_subregion.tif",
"MYD11C3.A2011001.006.2016053231728_LST_Day_CMG_subregion.tif",
"MYD11C3.A2012001.006.2016106151313_LST_Day_CMG_subregion.tif",
"MYD11C3.A2013001.006.2016189231222_LST_Day_CMG_subregion.tif",
"MYD11C3.A2014001.006.2016198015925_LST_Day_CMG_subregion.tif",
"MYD11C3.A2015001.006.2016223172712_LST_Day_CMG_subregion.tif",
"MYD11C3.A2016001.006.2016242200237_LST_Day_CMG_subregion.tif",
"MYD11C3.A2017001.006.2017032230414_LST_Day_CMG_subregion.tif",
"MYD11C3.A2018001.006.2018032175447_LST_Day_CMG_subregion.tif",
"MYD11C3.A2019001.006.2019035162351_LST_Day_CMG_subregion.tif"
)), class = "data.frame", row.names = c(NA, -17L))