0

Is there a way to replace NAN with 0 in the below code. Because there are cases when both numerator and denominator is 0 and when divided returns NAN

> mean(0/0)
[1] NaN
user11740857
  • 502
  • 3
  • 10
  • 1
    will this work `replace_na(mean(0/0),0)` ? – Karthik S Jun 25 '21 at 08:06
  • You can use an ifelse condition to check if your value is Nan : `ifelse(object == "Nan", 0, object)` A sample of your data could help us to have a more precise solution. But assuming it is in a dataframe, you get : `mutate(column = ifelse(column == "Nan",0,column)` – MonJeanJean Jun 25 '21 at 08:07
  • perfect thanks alot – user11740857 Jun 25 '21 at 08:08
  • Suppose your dataframe is df. You can use this to replace all of the NaN from your df. `df <- df %>% mutate(across(where(is.numeric), ~replace_na(.x,0)))` – user20203146 Jun 13 '23 at 12:06

0 Answers0