jeudi 12 juillet 2018

normalize function with condition

I am working on creating a normalization function based on a condition.

The below code works without a condition.

normalized <- function(x){
  return((x-mean(x)) / (sd(x)))
}

normalized_dataset<-final_dataset %>% 
  dplyr::group_by(Col1) %>% 
  dplyr::mutate_at(vars(-one_of("Col1","Col2")), normalized)

This does not work on the otherhand -

##Index All Columns with If Statement
normalized_conditional<-function(x){
  out<- ifelse(x>0, (x-min(x) / sd(x)),(x-max(x) / sd(x)))
  return(out)
}

normalized_conditional_dataset<-final_dataset %>% 
  dplyr::group_by(Col1) %>% 
  dplyr::mutate_at(vars(-one_of("Col1","Col2")), normalized_conditional)

The output for this is an unchanged dataset. Any idea what I am doing wrong?

Aucun commentaire:

Enregistrer un commentaire