【问题标题】:Date format does not work in R日期格式在 R 中不起作用
【发布时间】:2018-08-29 16:23:18
【问题描述】:

这是我的日期框上的日期列:

data <- data.frame(year_month = c("2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01", "2015-05-01", "2015-06-01"))
class(data$year_month)
[1] "factor"

我试过了:

 data$year_month1<-as.Date(as.factor(data$year_month), format="%b %Y")

我想要:

" Jan 2015", "Feb 2015", "Mar 2015", "Apr 2015", "May 2015", "Jun 2105"

会发生什么: 我的 data$year_month1 专栏为空。

我尝试了很多我在这里找到的解决方案,但格式根本没有改变,或者它是空的。

请帮忙>

【问题讨论】:

标签: date format character


【解决方案1】:
format(as.Date(data$year_month), format="%b %Y")

#first convert to Date, without as.factor (why did you do that?)
#then convert it to character as you want it, with format()

【讨论】:

  • 因为当我环顾四周时,我看到了一些解决方案,他们使用 as.factor 来改变它......愚蠢,我知道。但是非常感谢,它解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多