R – How to Rename .tif Files in a Stacked TIFF Image to DD/MM/YYYY Format

dategeotiff-tiffrrasterrename

I'm struggling to rename the TIFF files in a stacked TIFF file in date format (DD-MM-YYYY). So, far I have converted it to Date format. But, the date values are in YYYY-MM-DD format by default. How to convert it to DD-MM-YYYY format. I checked the class of the variable column. It is in character. I tried to convert it with 'lubridate' in R but still, the class of the variable column is in character. So, I renamed it manually for 10 dates. But, I have plenty of such files. So, is there any way to do it automatically?

library(raster)
library(sf)
library(terra)
library(ggplot2)
library(rgdal)
library(sp)
setwd('D:/PHD/Data/2009/DUST/')
DUST <- raster(paste0("dust.tif"))
plot(DUST)
s = stack("dust.tif")
plot(s)
ga_sf <- st_read(dsn = "C:/Users/shrav/Downloads/India_State/India_State/India_State.shp", quiet = TRUE)
cropDUST<- crop(s,ga_sf )
DUST_mask<- mask(cropDUST,ga_sf )
plot(DUST_mask)
library(lubridate)

#renaming the tif files with dates from 18-06-2009 to 2706-2009

tempo <- seq(as.Date("2009/06/18"),by ="day", length.out = 10)
 filename = paste0(tempo)
 names(DUST_mask) <- c(filename)
 names(DUST_mask)

 #convert to data frame to plot all the images

 library(dplyr)
 DUST.df<- as.data.frame(DUST_mask, xy= TRUE)%>%

  melt (id.vars= c('x','y'))

  DUST.df[3] <- lapply(DUST.df[3], gsub, pattern = "X", replacement = "", fixed = TRUE)
  DUST.df[3] <- lapply(DUST.df[3], gsub, pattern = ".", replacement = "-", fixed = TRUE)
  DUST.df %>% 
  #renaming the dates as DD-MM-YYYY format
  mutate(variable = recode(variable,'2009-06-18'='18-06-2009',
   '2009-06-19'='19-06-2009',
   '2009-06-20'='20-06-2009',
   '2009-06-21'='21-06-2009',
   '2009-06-22'='22-06-2009',
   '2009-06-23' = '23-06-2009',
   '2009-06-24' = '24-06-2009',
   '2009-06-25' = '25-06-2009',
   '2009-06-26' = '26-06-2009',
   '2009-06-27' = '27-06-2009'
   ))
   p<-ggplot ()+ geom_raster(data= DUST.df, aes(x=x, y=y, fill= value))+
   geom_sf(data=ga_sf, color = "black", fill = NA, size = 0.5)+
   facet_wrap(~variable,nrow = 2, ncol = 5)+
   theme(strip.background =element_rect(fill="white"))+
   theme(strip.text = element_text(colour = 'black',face='bold'))+
   theme(panel.background = element_rect(fill = "white",
                                    colour = "white",
                                    size = 0.5, linetype = "solid"),
    panel.grid.major = element_line(size = 0.5, linetype = 'solid',colour = "grey80"))+
   theme(panel.border = element_rect(color = "black",fill = NA,size = 1))+
   labs(x = "Longitude", 
    y = "Latitude")+ 
     scale_fill_stepsn(name='COLUMNAR DUST (kg/m2)', colours = 
    heat.colors(12),na.value="NA",n.breaks = 9,limits = c(0.5e-04,7e-04))+
    theme(axis.title.x = element_text(face="bold",color="black"), axis.title.y = 
     element_text(face="bold",color="black"),legend.text = element_text(face = "bold"),
    legend.position = "bottom",
    #--- NEW LINES HERE!! ---#
    legend.key.height = unit(0.5, "cm"),
    legend.key.width = unit(2, "cm"))+
    guides(legend,fill = guide_colorbar(title.position = "top"))
     p 

Best Answer

Do you simply need the format function with a format string? This lets you flexibly convert a data object to any character format. Example:

Make a vector of dates:

> d = as.Date(c("2020-01-12","2020-01-31"))

Year-month-date format:

> format(d,"%Y-%m-%d")
[1] "2020-01-12" "2020-01-31"

"American" date format:

> format(d,"%b %d, %Y")
[1] "Jan 12, 2020" "Jan 31, 2020"

Two-digit year, not Y2K compliant format:

> format(d,"%y-%b-%d")
[1] "20-Jan-12" "20-Jan-31"

This isn't a GIS question really, so any further questions should go elsewhere.