R格式的日期来源 [英] R Date origin for formatting
问题描述
我阅读了此示例有关使用 origin 的信息.代码>设置日期开始时间.我正在从csv中读取数据,其中日期是天数,并作为因素读取.
I read this example about using the origin
to set the date start time. I am reading in from csv where the dates are the number of days and read in as factors.
Example: $ admin_date : Factor w/ 318 levels "37362","37735"
我想转换为日期格式,以测量事件之间的天数和月数.但是当我尝试所有不同的 origin
时,例如:
I would like to convert to date formats to measure number of days and months between events. But when I try all of the different origin
such as:
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"
这是找到正确的起源
的原因,还是当我转换为数字
Is this something with finding the correct origin
or is this when I am converting to numeric
有没有一种快速的方法来找到必要的原点?我读了 help("as.Date")
,而我得到的只是:
Is there a quick way to find the origin necessary? I read help("as.Date")
and all I got was:
## 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)
推荐答案
您没有给出预期的结果,但是也许是这样:
You don't give the expected result, but maybe this:
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"
如果您不知道来源,就无法仅从数据中找出答案.
If you don't know the origin, there is no way to find out just from your data.
这篇关于R格式的日期来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!