Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如果一列在同一天内满足某些条件,则为xts对象的子集_R_Xts - Fatal编程技术网

R 如果一列在同一天内满足某些条件,则为xts对象的子集

R 如果一列在同一天内满足某些条件,则为xts对象的子集,r,xts,R,Xts,我想排除x2等于零超过预定次数(即同一天>300)的日子: 库(xts) 种子(1) tmp这里有一个解决方案: ##split idx object with respect to days aa <- split.xts(x, f="days") ## get indices of days for which x2 == 0 less than 300 times idx <- which(lapply(aa, function(xx){length(which(xx[,"x

我想排除
x2
等于零超过预定次数(即同一天>300)的日子:

库(xts)
种子(1)
tmp这里有一个解决方案:

##split idx object with respect to days
aa <- split.xts(x, f="days")

## get indices of days for which x2 == 0 less than 300 times
idx <- which(lapply(aa, function(xx){length(which(xx[,"x2"]==0))}) <= 300)

idx
[1] 2 3 4

##make one xts object containing only the desired days
new.x <- do.call(rbind, aa[idx])

dim(x)
[1] 5760    2

dim(new.x)
[1] 4320    2
##根据天数拆分idx对象

aa无论您使用什么解决方案,从POSIXt转换为Date时都需要小心时区。下面是一个使用
ave
的解决方案:

> x <- xts(cbind(x1, x2), tmp, tzone="UTC")
> y <- x[ave(x$x2==0, as.Date(index(x)), FUN=sum) < 300,]
> head(y)
                            x1         x2
2013-09-04 00:00:01  0.6855122  0.8171146
2013-09-04 00:01:01  0.3895035  0.1818066
2013-09-04 00:02:01 -1.3053959  1.2532384
2013-09-04 00:03:01  1.2168880  0.6069871
2013-09-04 00:04:01  0.7951740  0.2825354
2013-09-04 00:05:01 -0.4882025 -0.3089424
>x y头(y)
x1-x2
2013-09-04 00:00:01  0.6855122  0.8171146
2013-09-04 00:01:01  0.3895035  0.1818066
2013-09-04 00:02:01 -1.3053959  1.2532384
2013-09-04 00:03:01  1.2168880  0.6069871
2013-09-04 00:04:01  0.7951740  0.2825354
2013-09-04 00:05:01 -0.4882025 -0.3089424

您的问题不符合StackOverflow模型(它不是机械土耳其人)。看到了吗?@Joshua Ulrich我想说得很具体,但如果不清楚,我很抱歉。我已经编辑了我的问题。问题不是你太具体了。这是因为您没有显示您尝试了什么或输出应该是什么。展示您的尝试非常重要,因为这表明您理解我们不是来为您工作的。谢谢您的解决方案。我一定是做错了什么,因为我尝试了
tzone=Sys.timezone()
,但无法将
y
子集
Sys.timezone()
不能保证返回任何有用的内容,因为它是特定于操作系统的,正如
Sys.timezone
中所述。最好获取/设置
TZ
环境变量。
> x <- xts(cbind(x1, x2), tmp, tzone="UTC")
> y <- x[ave(x$x2==0, as.Date(index(x)), FUN=sum) < 300,]
> head(y)
                            x1         x2
2013-09-04 00:00:01  0.6855122  0.8171146
2013-09-04 00:01:01  0.3895035  0.1818066
2013-09-04 00:02:01 -1.3053959  1.2532384
2013-09-04 00:03:01  1.2168880  0.6069871
2013-09-04 00:04:01  0.7951740  0.2825354
2013-09-04 00:05:01 -0.4882025 -0.3089424