How to convert datetime format into 'ddmmyyyy' using R?

Viewed 362

My dates dataframe looks like this:

  Date              Values
  1JAN2018           80
  23DEC2019          21.3
  ...                ...

How can I format this into a ddmmyyyy date so that I can use ggplot to create a time series plot?

What did I do?

Date <- as.Date(Date, '%d%m%Y')

But unfortunately, that didn't seem to do the trick.

Thank you so much! :D

EDIT:

Thanks for the answers. This is my current plot. Is it possible to smoothen this out more? It seems very static:

enter image description here

Both values are measured several times (HH, MM) at the same time each day (around 40 times). When using your code:

 ggplot(aug, aes(aug$DATE)) + 
 #geom_smooth(stat = "identity") +
 geom_line(aes(y = aug$VALUE_ONE, colour = "aug$VALUE_ONE")) +
 geom_line(aes(y = aug$VALUE_TWO, colour = "aug$VALUE_TWO")) +
 ggtitle("Time Series Data)")+
 xlab("Time")+ 
 ylab("Value")+
 theme(axis.text.x = element_text(angle = 60, hjust = 1), 
    plot.title = element_text(hjust = 0.5))

EDIT2:

Thanks again for the answers. To get a better view of the data, the data is as follows:

  Date        ValueOne      ValueTwo    Time
  1JAN2018     20              11       05:22
  1JAN2018     25              12       05:33
  1JAN2018     34              44       05:59
  1JAN2018     32              55       06:30
  1JAN2018      4              88       06:48
  1JAN2018     11              78       10:33
  1JAN2018     12              100      15:33

Every day has around 40 measures of both ValueOne and ValueTwo at different moments on that day. Because there are so many measurements, the line stays static to me unless I plot a single day for example. In that case it works well. Do you ave any idea?

3 Answers

A simple solution is to use lubridate package

# Install lubridate package
install.packages("lubridate")


# Use lubridate package
library(lubridate)

dmy('23DEC2019')
[1] "2019-12-23"

dmy('1JAN2018')
[1] "2018-01-01"


# Plotting the data in ggplot
library(ggplot2)

ggplot(data, aes(x=date, y=values)) + 
  geom_smooth(stat = "identity") +
  ggtitle("Time Series Data)")+
  xlab("Time")+ 
  ylab("Value")+
  theme(axis.text.x = element_text(angle = 60, hjust = 1), 
        plot.title = element_text(hjust = 0.5))

The anytime package offers functions anytime() and anydate() which do this---from any input format, and without a required format string.

R> library(anytime)                     
R> anydate(c("23DEC2019", "1JAN2018"))  
[1] "2019-12-23" "2018-01-01"             
R> 

It should be sufficient to do

as.Date(x, format = "%d%b%Y")

However, for some locales this produces NAs

x <- c("1JAN2018", "23DEC2019")
as.Date(x, format = "%d%b%Y")
# [1] "2018-01-01" NA

You see this gives NA for the entry 23DEC2019 (for me).

From ?strptime

## read in date info in format 'ddmmmyyyy'
## This will give NA(s) in some non-English locales; setting the C locale
## as in the commented lines will overcome this on most systems.
## lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")

So you might also need

lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")

Now run above code again

as.Date(x, "%d%b%Y")
#[1] "2018-01-01" "2019-12-23"

And finally change locale back again

Sys.setlocale("LC_TIME", lct)
Related