lundi 9 novembre 2020

How to check each element value of a vector testing for two specific values?

library(tidyverse)

I have a vector, called Drinks_Choice

Drink_Choice <- c(NA, 3, 4, 2, 1, 2, 5, NA)

Where 1-5 represets a certain drink type

Once NAs have been ommited I want to be able to test each element of the vector if is has a value of 1 or 5. Initially I tried using a for loop:

Drink_Choice %>%
    na.omit()%>%
    if (Drink_Choice == 1|5) {
          cat("Even\n")
        } else {
          cat("Odd\n")
        }   

As an output I need TRUE (where element is 1 or 5) or FALSE (where element is 2 to 4). Many thanks for any help!!

Aucun commentaire:

Enregistrer un commentaire