Reputation: 1047
I imported a database from excel to r. I have a variable says "Date of birth" with excel format dd/mm/yyyy and I want to pass to format dd-mm-yyyy to can to work with class type Date.
> c
Athlete Gender Date.of.birth Age Country
1 SABRINA MOCKENHAUPT women 06/12/1980 33 Germany
2 IRINA MIKITENKO women 23/08/1972 41 Germany
3 MARILSON DOS SANTOS man 08/10/1977 36 Brazil
4 RYAN HALL man 14/10/1982 31 United States
5 TIKI GELANA women 22/10/1987 26 Ethiopia
I used this class change, c$Date.of.birth<-as.Date(c$Date.of.birth)
but it doesn't change correctly dd / mm / yyyy to dd-mm-yyyy
Tank you for your help!
Upvotes: 0
Views: 156
Reputation: 116
You need to tell as.Date how the original date is formatted, and then format the date again to dd-mm-yyyy.
format(as.Date("06/12/1980", format = "%d/%m/%Y"), format = "%d-%m-%Y")
Upvotes: 1
Reputation: 121568
To coerce your data to Date
type:
as.Date(dat$Date.of.birth,format="%d/%m/%Y")
Or better using lubridate
package:
dmy(dat$Date.of.birth)
Upvotes: 0