When doing data analysis it happens often that we have a set of values and want to obtain various possible combinations of them. For example, taking 5 random samples from a dataset of 20. How many possible 5-sample sets are there and how to obtain all of them? R has a bunch of functions that help with tasks like these:
Answers to the exercises are available here.
If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.
You first throw a coin with 2 possible outcomes: it either lands heads or tails. Then you throw a dice with 6 possible outcomes: 1,2,3,4,5 and 6.
Generate all possible results of your action.
Generate a multiplication table for numbers ranging from 1 to 10.
You have a set of card values:
values <- c("A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K")
and a set of suits representing diamonds, clubs, spades and hearths:
suits <- c("d", "c", "s", "h")
Generate a deck of playing cards. (e.g. King of spades should be represented as Ks).
paste(..., sep="") can be used to combine characters.
Think about a game of poker using a standard deck of cards like the one generated earlier. Starting hand in poker consists of 5 cards and their order does not matter. How many different starting hands are there in total?
You have a set of colors to choose from:
colors <- c("red", "blue", "green", "white", "black", "yellow")
You have to pick 3 colors and you cant’ pick the same color more than once. List all possible combinations.
Using the same set of colors – pick 3 without picking the same more than once, just like in the previous exercise.
List all possible combinations but this time sort each combination alphabetically.
You have the same choices of colors and have to pick 3 but this time you can pick the same color more than once.
List all possible combinations.
You have the same set of colors but this time instead of having to pick 3 you can choose to pick either 1, 2 or 3.
How many different choices can you make?
You have the same set of colors and you can choose to pick either 1, 2 or 3.
Make a list of all possible choices.
There are 3 color palletes: the first one has 4 colors, the second has 6 colors and the third has 8 colors. You have to pick a pallete and then choose up to 5 (1, 2, 3, 4 or 5) colors from the chosen color pallete. How many different possibilities are there?