Home > database >  How can I convert character to date in r?
How can I convert character to date in r?

Time:05-20

the code the current result

As I have shown in picture 1 and 2, I want to convert my character data to date, but it return na, the problem is probably caused by %d in format. How can I solve the problem?

CodePudding user response:

Have a look at the lubridate package. There you have different functions doing what you intend. If your date format is for instance "YYYY-MM-DD" just use ymd().

CodePudding user response:

As @almost_thor said right, you can use the lubridate package. In your case you can use the dmy function like this:

df <- data.frame(LOS = c("18-Jun-11", "8-Mar-11", "25-Feb-11"))
library(lubridate)
dmy(df$LOS)

Output:

[1] "2011-06-18" "2011-03-08" "2011-02-25"
  •  Tags:  
  • r
  • Related