Search code examples
rexcelxlsxlubridate

How to rename dataframe columns with month year in R


I am reading xlsx which has some date columns names as

Name <- c("some data")
Date <- c("some data")
ID <- c("some data")
`comprehensive (Monthly) 2022-01` <- c( "some data")
`comprehensive (Monthly) 2022-02`  <- c( "some data")
`comprehensive (Monthly) 2022-03` <- c( "some data")
`comprehensive (Monthly) 2022-04`  <- c( "some data")
`comprehensive (Monthly) 2022-05` <- c( "some data")
`comprehensive (Monthly) 2022-06`  <- c( "some data")
`comprehensive (Monthly) 2022-07` <- c( "some data")
`comprehensive (Monthly) 2022-08`  <- c( "some data")


df <- data.frame(
Name,
Date,
ID,
`comprehensive (Monthly) 2022-01`,
`comprehensive (Monthly) 2022-02`,
`comprehensive (Monthly) 2022-03`,
`comprehensive (Monthly) 2022-04`)

I would only like to rename All the columns like comprehensive (Monthly) 2022-01 and write to new xlsx file. what is the best way to achieve the end result below ?

Can i write a function like below to strsplit ?

function(df)
{
  temp <- (df)
  temp$`comprehensive (Monthly) 2022-01`<- strsplit(df)[[1]][2]
  return(temp) |> write.xlsx(new_df)
}
new_df

Name <- c("some data")
Date <- c("some data")
ID <- c("some data")
`2022-01` <- c( "some data")
`2022-02`  <- c( "some data")
`2022-03` <- c( "some data")
`2022-04`  <- c( "some data")
`2022-05` <- c( "some data")
`2022-06`  <- c( "some data")
`2022-07` <- c( "some data")
`2022-08`  <- c( "some data")

Solution

  • Here's a couple of examples of using names<- and regex sub functions on the existing names of hte dataframe:

     names(df) <- sub("comprehensive|\\(|\\)", "", names(df))
     df
    #-----------------------
           Name      Date        ID ..Monthly..2022.01 ..Monthly..2022.02 ..Monthly..2022.03 ..Monthly..2022.04
    1 some data some data some data          some data          some data          some data          some data
     names(df) <- sub("comprehensive|\\(|\\)|\\c", "", names(df))
    #----------
     df
    #-----------
           Name      Date        ID ..Monthly..2022.01 ..Monthly..2022.02 ..Monthly..2022.03 ..Monthly..2022.04
    1 some data some data some data          some data          some data          some data          some data
    #--------------
    
    names(df) <- gsub("comprehensive|\\(|\\)|\\.", "", names(df)) # remove periods
    
    df
    #-------------
           Name      Date        ID Monthly202201 Monthly202202 Monthly202203 Monthly202204
    1 some data some data some data     some data     some data     some data     some data
    

    All of the special characters in an R regex pattern need to be double escaped.

    See the list of special characters at:

    ?regex