提取R中月度数据的第n天 [英] Extracting nth day of monthly data in r

查看:41
本文介绍了提取R中月度数据的第n天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用循环创建列时遇到了问题.

I'm facing a problem with making columns using loop.

我有一个xts数据集,它是每秒数据.

I have a xts dataset and it's second-by-second data.

它从

2014-09-01 00:00:00 104.172
2014-09-01 00:00:01 104.170
2014-09-01 00:00:02 104.170
2014-09-01 00:00:03 104.170
2014-09-01 00:00:04 104.170
2014-09-01 00:00:05 104.170

并以

2014-09-30 03:59:43 109.312
2014-09-30 03:59:44 109.312
2014-09-30 03:59:45 109.312
2014-09-30 03:59:46 109.312
2014-09-30 03:59:47 109.312
2014-09-30 03:59:48 109.313

我想从该数据集中创建第n天的栏.所以我做了这样的事情

I would like to make nth day columns from this data set. So I did something like this

for(i in 1:30){ask[i] <- ask[.indexmday(ask) == i]}

但是没有用.相反,我得到了警告

but it didn't work. Instead, I got a warning

number of items to replace is not a multiple of replacement length

当我这样做

asksep1 <- ask[.indexmday(ask) == 1]

它有效,我可以获得 9 月 1 日的数据.所以我认为我的循环有问题.

it works and I can get Sep-1st data. So I think there's something wrong with my loop.

我该怎么做?感谢您的帮助!

How can I do that? Thanks for any help!

推荐答案

您可以创建列表并将每天的子集存储在列表中.当每个的行数不同时,这可能会很有用.

You could create a list and store the subset for each day in a list. It might be useful when the number of rows are not the same for each day.

days <- unique(.indexmday(ask))
lst <- vector('list', length(days))
for(i in seq_along(days))lst[[i]] <- ask[.indexmday(ask)==days[i]]

sapply(lst, nrow)
# [1] 86400 86400 86400 86400 86400 86400 86400 86400 86400 86400 86400 86400
#[13] 86400 86400 86400 86400 86400 86400 86400 86400 86400 86400 86400 86400
#[25] 86400 86400 86400 86400 86400 14400

数据

val <- rnorm(840000*3, 110)
indx <- seq(as.POSIXct('2014-09-01 00:00:00', format='%Y-%m-%d %H:%M:%S'),
    length.out=840000*3, by='sec')
library(xts)
ask <- xts(val, order.by=indx)

这篇关于提取R中月度数据的第n天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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