如何使用R将日期时间格式转换为“ ddmmyyyy”? [英] How to convert datetime format into 'ddmmyyyy' using R?

查看:145
本文介绍了如何使用R将日期时间格式转换为“ ddmmyyyy”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期数据框看起来像这样:

 日期值
2018年1月1日80
2019年12月23日21.3
... ...

如何将其格式化为 ddmmyyyyy 日期,以便可以使用 ggplot 创建时间序列图? p>

我做了什么?

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

但不幸的是,这似乎并没有解决问题。



非常感谢! :D



编辑:



感谢您的回答。这是我目前的情节。是否有可能进一步解决这个问题?似乎非常静态:





两个值都是每天在同一时间(大约40次)测量几次(HH,MM)。使用代码时:

  ggplot(aug,aes(aug $ DATE))+ 
#geom_smooth(stat = identity)+
geom_line(aes(y = aug $ VALUE_ONE,color = aug $ VALUE_ONE))+
geom_line(aes(y = aug $ VALUE_TWO,color = aug $ VALUE_TWO ))+
ggtitle(时间序列数据))+
xlab(时间)+
ylab( Value)+
主题(axis.text.x = element_text(角度= 6​​0,高度= 1),
图.title = element_text(高度= 0.5))

EDIT2:



再次感谢您的回答。为了更好地查看数据,数据如下:

 日期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

每天都有大约40种度量ValueOne和ValueTwo在当天的不同时刻。因为有太多的测量值,所以该行对我而言保持静止,例如,除非我画了一天。在那种情况下,它运作良好。您有任何想法吗?

解决方案

一个简单的解决方案是使用 lubridate package

 #安装lubridate软件包
install.packages( lubridate)


#使用lubridate软件包
库(lubridate)

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

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


#在ggplot
库中绘制数据(ggplot2)

ggplot(data,aes(x = date,y = values))+
geom_smooth(stat = identity)+
ggtitle( Time Series Data))+
xlab(时间)+
ylab(值)+
主题(axis.text.x = element_text(角度= 6​​0,正好= 1),
图。 title = element_text(hjust = 0.5))


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:

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?

解决方案

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))

这篇关于如何使用R将日期时间格式转换为“ ddmmyyyy”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆