日期和时间在不同的列,转换为日期时间在R [英] Dates and Times in separate columns, convert to datetime in R

查看:131
本文介绍了日期和时间在不同的列,转换为日期时间在R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从NOAA导入一些数据,它在日期和时间分列。我搜索了一种优雅的方法来在我的R数据帧中附加一个datetime列,但是无法使用。我发现一个关于逆向的堆栈交换问题,但不是这个。有没有一个简单的as.Date命令我可以运行?我只是使用read.table下载的文本文件,并导入只需查找。

I am importing some data from NOAA and it has dates and times in separate columns. I searched for an elegant way to append a single datetime column in my R dataframe but was unable. I found a stack exchange question about the inverse but not this one. Is there a simple as.Date command I could run? I am simply using read.table for a downloaded text file and it imports just find.

浮标数据在这里:
http://www.ndbc.noaa.gov/data/realtime2/51202.txt

>yr mo  dy  hr  mn  degT    m.s m.s.1   m   sec sec.1 degT.1    hPa degC    degC.1  degC.2  nmi hPa.1   ft
>2012   1   16  3   55  MM  MM  MM  1.4 10  7.2 339 MM  MM  23.9    MM  MM  MM  MM


推荐答案

您可以使用 ISOdatetime ,这只是一个简单的包装器, as.POSIXct 。确保将 sec 参数指定为零。

You can use ISOdatetime, which is just a simple wrapper to as.POSIXct. Make sure to specify the sec argument as zero.

Data$timestamp <- with(Data, ISOdatetime(YY,MM,DD,hh,mm,0))

这篇关于日期和时间在不同的列,转换为日期时间在R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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