1

I have written a Fun that I use to normalize a set of numbers from 0 to 5. but I have NAs as part of my data and would like the Fun to skip them and move to the next value.

I have tried this code:

IDNO<-1:11
Scores<-c(1:10,NA)
ScoreDF<-as.data.frame(cbind(IDNO,Scores))
Normalize.Final.Score.fun <- function(x){
  5*(x-min(x))/(max(x)-min(x))
}

ScoreDF<-ScoreDF %>% 
  mutate(BotanicalVal=Normalize.Final.Score.fun(Scores))

I expect the output to be as I would get running this line:

ScoreDF$ExpectedBotanicalVal<- c(Normalize.Final.Score.fun(Scores[1:10]),"NA")

[1] "0" "0.555555555555556" "1.11111111111111" [4] "1.66666666666667" "2.22222222222222" "2.77777777777778" [7] "3.33333333333333" "3.88888888888889" "4.44444444444444" [10] "5"
"NA"

Whereas I get only NAs. Thanks in advance for your help

Idan
  • 43
  • 6

1 Answers1

1

Just add na.rm = TRUE to your calls

Normalize.Final.Score.fun <- function(x) {
  5 * (x - min(x, na.rm = TRUE)) / (max(x, na.rm = TRUE) - min(x, na.rm = TRUE))
}
s_baldur
  • 29,441
  • 4
  • 36
  • 69