Converting data format

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?

Topic rstudio data-formats dataset r

Category Data Science


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"

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"

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.