Rename columns when they don't exist

I'm reading and processing on a cron job a daily spreadsheet from the state of Texas tracking COVID vaccination data. I'm having issues because some columns tend to come and go, which rename does not like. For example, I have this statement:

  readxl::read_excel(Vaccine_path, sheet=2) %>% 
  rename(County="County Name", 
         Region="Public Health Region (PHR)", 
         Doses_Alloc="Total Doses Allocated", 
         Doses_Admin="Vaccine Doses Administered", 
         People_one_dose="People Vaccinated with at least One Dose", 
         People_fully="People Fully Vaccinated")

which works fine except when for a few random days the Allocated Doses column disappeared.

Suggestions on the cleanest way to deal with this inconsistent behavior in an automatic job?


I would use R's standard control flow syntax if(){} to test if a name is present, before conditionally renaming it.

Here is what I ended up with.

namekey <- c("County Name"="County", 
             "Public Health Region (PHR)"="Region", 
             "Total Doses Allocated"="Doses_alloc", 
             "Vaccine Doses Administered"="Doses_admin", 
             "People Vaccinated with at least One Dose"="People_one_dose", 
             "People Fully Vaccinated"="People_fully", 
             "Population, 16+"="Pop_adult",
             "Population, 65+"="Pop_old", 
             "Population, Phase 1A Healthcare Workers"="Pop_healthcare",
             "Population, Phase 1A Long-term Care Residents"="Pop_longterm",
             "Population, Phase 1B Any Medical Condition"="Pop_1B_medical")

foo <- readxl::read_excel(Vaccine_path, sheet=2, na=c("", "--"))  

names(foo) <- namekey[names(foo)]

Thank you. Very helpful.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.