如何将因子转换为日期格式? [英] How do I convert a factor into date format?

查看:56
本文介绍了如何将因子转换为日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 SQL 查询中导入了一个带有日期的 CSV 文件,但这些日期实际上是日期时间值,而 R 似乎无法将它们识别为日期:

I imported a CSV file with dates from a SQL query, but the dates are really date-time values and R doesn't seem to recognize them as dates:

> mydate
[1] 1/15/2006 0:00:00
2373 Levels: 1/1/2006 0:00:00 1/1/2007 0:00:00 1/1/2008 0:00:00 ... 9/9/2012 0:00:00
> class(mydate)
[1] "factor"
> as.Date(mydate)
Error in charToDate(x) : 
  character string is not in a standard unambiguous format

如何将 mydate 转换为日期格式?(我不需要包括时间部分.)

How do I convert mydate to date format? (I don't need to include the time portion.)

推荐答案

你已经接近了.format= 需要添加到 as.Date 调用中:

You were close. format= needs to be added to the as.Date call:

mydate <- factor("1/15/2006 0:00:00")
as.Date(mydate, format = "%m/%d/%Y")
## [1] "2006-01-15"

这篇关于如何将因子转换为日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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