mercredi 6 septembre 2017

Multiple ifelse to build an additional vector to add as a column

I wrote an function which is checking each row of a data.frame. The checks are conditional statements which print YES or NO. Should be very straight forward. But somehow the results are not what was intended. I've tried 2 different ways.

Please find some test data here: TEST TABLE

add_accepted_column <- function(df){
  shopmanager_status <- df[28]
  source_medium <- df[22]
  campaign <- df[23]
  click_to_conversion <- df[29]
  accepted <- c()
  if(shopmanager_status %in% c("complete","processing") & source_medium %in% c("tradetracker / nl", "google / organic", "bing / organic", "yahoo / organic")){
      accepted <- c(accepted,"YES")
  }
  else if(shopmanager_status %in% c("complete","processing") & click_to_conversion < 1){
    accepted <- c(accepted,"YES")
  } 
  else if(shopmanager_status %in% c("complete","processing") & campaign %in% c("1.Top-Brand")){
    accepted <- c(accepted,"YES")
  } else{
    accepted <- c(accepted,"NO")
  }
}

# 
 add_accepted_column <- function(df){
   shopmanager_status <- df[28]
   source_medium <- df[22]
   campaign <- df[23]
   click_to_conversion <- df[29]
   #print(class(click_to_conversion))
   accepted <- c()
   accepted <- c(accepted,if_else(shopmanager_status %in% c("complete","processing") &
                   source_medium %in% c("tradetracker / nl", "google / organic", "bing / organic", "yahoo / organic") ||
                     click_to_conversion < 1 ||
                     campaign %in% c("1.Top-Brand"),"YES","NO"))
   return(accepted)
 }

It is super weird that for example google / cpc Give me a YES.

If more examples are needed im happy to supply. Somehow I cant find out what is going on with the IF ELSE statements.

Aucun commentaire:

Enregistrer un commentaire