R将因子YYYY-MM转换为日期 [英] R converting a factor YYYY-MM to a date
本文介绍了R将因子YYYY-MM转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,日期格式为YYYY-MM,类别因素,我正在尝试将其转换为课程日期。
I have a dataframe with a date in the form YYYY-MM, class factor and I am trying to convert it to class date.
我试过: p>
I tried:
Date <- c("2015-08","2015-09","2015-08")
Val <- c(1,2,3)
df <- data.frame(Date,Val)
df[,1] <- as.POSIXct(as.character(df[,1]), format = "%Y-%m")
df
但这不行。我将非常感谢您的帮助。
But this does not work. I would be grateful for your help.
推荐答案
R不支持%Y-%m格式的日期。一天需要
R does not support Dates in the format "%Y-%m"... A day is needed
您可以执行以下操作:
as.POSIXct(paste0(as.character(df[,1]),"-01"), format = "%Y-%m-%d")
导致
"2015-08-01 CEST" "2015-09-01 CEST" "2015-08-01 CEST"
这篇关于R将因子YYYY-MM转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文