如何在xts对象中用NA替换Inf和NaN

时间:2016-01-09 07:34:48

标签: r xts

我有一个xts对象(df):

##                  a     b
## 2015-09-14 -0.5470    NA
## 2015-09-15  0.0112    NA
## 2015-09-16  0.0910 0.932
## 2015-09-17    -Inf 0.862
## 2015-09-18     Inf 1.946
## 2015-09-21 -0.7050 2.692
## 2015-09-22     NaN 2.011
## 2015-09-23 -0.5440 1.859
##     .
##     .
##    etc

由于列a包含Inf-InfNaN,我收到了一些错误。有没有办法用NA替换这些值(将InfNaN视为缺失值)?

1 个答案:

答案 0 :(得分:3)

您可以像普通<select class='filter_class' name="filter" data-userid="29" > <option value="all">All</option> <option value="asc">Asc</option> <option value="desc">Desc</option> </select> <select class='filter_class' name="filter" data-userid="30" > <option value="all">All</option> <option value="asc">Asc</option> <option value="desc">Desc</option> </select> <script> $(".filter_class").change(function(){ var user_id = $(this).data('userid'); alert(user_id); }) </script>

一样使用apply

使用您提供的数据:

matrix

另一种方法是通过索引要保留(或删除)

的元素来进行替换
d <- xts(read.zoo(text='date a     b
2015-09-14 -0.5470    NA
2015-09-15  0.0112    NA
2015-09-16  0.0910 0.932
2015-09-17    -Inf 0.862
2015-09-18     Inf 1.946
2015-09-21 -0.7050 2.692
2015-09-22     NaN 2.011
2015-09-23 -0.5440 1.859', header=T, index=1))

apply(d, 2, function(x) ifelse(is.finite(x), x, NA))

##                  a     b
## 2015-09-14 -0.5470    NA
## 2015-09-15  0.0112    NA
## 2015-09-16  0.0910 0.932
## 2015-09-17      NA 0.862
## 2015-09-18      NA 1.946
## 2015-09-21 -0.7050 2.692
## 2015-09-22      NA 2.011
## 2015-09-23 -0.5440 1.859