[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In the exercises below we cover the basics of factors. Before proceeding, first read chapter 4 of An Introduction to R, and the help pages for the `cut`, and `table` functions.

Answers to the exercises are available here.

Exercise 1
If `x = c(1, 2, 3, 3, 5, 3, 2, 4, NA)`, what are the levels of `factor(x)`?
a. 1, 2, 3, 4, 5
b. NA
c. 1, 2, 3, 4, 5, NA

Exercise 2
Let `x <- c(11, 22, 47, 47, 11, 47, 11)`. If an R expression `factor(x, levels=c(11, 22, 47), ordered=TRUE)` is executed, what will be the 4th element in the output?
a. 11
b. 22
c. 47

Exercise 3
If `z <- c("p", "a" , "g", "t", "b")`, then which of the following R expressions will replace the third element in `z` with "b".
a. `factor(z) <- "b"`
b. `levels(z) <- "b"`
c. `z <- "b"`

Exercise 4
If `z <- factor(c("p", "q", "p", "r", "q"))` and levels of `z` are "p", "q" ,"r", write an R expression that will change the level "p" to "w" so that z is equal to: "w", "q" , "w", "r" , "q".

Exercise 5
If:
`s1 <- factor(sample(letters, size=5, replace=TRUE))` and
`s2 <- factor(sample(letters, size=5, replace=TRUE))`,
write an R expression that will concatenate s1 and s2 in a single factor with 10 elements.

Exercise 6
Consider the `iris` data set in R. Write an R expression that will ‘cut’ the `Sepal.Length` variable and create the following factor with five levels.
``` (4.3, 5.02] (5.02, 5.74] (5.74, 6.46] (6.46, 7.18] (7.18, 7.9]```
` 32 41 42 24 11`

Exercise 7
Consider again the `iris` data set. Write an R expression that will generate a two-way frequency table with two rows and three colums. The rows should relate to `Sepal.length` (less than 5: TRUE or FALSE) and columns to `Species`, with the following output:
``` setosa versicolor virginica FALSE 30 49 49 TRUE 20 1 1 ```

Exercise 8
Consider the factor `responses <- factor(c("Agree", "Agree", "Strongly Agree", "Disagree", "Agree"))`, with the following output:
```  Agree Agree Strongly Agree Disagree Agree Levels: Agree Disagree Strongly Agree ```
Later it was found that new a level "Strongly Disagree" exists. Write an R expression that will include "strongly disagree" as new level attribute of the factor and returns the following output:
```  Agree Agree Strongly Agree Disagree Agree Levels: Strongly Agree Agree Disagree Strongly Disagree ```

Exercise 9
Let `x <- data.frame(q=c(2, 4, 6), p=c("a", "b", "c"))`. Write an R statement that will replace levels a, b, c with labels "fertiliser1", "fertliser2", "fertiliser3".

Exercise 10
If `x <- factor(c("high", "low", "medium", "high", "high", "low", "medium"))`, write an R expression that will provide unique numeric values for various levels of x with the following output:
``` levels value 1 high 1 2 low 2 3 medium 3 ```