将日期和时间合并到“日期"列中以进行绘图 [英] Combining date and time into a Date column for plotting

查看:203
本文介绍了将日期和时间合并到“日期"列中以进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个线图.我的数据框中有3列:

I want to create a line plot. I have 3 columns in my data frame:

date        time   numbers
01-02-2010  14:57  5
01-02-2010  23:23  7
02-02-2010  05:05  3
02-02-2010  10:23  11

如何合并前两列并根据日期和时间进行绘图? Date是Date类,时间只是一个char变量.

How can I combine the first two columns and make a plot based on date and time ? Date is Date class, time is just a char variable.

推荐答案

重建数据:

dat <- read.table(text="
date        time   numbers
01-02-2010  14:57  5
01-02-2010  23:23  7
02-02-2010  05:05  3
02-02-2010  10:23  11", header=TRUE)

现在使用as.POSIXct()paste()将日期和时间组合为POSIX日期.您需要使用?strptime中定义的符号来指定格式.另请参阅?DateTimeClasses了解更多信息

Now use as.POSIXct() and paste() to combine your date and time into a POSIX date. You need to specify the format, using the symbols defined in ?strptime. Also see ?DateTimeClasses for more information

dat$newdate <- with(dat, as.POSIXct(paste(date, time), format="%m-%d-%Y %H:%M"))
plot(numbers ~ newdate, data=dat, type="b", col="blue")

这篇关于将日期和时间合并到“日期"列中以进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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