**R-exercises**, and kindly contributed to R-bloggers)

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?

**leave a comment**for the author, please follow the link and comment on their blog:

**R-exercises**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...