无法正确使用动物园的 as.yearmon,转换后的列的年份显示为 0086 而不是 1986

发布于 2025-01-15 03:10:33 字数 589 浏览 2 评论 0原文

您好,感谢您来到这里。

我正在尝试使用 as.yearmon 转换日期,但得到的结果很奇怪,我不知道如何解决这个问题;我尝试在此处和官方软件包指南上进行搜索,但没有找到遇到相同问题的人。 问题是我用 as.yearmon 转换了格式为“月/年”的数据集的列,但结果不正确。

例如,原始DF的列的前3个值为:“1/86”、“2/86”、“3/86”。

使用此函数转换它们后:

library(zoo)
Dates <- Returns
Dates$Month<- zoo::as.yearmon(Dates$Month, "%m / %Y")

[其中“Returns”是原始数据帧,并用修改后的日期标记新数据帧。]

我得到的结果,而不是:“gen 1986”,“feb 1986”,“mar 1986 ”是“gen 0086”、“feb 0086”、“mar 0086”,我不知道为什么。

[我不应该认为“gen”、“feb”、“mar”是意大利语;我不知道这是否重要,我不知道如何将其更改为“一月”,“二月”,“三月”,我想我也必须这样做]

提前感谢您的帮助,如果有的话不清楚请告诉我;我还是个菜鸟。

Hello and thanks for being here.

I'm trying to convert dates with as.yearmon but the results I'm getting are odd and I do not know how to fix this; I tried searching on here and on the offical package guide without finding someone with the same problem.
The problem is that I transformed a column of a dataset which was formatted as "month/year" with as.yearmon but the results were not correct.

For example, the first 3 values of the column of the original DF are: "1/86", "2/86", "3/86".

After using this function to convert them:

library(zoo)
Dates <- Returns
Dates$Month<- zoo::as.yearmon(Dates$Month, "%m / %Y")

[Where "Returns" is the original dataframe and Dates the new one with the modified dates.]

The result I got, instead of being: "gen 1986", "feb 1986", "mar 1986" was "gen 0086", "feb 0086", "mar 0086" and I don know why.

[I should not that "gen", "feb", "mar" are in Italian; I do not know if that matters and I do not know how to change that to "Jan", "Feb", "Mar" which I think I'll have to do as well]

Thanks in advance for your help, if something is not clear just let me know; I'm still a rookie.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫离伤花 2025-01-22 03:10:33

您需要使用“%m/%y”而不是“%m/%Y”。您的日期没有全年符号。

x <- c("1/86", "2/86", "3/86")

zoo::as.yearmon(x, "%m/%y")
[1] "jan 1986" "feb 1986" "mrt 1986"

日期函数返回本地语言环境中的名称。如果您想要英语版本:

Sys.setlocale("LC_TIME", "English")

zoo::as.yearmon(x, "%m/%y")
[1] "Jan 1986" "Feb 1986" "Mar 1986"

每次重新启动 R 时,都会将其设置回您的语言环境。

更多信息此处

You need to use "%m/%y" instead of "%m/%Y". Your dates don't have a the full year notation.

x <- c("1/86", "2/86", "3/86")

zoo::as.yearmon(x, "%m/%y")
[1] "jan 1986" "feb 1986" "mrt 1986"

Date functions return the names in the local locale. If you want them in English:

Sys.setlocale("LC_TIME", "English")

zoo::as.yearmon(x, "%m/%y")
[1] "Jan 1986" "Feb 1986" "Mar 1986"

Everytime you restart R, this will be set back to your locale.

More info here on SO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文