0
$\begingroup$

I'm trying to use the recent COVID-19 data from the site of Italian Civil Protection, but they use a rather complicated time format that I'm finding troublesome as a novice to plot as data in a graph. This is how the data is presented:

[1] 2020-02-24T18:00:00 2020-02-25T18:00:00 2020-02-26T18:00:00 2020-02-27T18:00:00 2020-02-28T18:00:00 2020-02-29T18:00:00

and I would like to use the format as DD-MM, without the time and the year. How can I do it?

| improve this question | | | | |
$\endgroup$
0
$\begingroup$

You can simply use as.Date, see below.

dates.text <- c("2020-02-24T18:00:00", "2020-02-25T18:00:00", "2020-02-26T18:00:00",
                "2020-02-27T18:00:00", "2020-02-28T18:00:00", "2020-02-29T18:00:00")
dates.text
# [1] "2020-02-24T18:00:00" "2020-02-25T18:00:00" "2020-02-26T18:00:00" "2020-02-27T18:00:00"
# [5] "2020-02-28T18:00:00" "2020-02-29T18:00:00"

dates.dates <- as.Date(dates.text)
dates.dates
# [1] "2020-02-24" "2020-02-25" "2020-02-26" "2020-02-27" "2020-02-28" "2020-02-29"
| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ Thanks a lot for the help! Have a nice day and stay safe. $\endgroup$ – Leonardo Becarelli Mar 30 at 9:21
0
$\begingroup$

First you need to split the string :

x = "2020-02-24T18:00:00 2020-02-25T18:00:00 2020-02-26T18:00:00"

x = strsplit(x, "\\s+")[[1]]

[1] "2020-02-24T18:00:00" "2020-02-25T18:00:00" "2020-02-26T18:00:00"

Then you can just the result as dates :

x = as.Date(x)
[1] "2020-02-24" "2020-02-25" "2020-02-26"
| improve this answer | | | | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.