I have the following table in R
id<-c(1,2,3,4)
medal<-c("2021-2020-2018","NA","2019","2015-2014-2012")
df<-data.frame(id,medal)
id medal
1 2021-2020-2018
2 NA
3 2019
4 2015-2014-2012
and would like to break the medal column to multiple dummy variables for each id as follows:
id medal 2021 2020 2019 2018 2015 2014 2012
1 2021-2020-2018 1 1 0 1 0 0 0
2 NA 0 0 0 0 0 0 0
3 2019 0 0 1 0 0 0 0
4 2015-2014-2012 0 0 0 0 1 1 1
I would appreciate your help with this.