将年月(“yyyy-mm”格式)转换为日期?
我有一个如下所示的数据集:
Month count
2009-01 12
2009-02 310
2009-03 2379
2009-04 234
2009-05 14
2009-08 1
2009-09 34
2009-10 2386
我想绘制数据(月份为 x 值,计数为 y 值)。由于数据存在空白,我想将月份的信息转换为日期。我尝试过:
as.Date("2009-03", "%Y-%m")
但没有成功。怎么了?似乎 as.Date()
也需要一天,并且无法设置该天的标准值?哪个功能可以解决我的问题?
I have a dataset that looks like this:
Month count
2009-01 12
2009-02 310
2009-03 2379
2009-04 234
2009-05 14
2009-08 1
2009-09 34
2009-10 2386
I want to plot the data (months as x values and counts as y values). Since there are gaps in the data, I want to convert the Information for the Month into a date. I tried:
as.Date("2009-03", "%Y-%m")
But it did not work. Whats wrong? It seems that as.Date()
requires also a day and is not able to set a standard value for the day? Which function solves my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
由于日期对应于一个数值和一个开始日期,因此您确实需要该日期。如果您确实需要数据采用日期格式,则可以通过将其粘贴到日期来手动将日期固定为每月的第一天:
Since dates correspond to a numeric value and a starting date, you indeed need the day. If you really need your data to be in Date format, you can just fix the day to the first of each month manually by pasting it to the date:
试试这个。 (这里我们使用
text=Lines
来保持示例自包含,但实际上我们会将其替换为文件名。)X 轴对于此数据来说不太漂亮,但如果您有更多数据现实中可能没问题,或者您可以使用
?plot.zoo
示例部分中所示的精美 X 轴代码。上面创建的动物园系列
z
具有"yearmon"
时间索引,如下所示:"yearmon"
可以单独使用还有:注意:
"yearmon"
类对象按日历顺序排序。这将以等间隔绘制每月点,这可能是想要的;但是,如果需要以与每月天数成比例的不等间隔绘制点,则将
z
的索引转换为"Date"
类:time(z) <- as.Date(time(z))
.Try this. (Here we use
text=Lines
to keep the example self contained but in reality we would replace it with the file name.)The X axis is not so pretty with this data but if you have more data in reality it might be ok or you can use the code for a fancy X axis shown in the examples section of
?plot.zoo
.The zoo series,
z
, that is created above has a"yearmon"
time index and looks like this:"yearmon"
can be used alone as well:Note:
"yearmon"
class objects sort in calendar order.This will plot the monthly points at equally spaced intervals which is likely what is wanted; however, if it were desired to plot the points at unequally spaced intervals spaced in proportion to the number of days in each month then convert the index of
z
to"Date"
class:time(z) <- as.Date(time(z))
.如果您需要日期格式为日期,最简洁的解决方案是:
as.Date
将为您将每个月的第一天固定为yearmon 对象。The most concise solution if you need the dates to be in Date format:
as.Date
will fix the first day of each month to a yearmon object for you.您还可以使用
lubridate
包中的parse_date_time
或fast_strptime
函数来实现此目的:这两者之间的区别在于
parse_date_time< /code> 允许 lubridate 风格的格式规范,而
fast_strptime
需要与strptime
相同的格式规范。要指定时区,您可以使用
tz
参数:当日期时间数据存在不规则情况时,您可以使用 truncated 参数来指定不规则情况的数量允许:
使用的数据:
You could also achieve this with the
parse_date_time
orfast_strptime
functions from thelubridate
-package:The difference between those two is that
parse_date_time
allows for lubridate-style format specification, whilefast_strptime
requires the same format specification asstrptime
.For specifying the timezone, you can use the
tz
-parameter:When you have irregularities in your date-time data, you can use the
truncated
-parameter to specify how many irregularities are allowed:Used data:
使用 anytime 包:
Using anytime package:
事实上,正如上面提到的(以及其他地方),为了将字符串转换为日期,您需要月份的特定日期。从
as.Date()
手册页:一个简单的解决方案是将日期
"01"
粘贴到每个日期,并使用strptime()
将其指示为该月的第一天。对于那些寻求在 R 中处理日期和时间的更多背景知识的人:
在 R 中,时间使用
POSIXct
和POSIXlt
类和日期使用Date
类。日期存储为自 1970 年 1 月 1 日以来的天数,时间存储为自 1970 年 1 月 1 日以来的秒数。
因此,例如:
要对日期和时间执行操作:
要处理日期,您可以使用 < code>strptime() (从手册页借用这些示例):
Indeed, as has been mentioned above (and elsewhere on SO), in order to convert the string to a date, you need a specific date of the month. From the
as.Date()
manual page:A simple solution would be to paste the date
"01"
to each date and usestrptime()
to indicate it as the first day of that month.For those seeking a little more background on processing dates and times in R:
In R, times use
POSIXct
andPOSIXlt
classes and dates use theDate
class.Dates are stored as the number of days since January 1st, 1970 and times are stored as the number of seconds since January 1st, 1970.
So, for example:
To perform operations on dates and times:
And to process dates, you can use
strptime()
(borrowing these examples from the manual page):一种使用
lubridate
中的ym
的方法。月份可以是数字、缩写月份或带有各种分隔符的完整月份名称(即使没有分隔符),例如
在给定的数据上:
请注意,如果您有的话,还有
my
反之亦然,例如Sep/2022
。数据
A way using
ym
fromlubridate
.The month can either be a number, an abbreviated month or a full month name with a variety of separators (even without separator), e.g.
on the given data:
Note that there's also
my
if you have it the other way round, e.g.Sep/2022
.Data
我认为@ben-rollert 的解决方案是一个很好的解决方案。
如果您想在新包内的函数中使用此解决方案,您必须小心。
开发包时,建议使用语法
packagename::function_name()
(请参阅 http://kbroman.org/pkg_primer/pages/depends.html)。在这种情况下,您必须使用由
zoo
库定义的as.Date()
版本。这是一个示例:
因此,如果您正在开发一个包,最好的做法是使用:
I think @ben-rollert's solution is a good solution.
You just have to be careful if you want to use this solution in a function inside a new package.
When developping packages, it's recommended to use the syntaxe
packagename::function_name()
(see http://kbroman.org/pkg_primer/pages/depends.html).In this case, you have to use the version of
as.Date()
defined by thezoo
library.Here is an example :
So if you're developping a package, the good practice is to use :
tidyverse
最近添加了clock
包添加到lubridate
,它有一些很好的功能:日期操作和提取
它的输出是一个年-月-日向量,您可以在其中仍然可以进行日期算术并应用其他常见功能,例如预期:
如果需要,您还可以使用
set_day
设置日期:处理无效日期
或者,如果您想使用此结构干净地获取每个月的最后一天,
invalid_*
函数集可以提供帮助:您可以使用
invalid_resolve
处理无效日期,也可以使用invalid_remove
删除它们:来自文档你可以为
invalid
参数指定以下值来处理无效日期:tidyverse
recently added theclock
package in addition tolubridate
that has some nice functionality for this:Date Manipulation and Extraction
The output of this is a year-month-day vector where you can still do date arithmetic and apply other common functions as expected:
You can also set the day, if you need it, with
set_day
:Handling Invalid Dates
Or if you wanted to cleanly get the last day of every month with this structure, the
invalid_*
set of functions can help:You can handle invalid dates with
invalid_resolve
or you can use drop them withinvalid_remove
:From the documentation you can specify the following values for the
invalid
argument to handle invalid dates: