Creating A Deck Of Cards In R Without Using While And Double For Loop











up vote
5
down vote

favorite
2












I am creating a blackjack simulator in R. The code below succeeds in creating the deck(s) of cards that I want. (For those that play, I will deal with the value of an Ace later).



My question is, is there a better way to create the deck that doesn't involve a while loop plus a double for loop? I have more of an issue with the double for loop. The while loop is likely unavoidable since the number of decks created is variable.



I also initialize an empty data frame which I know isn't the best practice, however, the data set is so small in this case that it won't effect performance.



And lastly, is there an equivalent of i++ in R? I have been programming in java as well and have gotten used to it.



Thanks.



createDeck <- function(totalNumOfDecks = 2)
{
suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
cards <- c("Ace", "Deuce", "Three", "Four","Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King")
values <- c(0,2,3,4,5,
6,7,8,9,10,
10,10,10)

deck <- data.frame(Suit=character(0), Card=character(0), Value=numeric(0))

numOfDecks = 1

while (numOfDecks <= totalNumOfDecks){
for (i in suits){
for (j in cards){
deck <- rbind.data.frame(deck, cbind.data.frame(j, i, values[match(j, cards)]))
}
}
numOfDecks = numOfDecks + 1
}

print(deck)
}









share|improve this question


















  • 1




    See ?expand.grid. But frankly, it would probably be easier to just use a single factor with 52 levels.
    – joran
    Jul 10 '14 at 19:54






  • 2




    If josilber's method for creating N decks with characterstrings works for you, then let me just suggest that, since presumably you're going to want to shuffle your cards at some point, that you create a "shuffler" something like cardorder<-sample(1:(N*52),N*52) and use the resulting vector to re-order the deck rows.
    – Carl Witthoft
    Jul 10 '14 at 20:06






  • 1




    ...just to expand on my comment: what I meant was that I would probably just use a integer vector 0:51 for the cards and then use modular arithmetic to determine suit and rank, and indexing of another vector to determine value. That would probably be much, much faster than pushing data.frame's around.
    – joran
    Jul 10 '14 at 20:17










  • @CarlWitthoft - I wasn't going to shuffle, rather, I would remove one card at random.
    – mks212
    Jul 11 '14 at 16:31










  • @joran I am interested in your idea, but how would modular arithmetic help in this case? I can't make the connection.
    – mks212
    Jul 11 '14 at 16:31















up vote
5
down vote

favorite
2












I am creating a blackjack simulator in R. The code below succeeds in creating the deck(s) of cards that I want. (For those that play, I will deal with the value of an Ace later).



My question is, is there a better way to create the deck that doesn't involve a while loop plus a double for loop? I have more of an issue with the double for loop. The while loop is likely unavoidable since the number of decks created is variable.



I also initialize an empty data frame which I know isn't the best practice, however, the data set is so small in this case that it won't effect performance.



And lastly, is there an equivalent of i++ in R? I have been programming in java as well and have gotten used to it.



Thanks.



createDeck <- function(totalNumOfDecks = 2)
{
suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
cards <- c("Ace", "Deuce", "Three", "Four","Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King")
values <- c(0,2,3,4,5,
6,7,8,9,10,
10,10,10)

deck <- data.frame(Suit=character(0), Card=character(0), Value=numeric(0))

numOfDecks = 1

while (numOfDecks <= totalNumOfDecks){
for (i in suits){
for (j in cards){
deck <- rbind.data.frame(deck, cbind.data.frame(j, i, values[match(j, cards)]))
}
}
numOfDecks = numOfDecks + 1
}

print(deck)
}









share|improve this question


















  • 1




    See ?expand.grid. But frankly, it would probably be easier to just use a single factor with 52 levels.
    – joran
    Jul 10 '14 at 19:54






  • 2




    If josilber's method for creating N decks with characterstrings works for you, then let me just suggest that, since presumably you're going to want to shuffle your cards at some point, that you create a "shuffler" something like cardorder<-sample(1:(N*52),N*52) and use the resulting vector to re-order the deck rows.
    – Carl Witthoft
    Jul 10 '14 at 20:06






  • 1




    ...just to expand on my comment: what I meant was that I would probably just use a integer vector 0:51 for the cards and then use modular arithmetic to determine suit and rank, and indexing of another vector to determine value. That would probably be much, much faster than pushing data.frame's around.
    – joran
    Jul 10 '14 at 20:17










  • @CarlWitthoft - I wasn't going to shuffle, rather, I would remove one card at random.
    – mks212
    Jul 11 '14 at 16:31










  • @joran I am interested in your idea, but how would modular arithmetic help in this case? I can't make the connection.
    – mks212
    Jul 11 '14 at 16:31













up vote
5
down vote

favorite
2









up vote
5
down vote

favorite
2






2





I am creating a blackjack simulator in R. The code below succeeds in creating the deck(s) of cards that I want. (For those that play, I will deal with the value of an Ace later).



My question is, is there a better way to create the deck that doesn't involve a while loop plus a double for loop? I have more of an issue with the double for loop. The while loop is likely unavoidable since the number of decks created is variable.



I also initialize an empty data frame which I know isn't the best practice, however, the data set is so small in this case that it won't effect performance.



And lastly, is there an equivalent of i++ in R? I have been programming in java as well and have gotten used to it.



Thanks.



createDeck <- function(totalNumOfDecks = 2)
{
suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
cards <- c("Ace", "Deuce", "Three", "Four","Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King")
values <- c(0,2,3,4,5,
6,7,8,9,10,
10,10,10)

deck <- data.frame(Suit=character(0), Card=character(0), Value=numeric(0))

numOfDecks = 1

while (numOfDecks <= totalNumOfDecks){
for (i in suits){
for (j in cards){
deck <- rbind.data.frame(deck, cbind.data.frame(j, i, values[match(j, cards)]))
}
}
numOfDecks = numOfDecks + 1
}

print(deck)
}









share|improve this question













I am creating a blackjack simulator in R. The code below succeeds in creating the deck(s) of cards that I want. (For those that play, I will deal with the value of an Ace later).



My question is, is there a better way to create the deck that doesn't involve a while loop plus a double for loop? I have more of an issue with the double for loop. The while loop is likely unavoidable since the number of decks created is variable.



I also initialize an empty data frame which I know isn't the best practice, however, the data set is so small in this case that it won't effect performance.



And lastly, is there an equivalent of i++ in R? I have been programming in java as well and have gotten used to it.



Thanks.



createDeck <- function(totalNumOfDecks = 2)
{
suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
cards <- c("Ace", "Deuce", "Three", "Four","Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King")
values <- c(0,2,3,4,5,
6,7,8,9,10,
10,10,10)

deck <- data.frame(Suit=character(0), Card=character(0), Value=numeric(0))

numOfDecks = 1

while (numOfDecks <= totalNumOfDecks){
for (i in suits){
for (j in cards){
deck <- rbind.data.frame(deck, cbind.data.frame(j, i, values[match(j, cards)]))
}
}
numOfDecks = numOfDecks + 1
}

print(deck)
}






r for-loop while-loop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 10 '14 at 19:46









mks212

2751424




2751424








  • 1




    See ?expand.grid. But frankly, it would probably be easier to just use a single factor with 52 levels.
    – joran
    Jul 10 '14 at 19:54






  • 2




    If josilber's method for creating N decks with characterstrings works for you, then let me just suggest that, since presumably you're going to want to shuffle your cards at some point, that you create a "shuffler" something like cardorder<-sample(1:(N*52),N*52) and use the resulting vector to re-order the deck rows.
    – Carl Witthoft
    Jul 10 '14 at 20:06






  • 1




    ...just to expand on my comment: what I meant was that I would probably just use a integer vector 0:51 for the cards and then use modular arithmetic to determine suit and rank, and indexing of another vector to determine value. That would probably be much, much faster than pushing data.frame's around.
    – joran
    Jul 10 '14 at 20:17










  • @CarlWitthoft - I wasn't going to shuffle, rather, I would remove one card at random.
    – mks212
    Jul 11 '14 at 16:31










  • @joran I am interested in your idea, but how would modular arithmetic help in this case? I can't make the connection.
    – mks212
    Jul 11 '14 at 16:31














  • 1




    See ?expand.grid. But frankly, it would probably be easier to just use a single factor with 52 levels.
    – joran
    Jul 10 '14 at 19:54






  • 2




    If josilber's method for creating N decks with characterstrings works for you, then let me just suggest that, since presumably you're going to want to shuffle your cards at some point, that you create a "shuffler" something like cardorder<-sample(1:(N*52),N*52) and use the resulting vector to re-order the deck rows.
    – Carl Witthoft
    Jul 10 '14 at 20:06






  • 1




    ...just to expand on my comment: what I meant was that I would probably just use a integer vector 0:51 for the cards and then use modular arithmetic to determine suit and rank, and indexing of another vector to determine value. That would probably be much, much faster than pushing data.frame's around.
    – joran
    Jul 10 '14 at 20:17










  • @CarlWitthoft - I wasn't going to shuffle, rather, I would remove one card at random.
    – mks212
    Jul 11 '14 at 16:31










  • @joran I am interested in your idea, but how would modular arithmetic help in this case? I can't make the connection.
    – mks212
    Jul 11 '14 at 16:31








1




1




See ?expand.grid. But frankly, it would probably be easier to just use a single factor with 52 levels.
– joran
Jul 10 '14 at 19:54




See ?expand.grid. But frankly, it would probably be easier to just use a single factor with 52 levels.
– joran
Jul 10 '14 at 19:54




2




2




If josilber's method for creating N decks with characterstrings works for you, then let me just suggest that, since presumably you're going to want to shuffle your cards at some point, that you create a "shuffler" something like cardorder<-sample(1:(N*52),N*52) and use the resulting vector to re-order the deck rows.
– Carl Witthoft
Jul 10 '14 at 20:06




If josilber's method for creating N decks with characterstrings works for you, then let me just suggest that, since presumably you're going to want to shuffle your cards at some point, that you create a "shuffler" something like cardorder<-sample(1:(N*52),N*52) and use the resulting vector to re-order the deck rows.
– Carl Witthoft
Jul 10 '14 at 20:06




1




1




...just to expand on my comment: what I meant was that I would probably just use a integer vector 0:51 for the cards and then use modular arithmetic to determine suit and rank, and indexing of another vector to determine value. That would probably be much, much faster than pushing data.frame's around.
– joran
Jul 10 '14 at 20:17




...just to expand on my comment: what I meant was that I would probably just use a integer vector 0:51 for the cards and then use modular arithmetic to determine suit and rank, and indexing of another vector to determine value. That would probably be much, much faster than pushing data.frame's around.
– joran
Jul 10 '14 at 20:17












@CarlWitthoft - I wasn't going to shuffle, rather, I would remove one card at random.
– mks212
Jul 11 '14 at 16:31




@CarlWitthoft - I wasn't going to shuffle, rather, I would remove one card at random.
– mks212
Jul 11 '14 at 16:31












@joran I am interested in your idea, but how would modular arithmetic help in this case? I can't make the connection.
– mks212
Jul 11 '14 at 16:31




@joran I am interested in your idea, but how would modular arithmetic help in this case? I can't make the connection.
– mks212
Jul 11 '14 at 16:31












4 Answers
4






active

oldest

votes

















up vote
6
down vote



accepted










The expand.grid function should be helpful:



# Define suits, cards, values
suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
cards <- c("Ace", "Deuce", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")
values <- c(0, 2:9, rep(10, 4))
totalNumOfDecks <- 2

# Build deck, replicated proper number of times
deck <- expand.grid(cards=cards, suits=suits)
deck$value <- values
deck <- deck[rep(seq(nrow(deck)), totalNumOfDecks),]


The call to expand.grid computes all pairing of cards and suits. The value variable is created by recycling the value vector for each suit. Finally, rep(seq(nrow(deck))) repeats rows 1-52 the proper number of times to get multiple copies of your deck.






share|improve this answer




























    up vote
    3
    down vote













    If I had to simulate a deck (or multiple decks) I would probably prefer to use a single integer vector in conjunction with some reference vectors, and then simply use modular arithmetic and indexing to determine suit, rank and value.



    #Setup
    suits <- c('Clubs','Diamonds','Hearts','Spades')
    card <- c('Ace','Two','Three','Four','Five',
    'Six','Seven','Eight','Nine','Ten',
    'Jack','Queen','King')
    value <- c(0,2:10,rep(10,3))
    deck <- 0:51

    #Full deck
    suits[(deck %/% 13) + 1]
    card[(deck %% 13) + 1]
    value[(deck %% 13) + 1]

    #Some random cards
    hand <- sample(deck,5)
    suits[(hand %/% 13) + 1]
    card[(hand %% 13) + 1]
    value[(hand %% 13) + 1]





    share|improve this answer




























      up vote
      2
      down vote













      buildDeck <- function(noOfDecks=1){
      suits <- c("Clubs", "Spades", "Diamonds", "Hearts")
      cards <-c("Ace", 2:10, "Jack", "Queen", "King")
      Deck <- paste(cards, rep(suits, each=13), sep="-")
      d<-rep(Deck,noOfDecks) #Build decks
      shuffledDeck <-sample(d,length(d)) #Shuffle decks
      shuffledDeck
      }





      share|improve this answer




























        up vote
        2
        down vote













        This is related to my recent paste.grid question; see there for some other options, including the straightforward levels(interaction(...)) approach.



        Just made a deck myself and it uses the unicode characters for the suits so it looks snazzy; here's what I did:



        cards = c(2:10, "J", "Q", "K", "A")
        suits = c("♠", "♥", "♦", "♣")
        deck <- paste0(rep(cards, length(suits)), #card values
        rep(suits, each = length(cards))) #suits
        deck
        # [1] "2♠" "3♠" "4♠" "5♠" "6♠" "7♠" "8♠" "9♠" "10♠" "J♠" "Q♠" "K♠"
        # [13] "A♠" "2♥" "3♥" "4♥" "5♥" "6♥" "7♥" "8♥" "9♥" "10♥" "J♥" "Q♥"
        # [25] "K♥" "A♥" "2♦" "3♦" "4♦" "5♦" "6♦" "7♦" "8♦" "9♦" "10♦" "J♦"
        # [37] "Q♦" "K♦" "A♦" "2♣" "3♣" "4♣" "5♣" "6♣" "7♣" "8♣" "9♣" "10♣"
        # [49] "J♣" "Q♣" "K♣" "A♣"





        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24685024%2fcreating-a-deck-of-cards-in-r-without-using-while-and-double-for-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          The expand.grid function should be helpful:



          # Define suits, cards, values
          suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
          cards <- c("Ace", "Deuce", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")
          values <- c(0, 2:9, rep(10, 4))
          totalNumOfDecks <- 2

          # Build deck, replicated proper number of times
          deck <- expand.grid(cards=cards, suits=suits)
          deck$value <- values
          deck <- deck[rep(seq(nrow(deck)), totalNumOfDecks),]


          The call to expand.grid computes all pairing of cards and suits. The value variable is created by recycling the value vector for each suit. Finally, rep(seq(nrow(deck))) repeats rows 1-52 the proper number of times to get multiple copies of your deck.






          share|improve this answer

























            up vote
            6
            down vote



            accepted










            The expand.grid function should be helpful:



            # Define suits, cards, values
            suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
            cards <- c("Ace", "Deuce", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")
            values <- c(0, 2:9, rep(10, 4))
            totalNumOfDecks <- 2

            # Build deck, replicated proper number of times
            deck <- expand.grid(cards=cards, suits=suits)
            deck$value <- values
            deck <- deck[rep(seq(nrow(deck)), totalNumOfDecks),]


            The call to expand.grid computes all pairing of cards and suits. The value variable is created by recycling the value vector for each suit. Finally, rep(seq(nrow(deck))) repeats rows 1-52 the proper number of times to get multiple copies of your deck.






            share|improve this answer























              up vote
              6
              down vote



              accepted







              up vote
              6
              down vote



              accepted






              The expand.grid function should be helpful:



              # Define suits, cards, values
              suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
              cards <- c("Ace", "Deuce", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")
              values <- c(0, 2:9, rep(10, 4))
              totalNumOfDecks <- 2

              # Build deck, replicated proper number of times
              deck <- expand.grid(cards=cards, suits=suits)
              deck$value <- values
              deck <- deck[rep(seq(nrow(deck)), totalNumOfDecks),]


              The call to expand.grid computes all pairing of cards and suits. The value variable is created by recycling the value vector for each suit. Finally, rep(seq(nrow(deck))) repeats rows 1-52 the proper number of times to get multiple copies of your deck.






              share|improve this answer












              The expand.grid function should be helpful:



              # Define suits, cards, values
              suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
              cards <- c("Ace", "Deuce", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")
              values <- c(0, 2:9, rep(10, 4))
              totalNumOfDecks <- 2

              # Build deck, replicated proper number of times
              deck <- expand.grid(cards=cards, suits=suits)
              deck$value <- values
              deck <- deck[rep(seq(nrow(deck)), totalNumOfDecks),]


              The call to expand.grid computes all pairing of cards and suits. The value variable is created by recycling the value vector for each suit. Finally, rep(seq(nrow(deck))) repeats rows 1-52 the proper number of times to get multiple copies of your deck.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 10 '14 at 19:55









              josliber

              37k115996




              37k115996
























                  up vote
                  3
                  down vote













                  If I had to simulate a deck (or multiple decks) I would probably prefer to use a single integer vector in conjunction with some reference vectors, and then simply use modular arithmetic and indexing to determine suit, rank and value.



                  #Setup
                  suits <- c('Clubs','Diamonds','Hearts','Spades')
                  card <- c('Ace','Two','Three','Four','Five',
                  'Six','Seven','Eight','Nine','Ten',
                  'Jack','Queen','King')
                  value <- c(0,2:10,rep(10,3))
                  deck <- 0:51

                  #Full deck
                  suits[(deck %/% 13) + 1]
                  card[(deck %% 13) + 1]
                  value[(deck %% 13) + 1]

                  #Some random cards
                  hand <- sample(deck,5)
                  suits[(hand %/% 13) + 1]
                  card[(hand %% 13) + 1]
                  value[(hand %% 13) + 1]





                  share|improve this answer

























                    up vote
                    3
                    down vote













                    If I had to simulate a deck (or multiple decks) I would probably prefer to use a single integer vector in conjunction with some reference vectors, and then simply use modular arithmetic and indexing to determine suit, rank and value.



                    #Setup
                    suits <- c('Clubs','Diamonds','Hearts','Spades')
                    card <- c('Ace','Two','Three','Four','Five',
                    'Six','Seven','Eight','Nine','Ten',
                    'Jack','Queen','King')
                    value <- c(0,2:10,rep(10,3))
                    deck <- 0:51

                    #Full deck
                    suits[(deck %/% 13) + 1]
                    card[(deck %% 13) + 1]
                    value[(deck %% 13) + 1]

                    #Some random cards
                    hand <- sample(deck,5)
                    suits[(hand %/% 13) + 1]
                    card[(hand %% 13) + 1]
                    value[(hand %% 13) + 1]





                    share|improve this answer























                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      If I had to simulate a deck (or multiple decks) I would probably prefer to use a single integer vector in conjunction with some reference vectors, and then simply use modular arithmetic and indexing to determine suit, rank and value.



                      #Setup
                      suits <- c('Clubs','Diamonds','Hearts','Spades')
                      card <- c('Ace','Two','Three','Four','Five',
                      'Six','Seven','Eight','Nine','Ten',
                      'Jack','Queen','King')
                      value <- c(0,2:10,rep(10,3))
                      deck <- 0:51

                      #Full deck
                      suits[(deck %/% 13) + 1]
                      card[(deck %% 13) + 1]
                      value[(deck %% 13) + 1]

                      #Some random cards
                      hand <- sample(deck,5)
                      suits[(hand %/% 13) + 1]
                      card[(hand %% 13) + 1]
                      value[(hand %% 13) + 1]





                      share|improve this answer












                      If I had to simulate a deck (or multiple decks) I would probably prefer to use a single integer vector in conjunction with some reference vectors, and then simply use modular arithmetic and indexing to determine suit, rank and value.



                      #Setup
                      suits <- c('Clubs','Diamonds','Hearts','Spades')
                      card <- c('Ace','Two','Three','Four','Five',
                      'Six','Seven','Eight','Nine','Ten',
                      'Jack','Queen','King')
                      value <- c(0,2:10,rep(10,3))
                      deck <- 0:51

                      #Full deck
                      suits[(deck %/% 13) + 1]
                      card[(deck %% 13) + 1]
                      value[(deck %% 13) + 1]

                      #Some random cards
                      hand <- sample(deck,5)
                      suits[(hand %/% 13) + 1]
                      card[(hand %% 13) + 1]
                      value[(hand %% 13) + 1]






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 11 '14 at 16:44









                      joran

                      132k18316373




                      132k18316373






















                          up vote
                          2
                          down vote













                          buildDeck <- function(noOfDecks=1){
                          suits <- c("Clubs", "Spades", "Diamonds", "Hearts")
                          cards <-c("Ace", 2:10, "Jack", "Queen", "King")
                          Deck <- paste(cards, rep(suits, each=13), sep="-")
                          d<-rep(Deck,noOfDecks) #Build decks
                          shuffledDeck <-sample(d,length(d)) #Shuffle decks
                          shuffledDeck
                          }





                          share|improve this answer

























                            up vote
                            2
                            down vote













                            buildDeck <- function(noOfDecks=1){
                            suits <- c("Clubs", "Spades", "Diamonds", "Hearts")
                            cards <-c("Ace", 2:10, "Jack", "Queen", "King")
                            Deck <- paste(cards, rep(suits, each=13), sep="-")
                            d<-rep(Deck,noOfDecks) #Build decks
                            shuffledDeck <-sample(d,length(d)) #Shuffle decks
                            shuffledDeck
                            }





                            share|improve this answer























                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              buildDeck <- function(noOfDecks=1){
                              suits <- c("Clubs", "Spades", "Diamonds", "Hearts")
                              cards <-c("Ace", 2:10, "Jack", "Queen", "King")
                              Deck <- paste(cards, rep(suits, each=13), sep="-")
                              d<-rep(Deck,noOfDecks) #Build decks
                              shuffledDeck <-sample(d,length(d)) #Shuffle decks
                              shuffledDeck
                              }





                              share|improve this answer












                              buildDeck <- function(noOfDecks=1){
                              suits <- c("Clubs", "Spades", "Diamonds", "Hearts")
                              cards <-c("Ace", 2:10, "Jack", "Queen", "King")
                              Deck <- paste(cards, rep(suits, each=13), sep="-")
                              d<-rep(Deck,noOfDecks) #Build decks
                              shuffledDeck <-sample(d,length(d)) #Shuffle decks
                              shuffledDeck
                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Sep 16 '15 at 4:50









                              Nexislink

                              212




                              212






















                                  up vote
                                  2
                                  down vote













                                  This is related to my recent paste.grid question; see there for some other options, including the straightforward levels(interaction(...)) approach.



                                  Just made a deck myself and it uses the unicode characters for the suits so it looks snazzy; here's what I did:



                                  cards = c(2:10, "J", "Q", "K", "A")
                                  suits = c("♠", "♥", "♦", "♣")
                                  deck <- paste0(rep(cards, length(suits)), #card values
                                  rep(suits, each = length(cards))) #suits
                                  deck
                                  # [1] "2♠" "3♠" "4♠" "5♠" "6♠" "7♠" "8♠" "9♠" "10♠" "J♠" "Q♠" "K♠"
                                  # [13] "A♠" "2♥" "3♥" "4♥" "5♥" "6♥" "7♥" "8♥" "9♥" "10♥" "J♥" "Q♥"
                                  # [25] "K♥" "A♥" "2♦" "3♦" "4♦" "5♦" "6♦" "7♦" "8♦" "9♦" "10♦" "J♦"
                                  # [37] "Q♦" "K♦" "A♦" "2♣" "3♣" "4♣" "5♣" "6♣" "7♣" "8♣" "9♣" "10♣"
                                  # [49] "J♣" "Q♣" "K♣" "A♣"





                                  share|improve this answer



























                                    up vote
                                    2
                                    down vote













                                    This is related to my recent paste.grid question; see there for some other options, including the straightforward levels(interaction(...)) approach.



                                    Just made a deck myself and it uses the unicode characters for the suits so it looks snazzy; here's what I did:



                                    cards = c(2:10, "J", "Q", "K", "A")
                                    suits = c("♠", "♥", "♦", "♣")
                                    deck <- paste0(rep(cards, length(suits)), #card values
                                    rep(suits, each = length(cards))) #suits
                                    deck
                                    # [1] "2♠" "3♠" "4♠" "5♠" "6♠" "7♠" "8♠" "9♠" "10♠" "J♠" "Q♠" "K♠"
                                    # [13] "A♠" "2♥" "3♥" "4♥" "5♥" "6♥" "7♥" "8♥" "9♥" "10♥" "J♥" "Q♥"
                                    # [25] "K♥" "A♥" "2♦" "3♦" "4♦" "5♦" "6♦" "7♦" "8♦" "9♦" "10♦" "J♦"
                                    # [37] "Q♦" "K♦" "A♦" "2♣" "3♣" "4♣" "5♣" "6♣" "7♣" "8♣" "9♣" "10♣"
                                    # [49] "J♣" "Q♣" "K♣" "A♣"





                                    share|improve this answer

























                                      up vote
                                      2
                                      down vote










                                      up vote
                                      2
                                      down vote









                                      This is related to my recent paste.grid question; see there for some other options, including the straightforward levels(interaction(...)) approach.



                                      Just made a deck myself and it uses the unicode characters for the suits so it looks snazzy; here's what I did:



                                      cards = c(2:10, "J", "Q", "K", "A")
                                      suits = c("♠", "♥", "♦", "♣")
                                      deck <- paste0(rep(cards, length(suits)), #card values
                                      rep(suits, each = length(cards))) #suits
                                      deck
                                      # [1] "2♠" "3♠" "4♠" "5♠" "6♠" "7♠" "8♠" "9♠" "10♠" "J♠" "Q♠" "K♠"
                                      # [13] "A♠" "2♥" "3♥" "4♥" "5♥" "6♥" "7♥" "8♥" "9♥" "10♥" "J♥" "Q♥"
                                      # [25] "K♥" "A♥" "2♦" "3♦" "4♦" "5♦" "6♦" "7♦" "8♦" "9♦" "10♦" "J♦"
                                      # [37] "Q♦" "K♦" "A♦" "2♣" "3♣" "4♣" "5♣" "6♣" "7♣" "8♣" "9♣" "10♣"
                                      # [49] "J♣" "Q♣" "K♣" "A♣"





                                      share|improve this answer














                                      This is related to my recent paste.grid question; see there for some other options, including the straightforward levels(interaction(...)) approach.



                                      Just made a deck myself and it uses the unicode characters for the suits so it looks snazzy; here's what I did:



                                      cards = c(2:10, "J", "Q", "K", "A")
                                      suits = c("♠", "♥", "♦", "♣")
                                      deck <- paste0(rep(cards, length(suits)), #card values
                                      rep(suits, each = length(cards))) #suits
                                      deck
                                      # [1] "2♠" "3♠" "4♠" "5♠" "6♠" "7♠" "8♠" "9♠" "10♠" "J♠" "Q♠" "K♠"
                                      # [13] "A♠" "2♥" "3♥" "4♥" "5♥" "6♥" "7♥" "8♥" "9♥" "10♥" "J♥" "Q♥"
                                      # [25] "K♥" "A♥" "2♦" "3♦" "4♦" "5♦" "6♦" "7♦" "8♦" "9♦" "10♦" "J♦"
                                      # [37] "Q♦" "K♦" "A♦" "2♣" "3♣" "4♣" "5♣" "6♣" "7♣" "8♣" "9♣" "10♣"
                                      # [49] "J♣" "Q♣" "K♣" "A♣"






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 11 at 11:10

























                                      answered Apr 28 '16 at 1:43









                                      MichaelChirico

                                      19.5k859109




                                      19.5k859109






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24685024%2fcreating-a-deck-of-cards-in-r-without-using-while-and-double-for-loop%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          鏡平學校

                                          ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                                          Why https connections are so slow when debugging (stepping over) in Java?