将三列转换为unix时间R [英] converting three columns to unix time R

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

问题描述

我有一个数据集,其中的日期分为三列:年,月,日。我想将其转换为标准的R日期格式(即,1970年1月1日以来的天数)。现在我有这个:

I have a dataset, where the dates are given in three columns: year, month, day. I want to convert this to the standard R date format (i.e. days since 1-1-1970). Right now I have this:

date <- as.Date(paste(year,month,day, sep="-"))

但是我想知道这是否是正确的方法/是否有更有效的方法。

However I am wondering if this is the correct way / if there is a more efficient way.

推荐答案

as.Date 函数将日期表示为数字自1970年1月1日以来的 days 天。Unix时间是指自1970年1月1日以来的秒数。您需要使用作为.POSIXct 函数,然后将其转换为数字对象:

The as.Date function represents a date as the number of days since Jan 1, 1970. Unix time refers to the number of seconds since Jan 1, 1970. You'll want to use the as.POSIXct function and then convert it to a numeric object:

as.double(as.Date(paste(year, month, day, sep="-"))) # days since 1970-1-1
as.double(as.POSIXct(as.Date(paste(year, month, day, sep="-")))) # seconds since 1970-1-1

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

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