in R, how can I see if three elements of a character vector are the same out of a vector of length 5 -
i have poker hand , need check 3 of kind. there way see if 3 elements in vector same other 2 different?
e.g.:
hand <- c("q","q","6","5","q")
should return true
3 of kind.
hand2 <- c("q","q","6","6","q")
...would full-house though, , shouldn't identified 3 of kind.
using table
, logic checking should there:
tab <- table(hand) #hand #5 6 q #1 1 3 any(tab==3) & (sum(tab==1)==2) #[1] true tab <- table(hand2) #hand2 #6 q #2 3 any(tab==3) & (sum(tab==1)==2) #[1] false
this works because any
across tab
le, checking if there card values repeated 3 times. tab==1
part of function checks if values in tab
le equal 1, returning true
or false
each part of table. sum
-ing true/false
values equivalent summing 1/0
values, if check had sum of 2
other cards, can sure different.
Comments
Post a Comment