【问题标题】:Formatting yearmon function of package zoo in r在r中格式化包zoo的yearmon函数
【发布时间】:2015-12-06 06:20:43
【问题描述】:

我正在使用 yearmon 函数

library(zoo)
test<-as.Date(c("2015-12-25","2015-11-01"))
test<-as.Date(test,"%y-%m-%d")
test<-as.yearmon(test,"%y-%m")
test

输出

"Dec 2015" "Nov 2015"

我希望输出是 2015 年 11 月。我想要这个的原因是因为我使用它来创建热图,并且 2015 年 12 月显示在 2015 年 11 月之前的序列中,因为变量在 D 上排序。附上问题

Image of the issue for a larger dataset

【问题讨论】:

  • 请注意,sort(as.yearmon(c("2015-12-25","2015-11-01"))) 排序正确(11 月之前 12 月)。请在可重现的示例中显示您尝试使用的实际代码。

标签: r


【解决方案1】:

yearmon类有格式化方法:

> format(test, "%Y %b")
[1] "2015 Dec" "2015 Nov"

yearmon-objects 实际上存储为小数年

> as.numeric(test)
[1] 2015.917 2015.833

我相信format.yearmon 在强制之后调用format.Dateformat.POSIXt,由“格式”字符串的内容决定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-29
    • 2023-04-05
    • 2018-09-12
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多