将不带年份的日期转换为儒略日(从年初开始的天数) [英] Convert date without year into Julian day (number of days since start of the year)

查看:291
本文介绍了将不带年份的日期转换为儒略日(从年初开始的天数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些日期需要转换为儒略日.这是我的数据

I have some date that I need to convert into Julian days. This is the data I have

date <- c("21-Jul", "14-Jul", "08-Jul", "08-Jul","16-Jul")
class(date)

[1] "character"

我想将date转换为儒略日.因此,对于上述日期,儒略日将是:

I want to convert date into julian days. So for the above dates, the Julian days will be:

date
202,195,189,189,197 

我找到了一个将日期转换为儒略日的函数as.POSIXlt.例如

I found a function as.POSIXlt that converts date into Julian day. For e.g.

tmp <- as.POSIXlt("16Jun10", format = "%d%b%y")
tmp$yday
# [1] 166

但是,这需要按特定的顺序排列,包括我没有的年份.有什么方法可以将我没有字符的日期转换为朱利安(Julian)?

But this needs date in a particular order including year which I do not have. Is there any way to convert my dates which are characters and do not have year in it into Julian?

今年也不是not年.

Also, this year is not a leap year.

儒略日表示一年中的某一天,因此1月1日为1,2月1日为2,依此类推....

Julian days imply the day of the year so 1st Jan is 1, 2nd Jan is 2 and so on....

推荐答案

base R具有函数julian,该函数可以与paste0as.Date一起生成您要查找的内容.

base R has a function julian that can produce what you are looking for together with paste0 and as.Date.

julian(as.Date(paste0("1970-", date), format="%Y-%d-%b"))
[1] 201 194 188 188 196
attr(,"origin")
[1] "1970-01-01"

请注意,这是从1970年1月1日开始的天数(从0开始),因此您可以加1以获得所需的结果.

Note that this is counted as number of days from January 1, 1970 (it starts at 0), so you can add 1 to get the desired result.

julian(as.Date(paste0("1970-", date), format="%Y-%d-%b")) + 1
[1] 202 195 189 189 197

我手动检查了1970年不是a年:

I manually checked that 1970 is not a leap year:

seq(as.Date("1970-02-25"), as.Date("1970-03-02"), by="day")
[1] "1970-02-25" "1970-02-26" "1970-02-27" "1970-02-28" "1970-03-01" "1970-03-02"

为了将来参考,也可以使用as.Date的origin参数自行设置起点:

For future reference, it is also possible to set the starting point yourself, using the origin argument to as.Date:

julian(as.Date(paste0("1980-", date), format="%Y-%d-%b"), origin=as.Date("1980-01-01"))
[1] 202 195 189 189 197
attr(,"origin")
[1] "1980-01-01"

应该清楚,可以将原点设置为您希望的任何日期,以便可以用来计算在任何日期之后(或之前)的天数.

As should be clear, origin can be set to any date that you wish, so that this could be used to calculate the number of days following (or preceding) any date.

这篇关于将不带年份的日期转换为儒略日(从年初开始的天数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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