本文介绍了在R数据表中替换Inf in /显示列中的Inf数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我无法弄清楚如何在R中使用is.na(x)类型的函数来处理无限数字数据表,或者每列显示有多少个Inf。colSums(is.infinite(x)) 我使用以下示例数据集: 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)] DT abc 1:Inf a Inf 2:1 b 5 3:Inf c 100 $ b b DT [is.infinite(DT)] 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 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 c1: Inf a Inf2: 1 b 53: Inf c NAcolSums(is.na(DT))a b c0 0 1colSums(is.infinite(DT))Error in is.infinite(DT) : default method not implemented for type 'list'DT[is.na(DT)] <- 100 DT a b c1: Inf a Inf2: 1 b 53: Inf c 100DT[is.infinite(DT)] <- 100Error 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 colSumsDT[, colSums(sapply(.SD, is.infinite))]# a b c# 2 0 1Alternatively, you could use Reduce instead of colSumsDT[, Reduce(`+`, lapply(.SD, is.infinite))]## [1] 2 0 1Another option is to create your own custom function and then just loop it over the columnsMyfunc <- function(x) sum(is.infinite(x))DT[, lapply(.SD, Myfunc)]# a b c# 1: 2 0 1Of course you could also write data.frame method for is.infinite as it appears to be generic (see ?is.infinite). 这篇关于在R数据表中替换Inf in /显示列中的Inf数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!