将yyyymmdd字符串转换为R中的Date类 [英] Convert yyyymmdd string to Date class in R

查看:126
本文介绍了将yyyymmdd字符串转换为R中的Date类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我尝试过:

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

我得到以下错误:

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?

推荐答案

您需要提供 Date 列,而不是整个 data.frame 列。

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字符串转换为R中的Date类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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