Below are the solutions to these exercises on the mode of R objects.

# Exercise 1 mode(c('a', 'b', 'c'))

## [1] "character"

mode(3.32e16)

## [1] "numeric"

mode(1/3)

## [1] "numeric"

mode(sqrt(-2i))

## [1] "complex"

# Exercise 2 mode(pressure)

## [1] "list"

mode(lm)

## [1] "function"

mode(rivers)

## [1] "numeric"

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

## [1] 1 2 3 4 5 6 7 8 9 10

mode(x)

## [1] "list"

mode(x[[1]])

## [1] "character"

mode(x[[2]])

## [1] "logical"

mode(x[[3]])

## [1] "numeric"

mode(x[[4]])

## [1] "function"

mode(x[[5]])

## [1] "numeric"

sapply(x, mode) # alternative to previous 5 statements

## [1] "character" "logical" "numeric" "function" "numeric"

# Exercise 4 x <- 1:100 is.numeric(x)

## [1] TRUE

# Exercise 5 x <- 1:100 mode(x) <- 'character' # using the mode function x <- 1:100 x <- as.character(x) # without using the mode function x[1:5] # to check answer

## [1] "1" "2" "3" "4" "5"

# Exercise 6 mode(x) <- 'numeric' # using the mode function x <- as.numeric(x) # without using the mode function # Exercise 7 x <- c('1', '2', 'three') as.numeric(x)

## Warning: NAs introduced by coercion

## [1] 1 2 NA

# Exercise 8 x <- c(TRUE, TRUE, FALSE, TRUE) as.numeric(x)

## [1] 1 1 0 1

# Exercise 9 # The mode of y does not exist, because y is not defined, # because the '+' operator does not accept a vector of # mode character. # Exercise 10 x <- c('1', '2', '3') y <- as.numeric(x) * 2 mode(y) <- 'character' y

## [1] "2" "4" "6"

**What's next:**

- Explore all our (>1000) 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

PRASHANTH KONAKANCHI says

This website is a boon to new R learners

Onno Dijt says

Hi Prashanth,

Thank you for the compliment! We try the best we can to help.

Have fun!