简体   繁体   English

使用R中的xts获取每周的第一天和最后一天

[英]get first and last day of each week using xts in R

I read this fantastic solution in this post . 我在这篇文章中读到了这个梦幻般的解 I would like to get two time series objects. 我想得到两个时间序列对象。 One for the first day of each week and other for the last day of each week. 一个是每周的第一天,另一个是每周的最后一天。 My time series is irregular in the sense that doesn't include weekends and holidays. 我的时间序列是不规则的,不包括周末和假期。 Weeks go from Monday to Sunday. 周从周一到周日。 This is the beggining of my series: 这是我的系列的开始:

         Date   Val WeekNum
1  01/02/1990 41.38       1
2  02/02/1990 42.88       1
3  05/02/1990 42.00       2
4  06/02/1990 41.50       2
5  07/02/1990 42.25       2
6  08/02/1990 41.75       2
7  09/02/1990 42.13       2
8  12/02/1990 42.13       3
9  13/02/1990 42.00       3
10 14/02/1990 42.63       3
11 15/02/1990 43.75       3
12 16/02/1990 44.75       3
13 20/02/1990 44.13       4
14 21/02/1990 43.88       4
15 22/02/1990 44.38       4
16 23/02/1990 44.00       4
17 26/02/1990 44.00       5
18 27/02/1990 44.88       5
19 28/02/1990 44.50       5
20 01/03/1990 44.63       5
21 02/03/1990 46.00       5
22 05/03/1990 45.88       6
23 06/03/1990 45.50       6
24 07/03/1990 45.38       6
25 08/03/1990 43.63       6
26 09/03/1990 41.50       6
27 12/03/1990 41.38       7
28 13/03/1990 40.63       7
29 14/03/1990 40.25       7
30 15/03/1990 40.50       7
31 16/03/1990 40.50       7
32 19/03/1990 40.25       8
33 20/03/1990 39.88       8
34 21/03/1990 39.75       8
35 22/03/1990 38.50       8
36 23/03/1990 38.75       8
37 26/03/1990 39.63       9
38 27/03/1990 39.75       9

Then I use from the reference post: 然后我从参考文章中使用:

library(xts)
do.call(rbind, lapply(split(x, "weeks"), function(x) x[1]))

EDIT: it wasn't working because I had the data as of zoo instead of xts. 编辑:它没有工作,因为我有动物园而不是xts的数据。

You can simply use the first and last functions. 您可以简单地使用第firstlast函数。 You could also use head and tail . 你也可以使用headtail

# first/last
do.call(rbind, lapply(split(x, "weeks"), first))
do.call(rbind, lapply(split(x, "weeks"), last))
# head/tail
do.call(rbind, lapply(split(x, "weeks"), head, 1))
do.call(rbind, lapply(split(x, "weeks"), tail, 1))

You can use .indexwday to get day week number. 您可以使用.indexwday获取日期周数。 Then using range you get the first and last one. 然后使用range你得到第一个和最后一个。 Since , not all weeks begin with monday and finish with friday, it is better to take the min and max of each week. 因为,并非所有周都从周一开始并且周五结束,所以最好每周采取最小值和最大值。

To get the first and the last day of each week you can do something like this : 要获得每周的第一天和最后一天,您可以执行以下操作:

do.call(rbind, lapply(split(dat.xts, "weeks"), 
                      function(y) y[.indexwday(y) %in% range(.indexwday(y))]))



            Val WeekNum
1990-02-01 41.38       1
1990-02-02 42.88       1
1990-02-05 42.00       2
1990-02-09 42.13       2
1990-02-12 42.13       3
1990-02-16 44.75       3
1990-02-20 44.13       4
1990-02-23 44.00       4
1990-02-26 44.00       5
1990-03-02 46.00       5
1990-03-05 45.88       6
1990-03-09 41.50       6
1990-03-12 41.38       7
1990-03-16 40.50       7
1990-03-19 40.25       8
1990-03-23 38.75       8
1990-03-26 39.63       9
1990-03-27 39.75       9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM