Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Tableau
  3. Question
Tableau

Cleaning `Inf` values from an R dataframe

Asked by Buffy Heaton Jul 17, 2021 1.9K views 1 answer
Share

About this question

In R, I have an operation which creates some Inf values when I transform a data frame.

I would like to turn these Inf values into NA values. The code I have is slow for large data, is there a faster way of doing this?

Say I have the following data frame:

dat <- data.frame(a=c(1, Inf), b=c(Inf, 3), d=c("a","b"))

The following works in a single case:

 dat[,1][is.infinite(dat[,1])] = NA

So I generalized it with the following loop

cf_DFinf2NA <- function(x)
{
    for (i in 1:ncol(x)){
          x[,i][is.infinite(x[,i])] = NA
    }
    return(x)
}

But I don't think that this is really using the power of R.

Your answer

1 Answer

More Tableau discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Tableau Blogs

Guides, tips and career advice on Tableau from JanBask experts.