使用日期作为索引从向量中选择值
假设我有一个命名向量 bar
:
bar=c()
bar["1997-10-14"]=1
bar["2001-10-14"]=2
bar["2007-10-14"]=1
如何从 bar
中选择索引位于特定日期范围内的所有值?因此,如果我查找 "1995-01-01"
和 "2000-06-01"
之间的所有值,我应该得到 1
。同样,对于 "2001-09-01"
和 "2007-11-04"
之间的时间段,我应该得到 2
和 1
。
Suppose I have a named vector, bar
:
bar=c()
bar["1997-10-14"]=1
bar["2001-10-14"]=2
bar["2007-10-14"]=1
How can I select from bar
all values for which the index is within a specific date range? So, if I look for all values between "1995-01-01"
and "2000-06-01"
, I should get 1
. And similarly for the period between "2001-09-01"
and "2007-11-04"
, I should get 2
and 1
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题已经通过 xts 包得到了很好的解决,该包扩展了 zoo 包。
这里还有很多内容——但基本点是不要对伪装成日期的字符串进行操作。
使用
Date
类型,或者最好使用一个扩展包来有效地索引数百万个日期。This problem has been solved for good with the xts package which extends functionality from the zoo package.
There is a lot more here -- but the basic point is do not operate on strings masquerading as dates.
Use a
Date
type, or preferably even an extension package built to efficiently index on millions of dates.您需要使用
as.Date()
将日期从字符转换为Date
类型(如果您有更多信息,例如一天中的时间,则为 POSIX 类型)。然后,您可以与标准关系运算符(例如<=和>=)进行比较。为此,您应该考虑使用诸如
zoo
之类的时间序列包。编辑:
只是为了回应您的评论,这里有一个将日期与现有向量一起使用的示例:
尽管您确实应该只使用时间序列包。下面介绍了如何使用
zoo
(或xts
、timeSeries
、fts
等)执行此操作:索引现在是
Date
类型,您可以根据需要进行任意多次比较。阅读zoo
小插图了解更多信息。You need to convert your dates from characters into a
Date
type withas.Date()
(or a POSIX type if you have more information like the time of day). Then you can make comparisons with standard relational operators such as <= and >=.You should consider using a timeseries package such as
zoo
for this.Edit:
Just to respond to your comment, here's an example of using dates with your existing vector:
Although you really should just use a time series package. Here's how you could do this with
zoo
(orxts
,timeSeries
,fts
, etc.):Since the index is now a
Date
type, you can make as many comparisons as you want. Read thezoo
vignette for more information.使用日期按词汇顺序排列的事实:
结果被命名为向量(正如您原来的
bar
一样,它不是一个列表,它被命名为向量)。正如 Dirk 在他的回答中所述,出于效率原因,最好使用
Date
。如果没有外部包,您可以重新排列数据并创建两个向量(或两列data.frame
),一个用于日期,一个用于值:然后使用简单的索引:
Using fact that dates are in lexical order:
Result is named vector (as you original
bar
, it's not a list it's named vector).As Dirk states in his answer it's better to use
Date
for efficiency reasons. Without external packages you could rearrange you data and create two vectors (or two-columndata.frame
) one for dates, one for values:then use simple indexing: