Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to rename a column with dplyr?

I’m trying to use dplyr’s rename function but my column names are variable and I would like to use a function to set the column name and it’s not working.

My code:

library(dplyr)

data_hoje <- as.Date("2023-01-20")

teste %>%
  rename("Há 30 dias" = V1,
         "Há 7 dias" = V2,
         paste0(format(data_hoje, "%d/%b/%y")) = V3,
         " " = Delta)

I’m getting this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error: unexpected ‘=’ in:
" "Há 7 dias" = V2,
paste0(format(data_hoje, "%d/%b/%y")) ="

My dput:

structure(list(V1 = c("2022-12-20", "5.1908", "3.6700", "3.3729", 
"3.3400", NA, "5.2205"), V2 = c("2023-01-13", "5.3720", "3.8095", 
"3.4606", "3.3997", "3.2951", "5.3589"), V3 = c("2023-01-20", 
"5.4573", "3.8953", "3.5577", "3.5023", "3.5357", "5.4372"), 
    Delta = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)), class = "data.frame", row.names = c("Data", 
"2023", "2024", "2025", "2026", "2027", "Suavizada"))

How can I make this code work?

>Solution :

Use this:

teste %>% rename("Há 30 dias" := V1,
         "Há 7 dias" := V2,
         "{paste0(format(data_hoje, '%d/%b/%y'))}" := V3,
         " " := Delta)

see vignette("programming")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading