格式日期在yyyymmdd [英] Format Date in R yyyymmdd

查看:252
本文介绍了格式日期在yyyymmdd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将格式为YYYYMMDD的这些日期转换为Date类别。

 日期<  -  data.frame Date = c(20130707,20130706,20130705,20130704))

我尝试了:

 日期< -  as.Date(日期,%Y%m%d)

我收到以下错误:

 在as.Date.default中出错(日期,%Y%m%d):
不知道如何将日期转换为类日期



设置此格式的正确方法是什么?

解决方案

您需要提供日期列,而不是整个 data.frame

  R> 
[1]2013-07-072013-07-062013-07-05 2013-07-04


I would like to convert these dates with format YYYYMMDD to a Date class.

dates <- data.frame(Date = c("20130707", "20130706", "20130705", "20130704"))

I tried:

dates <- as.Date(dates, "%Y%m%d")

And I get the following error:

Error in as.Date.default(dates, "%Y%m%d") : 
  do not know how to convert 'dates' to class "Date"

What would be the correct way to set this format?

解决方案

You need to provide the Date column, not the entire data.frame.

R> as.Date(dates[["Date"]], "%Y%m%d")
[1] "2013-07-07" "2013-07-06" "2013-07-05" "2013-07-04"

这篇关于格式日期在yyyymmdd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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