日期格式为R中的格式m / d / y [英] as.Date with dates in format m/d/y in R
问题描述
客户向我发送了3月15日格式为例如3/15/2012的Excel文件。我保存为.csv文件,然后使用
camm $ Date< - as.Date(camm $ Date,%m /%d /%y)
但这给了我从2020年开始的价值观!
我尝试重新格式化原始csv文件中的日期,以便它们是例如03/14/2013但是没有这样做。
任何帮助赞赏
使用资本 as.Date
中调用Y 。这应该是诀窍:
> as.Date(3/15/2012,%m /%d /%Y)
pre>
[1]2012-03-15
从帮助文件的示例可以看出,当年满时,您应该使用
%Y
否则%y
例如:>日期< - c(02/27/92,02/27/92,01/14/92,02/28/92,02/01/92)
> as日期(日期,%m /%d /%y)
[1]1992-02-271992-02-271992-01-141992-02- 281992-02-01
您可以看到,在您的示例中,Year格式为2012那么你应该使用
%Y
,另一个例子(取自as.Date
帮助文件)年份格式是92,然后使用%y
是正确的方法。有关详情,请参阅as.Date
。A client sent me an Excel file with dates formatted as e.g 3/15/2012 for March 15. I saved this as a .csv file and then used
camm$Date <- as.Date(camm$Date, "%m/%d/%y")
but this gave me values starting in the year 2020!
I tried to reformat the dates in the original csv file so that they were e.g. 03/14/2013 but was unable to do so.
Any help appreciated
解决方案Use capital
Y
inas.Date
call instead. This should do the trick:> as.Date("3/15/2012", "%m/%d/%Y") [1] "2012-03-15"
From the help file's examples you can realize when year is full specified you should use
%Y
otherwise%y
for example:> dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92") > as.Date(dates, "%m/%d/%y") [1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28" "1992-02-01"
You can see that in your example the Year format is 2012 then you should use
%Y
, and in the other example (taken from theas.Date
help file) Year format is 92 then using%y
is the correct way to go. Seeas.Date
for further details.这篇关于日期格式为R中的格式m / d / y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!