在R数据表中替换Inf in /显示列中的Inf数 [英] Replace Inf in R data.table / Show number of Inf in colums

查看:3147
本文介绍了在R数据表中替换Inf in /显示列中的Inf数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何在R中使用is.na(x)类型的函数来处理无限数字数据表,或者每列显示有多少个Inf。colSums(is.infinite(x))



我使用以下示例数据集:

  DT < data.table(a = c(1 / 0,1,2 / 0),b = c(a,b,c),c = c(1 / b $ b DT 
abc
1:Inf a Inf
2:1 b 5
3:Inf c NA
colSums(is.na(DT))
abc
0 0 1
colSums(is.infinite(DT))
is.infinite(DT)中的错误:类型'list'未实现默认方法
DT [ is.na(DT)] < - 100
DT
abc
1:Inf a Inf
2:1 b 5
3:Inf c 100 $ b b
DT [is.infinite(DT)] < - 100
is.infinite(DT)中的错误:类型'list'未实现默认方法

我在这篇文章如何用NA替换Inf,但我会说应该有更好的方式实现这一点,以is.infinite为例。我想看看Inf的每栏,有关这个的任何想法吗?



非常感谢。
BR Tim

解决方案

is.finite is.infinite 没有 data.frame data.table 方法 is.na 有(比较方法(is.infinite) vs 方法(is.na)



您可以循环浏览列,然后使用 colSums

  DT [,colSums(sapply(.SD,is.infinite))] 
#abc
#2 0 1






可以使用减少而不是 colSums

  DT [,Reduce(`+`,lapply(.SD,is.infinite))] 
## [1] 2 0 1
/ pre>




另一种选择是创建自己的自定义函数,然后将其循环到列

  Myfunc < -  function(x)sum(is.infinite(x))
DT [,lapply(.SD,Myfunc) ]
#abc
#1:2 0 1

还写 is.infinite data.frame 方法,因为它看起来是通用的(参见 ?is.infinite )。


I can't figure out how to use an is.na(x) like function for infinite numbers in R with a data table or show per column how many Inf's there are: colSums(is.infinite(x))

I use the following example data set:

DT <- data.table(a=c(1/0,1,2/0),b=c("a","b","c"),c=c(1/0,5,NA))
DT
     a b   c
1: Inf a Inf
2:   1 b   5
3: Inf c   NA
colSums(is.na(DT))
a b c 
0 0 1 
colSums(is.infinite(DT))
Error in is.infinite(DT) : default method not implemented for type 'list'
DT[is.na(DT)] <- 100
 DT
     a b   c
1: Inf a Inf
2:   1 b   5
3: Inf c 100

DT[is.infinite(DT)] <- 100
Error in is.infinite(DT) : default method not implemented for type 'list'

I found in this post how to replace Inf with NA, but I would say there should be nicer way of achieving this, with is.infinite for example. And I would like to see the Inf's per column, any ideas about this?

Many thanks. BR Tim

解决方案

is.finite and is.infinite don't have a data.frame or a data.table methods like is.na has (compare methods(is.infinite) vs methods(is.na))

You could alternatively loop thru the columns and then use colSums

DT[, colSums(sapply(.SD, is.infinite))]
# a b c 
# 2 0 1 


Alternatively, you could use Reduce instead of colSums

DT[, Reduce(`+`, lapply(.SD, is.infinite))]
## [1] 2 0 1


Another option is to create your own custom function and then just loop it over the columns

Myfunc <- function(x) sum(is.infinite(x))
DT[, lapply(.SD, Myfunc)]
#    a b c
# 1: 2 0 1

Of course you could also write data.frame method for is.infinite as it appears to be generic (see ?is.infinite).

这篇关于在R数据表中替换Inf in /显示列中的Inf数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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