Home > Back-end >  How do i create a logical value in R that is TRUE if the numbers are part of a specific category?
How do i create a logical value in R that is TRUE if the numbers are part of a specific category?

Time:10-02

I have to create a logical value named is. a flush which is TRUE if and only if the hand is a flush - A flush is a hand that contains five cards all of the same suit. This is my dataset

deck <- data.frame(suit = rep(c("D","C","H","S"), 13),rank = rep(2:14, 4))

For reference= # D = ♦ Diamond, C = ♣ Club, H = ♥ Heart, S = ♠ Spade

11 = Jack, 12 = Queen, 13 = King, 14 = Ace

This is what I tried so far: I just picked manually 5 values that share the same suit and used the unique function to prove what i know already, however, this is clearly not a good way and I also am not creating any logical value.

hand <- deck[c(43,15,35,7,27),]
unique(hand)

Any help is appreciated! Cheers

CodePudding user response:

You canch chek if all suits in hand$suit are equal to the first suit (hand$suit[1]) in that vector.

is.Flush <- all(hand$suit == hand$suit[1])
is.Flush

#> TRUE
  • Related