我有一个动物园“ z”。有人可以解释一下为什么我加载动物园图书馆后为什么我的请求which(z[2,]>0 & z[1,]>0)
不再起作用了吗?
> str(z)
zoo [1:2, 1:12] 0 0 0 0 0 0 0 0 0 0 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : NULL
- attr(*, "index")=Classes 'dates', 'times' atomic [1:2] 17531 17562
.. ..- attr(*, "format")= chr "m/d/y"
.. ..- attr(*, "origin")= Named num [1:3] 1 1 1970
.. .. ..- attr(*, "names")= chr [1:3] "month" "day" "year"
> which(z[2,]>0 & z[1,]>0)
[1] 12
> library(zoo)
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
> which(z[2,]>0 & z[1,]>0)
integer(0)
对于那些感兴趣的人:
> dput(z)
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0.00996714478550151, 0.00996009385006366), .Dim = c(2L,
12L), .Dimnames = list(NULL, NULL), index = structure(c(17531,
17562), format = "m/d/y", origin = structure(c(1, 1, 1970), .Names = c("month",
"day", "year")), class = c("dates", "times")), class = "zoo")
答案 0 :(得分:0)
它将尝试合并z [1,]和z [2,]以执行&,但是它们没有共同点。
您可以尝试以下方法之一:
library(zoo)
## 1
which(apply(z > 0, 2, all))
## 2
which(pmin(z[1, ], z[2, ]) > 0)
## 3
which(colSums(z > 0) == nrow(z))
## 4
which(apply(z, 2, min) > 0)
## 5
cz <- coredata(z)
which(cz[1, ] > 0 & cz[2, ] > 0)
# 6
library(matrixStats)
which(colAlls(z > 0))