Removing 'Inf' values in R dataframe | Data cleaning in R

├Źndice
  1. What are 'Inf' values in R dataframes?
  2. Why remove 'Inf' values in R dataframes?
  3. How to remove 'Inf' values in R dataframes?

What are 'Inf' values in R dataframes?

'Inf' or infinity values are a special type of numeric value in R that represent positive or negative infinity. They can occur in dataframes when performing mathematical operations like division by zero or logarithmic functions on zero or negative values.

Why remove 'Inf' values in R dataframes?

Although 'Inf' values may not affect the outcome of some calculations, they can lead to errors in statistical analysis or machine learning models. It is important to remove them before proceeding with data analysis.

How to remove 'Inf' values in R dataframes?

The following code snippet shows how to remove 'Inf' values from a dataframe named 'df':


df[!is.infinite(df)] # removes all 'Inf' values in df

This code uses the 'is.infinite()' function to identify the 'Inf' values in the dataframe and the negation operator '!' to select all non-'Inf' values. The resulting dataframe will have all 'Inf' values removed.

Alternatively, you can replace 'Inf' values with a more appropriate value like 'NA' (missing value) using the 'replace()' function:


df[df == Inf] <- NA # replaces all 'Inf' values with 'NA'

Now that you know how to remove 'Inf' values from dataframes in R, you can clean your data and perform accurate analyses.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information