在R中转换文本日期 [英] Convert Text Date in R
问题描述
我有格式化日期的数据,如下所示:
I have data that formats dates like so:
Tue Oct 25 2016
Tue Oct 25 2016
Tue Oct 25 2016
Wed Oct 26 2016
Wed Oct 26 2016
Wed Oct 26 2016
我希望其格式为 R
可以将其用作日期(即 2016-10-25
).有帮助吗?
I would like this to be in a format in which R
can use it as a date (i.e. 2016-10-25
). Any help?
推荐答案
修改:我莫名其妙地想念您也有星期几.
Edit: I somehow missed that you had the day of the week as well.
这也可以使用 as.Date
和正确的格式字符串在基础上完成.在这种情况下,%a
将为您提供星期的缩写,%B
将为您提供月份的缩写,%d
将为您提供月份的缩写最小位数的天(即2而不是02),而%Y
将为您提供四位数的年份.在示例中,这些都用一个空格隔开,因此格式字符串应反映出这一点.
This can also be done in base using as.Date
with the correct formatting string. In this case %a
would give you the abbreviated day of the week,%B
will give you the month abbreviation, %d
will give you the minimum digit day (i.e. 2 instead of 02), and %Y
will give you the four digit year. In the example these are all separated by a single space, so the format string should reflect that.
datesx <- c("Tue Oct 25 2016", "Tue Oct 25 2016", "Wed Oct 26 2016", "Wed Oct 26 2016", "Wed Oct 26 2016", "Wed Oct 26 2016")
as.Date(datesx,format = "%a %B %d %Y")
[1]"2016-10-25""2016-10-25""2016-10-26""2016-10-26""2016-10-26""2016-10-26"
这篇关于在R中转换文本日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!