tidyverse

Deck of Cards

Years ago, I had to simulate a deck of playing cards in a Python course. Oddly enough, I don’t recall ever carrying out this task in R. Today, I saw a neat code snippet about the crossing() command in tidyr by Tan Ho in the R4DS Slack channel, so let us give it a try. library("tidyverse") suit <- c("D", "H", "C", "S") rank <- c(2:10, "J", "Q", "K", "A") deck <- tidyr::crossing(rank, suit) |> dplyr::mutate(card = paste0(rank, suit)) This yields a data frame with 3 columns: rank, suit, card.

Continue reading