R格式化的日期原点

时间:2015-10-27 15:23:23

标签: r date

我阅读了this example关于使用origin设置日期开始时间的信息。我正在从csv阅读,其中日期是天数,并作为因素读入。

Example: $ admin_date    : Factor w/ 318 levels "37362","37735"

我想转换为日期格式来衡量事件之间的天数和月数。但是,当我尝试所有不同的origin时,例如:

df$admin_date_new <- as.numeric(df$admin_date)
df$admin_date_new <- as.Date(df$admin_date, origin="1900/01/01")

我得到以下内容:

 $ admin_date_new: Date, format: "1900-08-31"

这是找到正确origin的问题,还是我转换为numeric

时的问题

是否有快速找到原点的方法?我读了help("as.Date"),我得到的只是:

## date given as number of days since 1900-01-01 (a date in 1989)
as.Date(32768, origin = "1900-01-01")
## Excel is said to use 1900-01-01 as day 1 (Windows default) or
## 1904-01-01 as day 0 (Mac default), but this is complicated by Excel
## incorrectly treating 1900 as a leap year.
## So for dates (post-1901) from Windows Excel
as.Date(35981, origin = "1899-12-30") # 1998-07-05
## and Mac Excel
as.Date(34519, origin = "1904-01-01") # 1998-07-05
## (these values come from http://support.microsoft.com/kb/214330)

1 个答案:

答案 0 :(得分:2)

你没有给出预期的结果,但可能是这样:

x <- factor(c("37362", "37735"))
y <- as.numeric(as.character(x))
as.Date(y, origin = "1900-01-01")
#[1] "2002-04-18" "2003-04-26"

如果您不知道原点,则无法从您的数据中找到。