本文介绍了创建一个独特的日期序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我想生成一个包含的列的数据框,结构如下:
2011-08-01
2011-08-02
2011-08-03
2011-08-04
...
我想知道是否可以使用 seq()
命令。
如下所示:(显然不起作用)
seq(2011-08-01:2011-08-31)
使用日期和正则表达式生成此
特定格式的日期。
解决方案
正如我在评论中指出的, seq
具有日期的方法, seq.Date
:
seq(as.Date('2011-01-01'),as.Date('2011-01-31'),by = 1)
[1] 2011-01-012011-01-022011-01-032011-01-042011-01-052011-01-062011-01-072011 -01-08
[9]2011-01-092011-01-102011-01-11 2011-01-122011-01-132011-01-142011-01-152011-01-16
[17]2011-01-17 2011-01-182011-01-192011-01-202011-01-212011-01-222011-01-232011-01-24
[25]2011-01-252011-01-262011-01-272011-01-282011-01-292011-01-302011 -01-31
Let's say that I want to generate a data frame which contains a column withis structured in the following format.
2011-08-01
2011-08-02
2011-08-03
2011-08-04
...
I want to know if it's possible to generate this data with the seq()
command.
Something like the following: (obviously doesn't work)
seq(2011-08-01:2011-08-31)
Would I instead have to use toDate and regex to generate this date in thisspecific format.
解决方案
As I noted in my comment, seq
has method for dates, seq.Date
:
seq(as.Date('2011-01-01'),as.Date('2011-01-31'),by = 1)
[1] "2011-01-01" "2011-01-02" "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06" "2011-01-07" "2011-01-08"
[9] "2011-01-09" "2011-01-10" "2011-01-11" "2011-01-12" "2011-01-13" "2011-01-14" "2011-01-15" "2011-01-16"
[17] "2011-01-17" "2011-01-18" "2011-01-19" "2011-01-20" "2011-01-21" "2011-01-22" "2011-01-23" "2011-01-24"
[25] "2011-01-25" "2011-01-26" "2011-01-27" "2011-01-28" "2011-01-29" "2011-01-30" "2011-01-31"
这篇关于创建一个独特的日期序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!