samedi 23 octobre 2021

R: "if else if" vs "or" with three cases

Im running through some old code to clean it up and saw an old if else if statement that brought up a question in my mind; this is a super simple logic question but for my own edification and to make sure I'm thinking about this logically I wanted to see if a change from the 'old way' to the 'new way', in essence for this case at least, would be the same. My question is: are these two ways of doing this essentially the same?

 #old way
df_test = tibble::as_tibble(data.frame(column1 = NA))

if(NROW(df_test$column1) == 0) {
  df_test = TRUE
} else if (all(is.na(df_test$column1))) {
  df_test = TRUE
} else {
  df_test = FALSE
}

#new way
df_test = tibble::as_tibble(data.frame(column1 = NA))

if(NROW(df_test$column1) == 0 | all(is.na(df_test$column1))) {
  df_test = TRUE
} else {
  df_test = FALSE
}

Aucun commentaire:

Enregistrer un commentaire