如何确定原产地在as.Date,R正确的说法 [英] How to determine the correct argument for origin in as.Date, R

查看:347
本文介绍了如何确定原产地在as.Date,R正确的说法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有R中的数据集,其中包含格式为yyyy / MM / DD日期的列。我试图用as.Date将这些日期转换为R. Date对象不过,我似乎无法找到原点正确的参数输入到as.Date。下面code是什么,我一直在努力的例子。我使用从Excel CSV文件,所以我用原点=基于其他网站1899年12月30日我已经看过。

I have a data set in R that contains a column of dates in the format yyyy/mm/dd. I am trying to use as.Date to convert these dates to date objects in R. However, I cannot seem to find the correct argument for origin to input into as.Date. The following code is an example of what I have been trying. I am using a CSV file from Excel, so I used origin="1899/12/30 based on other sites I have looked at.

> as.Date(2001/04/26, origin="1899/12/30")

 [1] "1900-01-18"

然而,这不是由于输入日期2001年4月26日的工作被返回为1900年1月18日。我需要的日期转换为Date对象,所以我就可以转换日期,Julian日期。

However, this is not working since the input date 2001/04/26 is returned as "1900-01-18". I need to convert the dates into date objects so I can then convert the dates into julian dates.

推荐答案

您既可以是 as.Date 用一个数值,或用字符值。当你只输入 2001年4月26日为R,这是做除法和得到19.24(数值)。和数值需要原产地和您提供的号码是从原点偏移。所以,你得到从产地19天了,即1900年1月18日。像2001年4月26日一个日期是

You can either is as.Date with a numeric value, or with a character value. When you type just 2001/04/26 into R, that's doing division and getting 19.24 (a numeric value). And numeric values require an origin and the number you supply is the offset from that origin. So you're getting 19 days away from your origin, ie "1900-01-18". A date like Apr 26 2001 would be

as.Date(40659, origin="1899-12-30")
# [1] "2011-04-26"

如果从Excel日期看起来像日期很有可能他们是字符值(或要素)。一个字符值转换为用 as.Date一个Date()你要那么指定的格式。在这里,

If your dates from Excel "look like" dates chances are they are character values (or factors). To convert a character value to a Date with as.Date() you want so specify a format. Here

as.Date("2001/04/26", format="%Y/%m/%d")
# [1] "2001-04-26"

?strptime 有关特殊变量的详细信息。现在,如果你读你的数据与函数read.table或东西data.frame,有一个机会,您的变量可能是一个因素。如果是这样的话,你会想要做转换为字符与

see ?strptime for details on the special % variables. Now if you're read your data into a data.frame with read.table or something, there's a chance your variable may be a factor. If that's the case, you'll want do convert to character with'

as.Date(as.character(mydf$datecol), format="%Y/%m/%d")

这篇关于如何确定原产地在as.Date,R正确的说法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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