转换mm-yy字符串“Jan-01”在R中的日期格式 [英] convert a mm-yy string "Jan-01" into date format in R

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

问题描述

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



如何将这种adate转换为R中的Date类

解决方案

如果您想要一个日期类,请使用as.Date :

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

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



<$
$< - Jan-01
dmy(粘贴(01-,x,sep =))

如果你只想依靠 base

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


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.

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

解决方案

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

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

If you want a POSIXct class, consider using lubridate:

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

If you want to just rely on base:

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

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

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