0

Title says it all, I created a composite index which has some NaN's in it. How would I go on and remove theses NaN's from a specific column (my composite index)?

I have looked at is.nan() but that just tells me if it's NaN or not...

FALA
  • 11
  • 3
  • 3
    (1) Please make this reproducible with usable data, preferably pasting the output from `dput(x)` where `x` is a representative sample that includes some `NaN` and some not. (2) Please go back to your [previous questions](https://stackoverflow.com/users/14795367/fala?tab=questions) and, for those with posted answers, really consider [accepting](https://stackoverflow.com/help/someone-answers) them. It's not strictly required, but intentionally avoiding that is a little counter-culture on StackOverflow. – r2evans Jan 19 '22 at 16:56
  • 1
    Does this answer your question? [How to replace NaN value with zero in a huge data frame?](https://stackoverflow.com/questions/18142117/how-to-replace-nan-value-with-zero-in-a-huge-data-frame) – divibisan Jan 20 '22 at 20:48

1 Answers1

0

You can use an if-else statement with is.nan() as the conditional and set the value to 0 on true else set the value to the number.

More information here: Does the ternary operator exist in R?

From the link, credit to the author of that accepted answer for the following example(s) of code:

if-else

> x <- if(a==1) 1 else 2
> x
[1] 1
> x <- if(a==2) 1 else 2
> x
[1] 2