本文介绍了如何从R中的数据框中删除带有inf的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的数据框( df ),大约有35-45列(变量),行数大于300。某些行包含NA,NaN,Inf,-Inf值或多个变量,我已经使用
na.omit(df)删除具有NA和NaN的行,但是我无法使用 na.omit 函数。

I have a very large dataframe(df) with approximately 35-45 columns(variables) and rows greater than 300. Some of the rows contains NA,NaN,Inf,-Inf values in single or multiple variables and I have used na.omit(df) to remove rows with NA and NaN but I cant remove rows with Inf and -Inf values using na.omit function.

在搜索时,我遇到了该线程 df [is.finite(df)] ,但不会删除带有Inf和-Inf的行,并且还会出现此错误

While searching I came across this thread Remove rows with Inf and NaN in R and used the modified code df[is.finite(df)] but its not removing the rows with Inf and -Inf and also gives this error

已编辑

删除整行,即使相应的一列或多列具有inf和-inf

要删除带有+/- Inf 的行,我建议以下内容:

To remove the rows with +/-Inf I'd suggest the following:

df <- df[!is.infinite(rowSums(df)),]

或等价

df <- df[is.finite(rowSums(df)),]

第二个选项(具有的是.finite()且不带否定符)也会删除包含 NA 值的行(如果尚未完成的话)。

The second option (the one with is.finite() and without the negation) removes also rows containing NA values in case that this has not already been done.

这篇关于如何从R中的数据框中删除带有inf的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!