In 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!

**What's next:**

- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

David says

What real world tasks are you using mode to solve?

r-exercises says

Good question. I think it deserves a somewhat more elaborate answer than I can provide here, so I decided to write an entire post addressing it.

Hadley Wickham says

You should never use mode – it’s designed for compatibility with S+. Using typeof() instead

Han de Vries says

Hi Hadley, thanks for pointing this out!