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

modeIn the exercises below we cover the basics of R object modes. Understanding mode is important, because mode is a very basic property of any R object. Practically, you’ll use the mode property often to convert e.g. a character vector to a numeric vector or vice versa. Before proceeding, first read section 3.1 of An Introduction to R, and the help page for the mode function.

Answers to the exercises are available here.

Exercise 1
What is the mode of the following objects? First write down the mode, without using R. Then confirm using an approriate R command.
a. c('a', 'b', 'c')
b. 3.32e16
c. 1/3
d. sqrt(-2i)

Exercise 2
What is the mode of the following objects? First, enter the name of the object at the prompt (R will show its contents), and try to infer the mode from what you see. Then enter an R command, such that R will print the mode on the screen.
a. pressure
b. lm
c. rivers

Exercise 3
Consider the following list:
x <- list(LETTERS, TRUE, print(1:10), print, 1:10)
What is the mode of x, and each of its elements? First write down the mode, without using R. Then confirm using the appropriate R commands.

Exercise 4
Show whether the vector x <- 1:100 is of mode numeric (TRUE) or not (FALSE).

Exercise 5
Change the mode of the vector x <- 1:100 to character, with and without using the mode function. Write down the first 5 elements of the vector, after the mode conversion. Check your answer by printing the first 5 characters on the screen.

Exercise 6
Change the mode of the character vector you created in the previous exercise, back to numeric. Again, with and without using the mode function.

Exercise 7
Change the mode of the vector x <- c('1', '2', 'three') to numeric. First write down the new vector x, without using R, then check your answer using R.

Exercise 8
Change the mode of the vector x <- c(TRUE, TRUE, FALSE, TRUE) to numeric. First write down the new vector x, without using R, then check your answer using R.

Exercise 9
Consider the vector x <- c('1', '2', 'three'). What is the mode of y <- x + 1. First write down your answer without using R, then check using R.

Exercise 10
Create a vector y <- c('2', '4', '6') from the vector x <- c('1', '2', '3').

Exercise 11
Try to create some exercises yourself, on the mode topic. This is the best way to really master the subject... Feel free to share as a comment below, so we can all learn from it!

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)