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: `expand.grid`

, `combn`

, `outer`

and `choose`

.

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.

**Exercise 1**

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.

**Exercise 2**

Generate a multiplication table for numbers ranging from 1 to 10.

**Exercise 3**

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).

Note: function `paste(..., sep="")`

can be used to combine characters.

**Exercise 4**

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?

**Exercise 5**

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.

**Exercise 6**

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.

**Exercise 7**

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.

**Exercise 8**

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?

**Exercise 9**

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.

**Exercise 10**

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?

carl sutton says

I have “studying” the R language as time permits for approximately 2 years, taking courses on Coursera, using Data Camp, reading 2 texts, and these exercises are exposing me to functions previously not know to exist. Choose, combn, outer, are handy functions to have at ones disposal. expand.grid I had seen mentioned but have not had a need to use, not yet anyway.

Karolis Koncevicius says

Thank you for the feedback! It’s good to know these exercises are helping you to learn something new. R indeed has quite a few useful but lesser known functions. And I will try to cover more of them in the future.

Mark says

Exercise 9 can be solved by:

sapply(1:3, function (x) { combn(colors, x) })

which can save the typing.