将月份年份转换为R中的日期 [英] Convert month year to a date in r
本文介绍了将月份年份转换为R中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将以下类型的字符串转换为日期格式。
I need to convert following types of strings to a date format.
Convert "Feb 2009" to 2009-02-01
Convert "Jan 2010" to 2010-01-01
Convert "Mar 2011" to 2011-03-01
我可以使用 zoo
包从以下代码中实现。
I can achieve this from the following code using zoo
package.
as.Date(as.yearmon("Feb 2009"))
但是由于某些限制,我不想使用这种转换方式。所以我想知道R中是否还有其他方法可以完成此任务?
But due to some constraints I do not want to use this way of converting. So I want to know if there is any other way in R of achieving this task?
推荐答案
您可以粘贴<$ c $使用粘贴
将向量c> 01 转换为向量,然后通过指定适当的值将其转换为日期
format
You can paste 01
to the vector using paste
and then convert to date
by specifying the appropriate format
as.Date(paste('01', v1), format='%d %b %Y')
#[1] "2009-02-01" "2010-01-01", "2011-03-01"
数据
data
v1 <- c("Feb 2009", "Jan 2010", "Mar 2011")
这篇关于将月份年份转换为R中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文