简体   繁体   English

具有多个时间序列(列)的动物园中的子集值

[英]subsetting values in a zoo with multiple time-series (columns)

I am trying to substitute -Inf for NA in a multi column zoo.我正在尝试在多列动物园中将 -Inf 替换为 NA 。 However when I tried to subset the values that are -Inf in the zoo object the way I would do for a matrix it does not work and therefore the substitution cannot be made:但是,当我尝试对动物园 object 中的 -Inf 值进行子集化时,就像我对矩阵所做的那样,它不起作用,因此无法进行替换:

library(zoo)
mat = matrix(1:50, ncol = 5)
mat[2,5]=-Inf
mat[8,3]=-Inf
colnames(mat) = letters[1:5]

z = as.zoo(mat)

mat[is.infinite(mat)] = NA
mat

z[is.infinite(z)]= NA
z

I can do this with a loop but it seems like it should not be necessary to use a loop for something so basic.我可以用循环来做到这一点,但似乎没有必要为如此基本的事情使用循环。

Logical subscripting will refer to the index.逻辑下标将引用索引。 Instead do it this way:而是这样做:

z[] <- ifelse(is.infinite(z), NA, z)

or if you want to assign it to a different variable the rhs would be:或者,如果您想将其分配给不同的变量,则 rhs 将是:

z * ifelse(is.infinite(z), NA, 1)

This also works:这也有效:

coredata(z)[is.infinite(z)] <- NA

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

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