转换mm-yy字符串“ Jan-01”转换成日期格式 [英] Convert a mm-yy string "Jan-01" into date format

查看:463
本文介绍了转换mm-yy字符串“ Jan-01”转换成日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的原始Excel文件中的日期代表 Jan-01格式的每月数据。当然,我可以先将它们手动转换为Excel中更有用的东西,然后再将其输入到R中。

The date in my original Excel file represents the monthly data in the format of "Jan-01". Of course, I can manually convert them to something more useful in the Excel first and then input that into R.

如何将这样的日期转换为R中的Date类?

How can I convert such adate into a Date class in R?

推荐答案

如果您想要 date 类,请使用as.Date :

If you want a date class, use as.Date:

x <- "Jan-01"
as.Date(paste("01-", x, sep = ""), format = "%d-%b-%y")

如果您想要一个 POSIXct 类,请考虑使用 lubridate

If you want a POSIXct class, consider using lubridate:

library(lubridate)
x <- "Jan-01"
dmy(paste("01-", x , sep =""))

如果您只想依赖 base

x <- "Jan-01"
as.POSIXct(paste("01-", x, sep = ""), format = "%d-%b-%y")

这篇关于转换mm-yy字符串“ Jan-01”转换成日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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