Factor exercises

[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 . 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 , then which of the following R expressions will replace the third element in z with “b”.
a. factor(z[3])
b. levels(z[3])
c. z[3]

Exercise 4
If z 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 and
s2 ,
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 , with the following output:

[1] 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:

[1] Agree Agree Strongly Agree Disagree Agree

Levels: Strongly Agree Agree Disagree Strongly Disagree

Exercise 9
Let x . Write an R statement that will replace levels a, b, c with labels “fertiliser1”, “fertliser2”, “fertiliser3”.

Exercise 10
If x , 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

To 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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)