您如何使用R中使用橄榄酸软件包的日期提取年份?

发布于 2025-02-09 00:20:44 字数 245 浏览 2 评论 0原文

尝试在列reign_start上使用ymd()函数,但给予 错误AS:

Warning: All formats failed to parse. No formats found.
[1] NA

中的年份表示。

BC

Tried using ymd() function on the column reign_start but give
error as:

Warning: All formats failed to parse. No formats found.
[1] NA

The years are represented in B.C.

The year is represented in B.C

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

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

发布评论

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

评论(1

新一帅帅 2025-02-16 00:20:44

ymd()用于将日期的字符表示转换为类date的变量, yyyy-mm-dd 。您的日期已经是类日期

您想要的lubridate函数是Year()。我不确定如何处理BC日期,但我认为您希望0026-01-16返回26

因此,尝试年(reign_start)。以下是一些需要ymd()首先(您的数据没有)的示例。

library(lubridate)
year(ymd("0026-01-16"))

[1] 26

# do we set year negative for BC? also works
year(ymd("-0026-01-16"))

[1] 26

ymd() is for converting character representations of dates into variables of class Date with format YYYY-MM-DD. Your dates are already of class Date.

The lubridate function you want is year(). I'm not sure how that handles BC dates but I assume you would like 0026-01-16 to return 26.

So try year(reign_start). Here are some examples which do require ymd() first (your data doesn't).

library(lubridate)
year(ymd("0026-01-16"))

[1] 26

# do we set year negative for BC? also works
year(ymd("-0026-01-16"))

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