How to change multiple date formats

lubridate
date

#1

Hello,

I have some data where date is in multiple formats like %d/%m/%Y ,%Y/%d/%m , %m/%d/%Y and I want to change all of these into a single format.Here is the code:

date_bad <- data.frame(date = c('11/02/1982','1987/10/13'))

formats <- c("%m/%d/%Y","%Y/%m/%d")
multidate <- function(data, formats){
  a<-list()
  for(i in 1:length(formats)){
    
    a[[i]]<- as.Date(data,format=formats[i])
    a[[1]][is.na(a[[i]])]<-a[[i]][!is.na(a[[i]])]
    
  }
  a[[1]]
}

date_bad$date <- multidate(date$date,c("%m/%d/%Y","%Y/%m/%d"))
date_bad$date

date_bad$date
1 “11-02-19” “1987-10-13”

Where am I going wrong in the code??Can somebody please help me here.
I went through this link in stackoverflow but it doesn’t work in my case.Also using lubridate will not work when there are more than 2 formats.