如何为 R 中动物园对象的每个月的最后一周子集? [英] How do I subset the last week for every month of a zoo object in R?

查看:22
本文介绍了如何为 R 中动物园对象的每个月的最后一周子集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几年的每日价格数据作为动物园对象.在 R 中对每个"月的最后一周进行子集划分的最佳方法是什么?

I have a few years of daily price data as a zoo object. What's the best way to subset the last week of 'every' month in R?

以下是复制它的方法:

set.seed(123)
price <- rnorm(365)
data <- cbind(seq(as.Date("2013-01-01"), by = "day", length.out = 365), price)
zoodata <- zoo(data[,2], as.Date(data[,1]))

我试过这个选项,但它只返回整个数据集的最后一周.

I tried this option but it only returns the last week of the entire data set.

do.call(rbind, lapply(split(zoodata, "months"), last, "1 week"))

结果输出为:

2013-12-30 2013-12-31 
 1.0246732  0.8176594 

我希望获得每个月的最后一周,所以应该有至少 12 周的数据.

I'm hoping to get the last week of every month so there should be at least 12 weeks of data.

提前致谢!

推荐答案

这里是使用 xts 包的方法

Here's a way using the xts package

## Create sample data
library(xts)
data(sample_matrix)
x <- as.xts(sample_matrix)
##

# split by month, and apply the last() function to each group
lapply(split(x, "months"), last, "1 week") 

如果你希望结果是单个对象,你可以使用 do.call

If you want the results to be a single object, you can rbind the list using do.call

do.call(rbind, lapply(split(x, "months"), last, "1 week"))
               Open     High      Low    Close
2007-01-29 49.85624 49.93038 49.76308 49.91875
2007-01-30 49.85477 50.02180 49.77242 50.02180
2007-01-31 50.07049 50.22578 50.07049 50.22578
2007-02-26 50.88168 50.88168 50.75481 50.75481
2007-02-27 50.74333 50.78909 50.61874 50.69206
2007-02-28 50.69435 50.77091 50.59881 50.77091
2007-03-26 48.34210 48.44637 48.28969 48.28969
2007-03-27 48.25248 48.41572 48.23648 48.30851
2007-03-28 48.33090 48.53595 48.33090 48.53595
2007-03-29 48.59236 48.69988 48.57432 48.69988
2007-03-30 48.74562 49.00218 48.74562 48.93546
2007-03-31 48.95616 49.09728 48.95616 48.97490
2007-04-30 49.13825 49.33974 49.11500 49.33974
2007-05-28 47.90142 47.93398 47.64718 47.64718
2007-05-29 47.65665 47.89342 47.65446 47.87252
2007-05-30 47.78866 47.93267 47.78866 47.83291
2007-05-31 47.82845 47.84044 47.73780 47.73780
2007-06-25 47.20471 47.42772 47.13405 47.42772
2007-06-26 47.44300 47.61611 47.44300 47.61611
2007-06-27 47.62323 47.71673 47.60015 47.62769
2007-06-28 47.67604 47.70460 47.57241 47.60716
2007-06-29 47.63629 47.77563 47.61733 47.66471
2007-06-30 47.67468 47.94127 47.67468 47.76719

注意:如果它很重要,你可以将 as.zoo() 包裹起来,但是 xts 对象是 zoo 对象,所以它是可能没有必要.

Note: if it's important, you can wrap as.zoo() around it, but xts objects are zoo objects, so it's probably not necessary.

这篇关于如何为 R 中动物园对象的每个月的最后一周子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆