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!

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!

Ruse says

How about this?

# Form a vector y that has the single element ‘123’ by using the paste() function (along with any other functions you might need) and the vector x <- c(1, 2, 3)

Luis says

Consider the following list:

x <- list(LETTERS, TRUE, print(1:10), print, 1:10)

Write an R expression that will return the positions of numeric elements.