A vector is a simple data structure in R. You will use it frequently, often as a building block of more complex data structures and operations on those structures. Before proceeding, please follow our short tutorial and review Chapter 2 of An Introduction to R. First, write down your answer, without using R and without looking at the answer options. Then, match the answer you wrote down with one of the choices given. Finally, check your answer using R.

Solutions are available here.

**Exercise 1**

Consider a vector:

x <- c(4,6,5,7,10,9,4,15)

What is the value of:

c(4,6,5,7,10,9,4,15) < 7

a. `TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE`

b. `TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE`

c. `FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE`

d. `TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE`

e. `TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE`

**Exercise 2**

Consider two vectors:

p <- c (3, 5, 6, 8)

and

q <- c (3, 3, 3)

What is the value of:

p+q

a. `6, 8, 6, 8`

b. `6, 8, 0, 0`

c. `6, 8, NA, NA`

d. `3, 5, 6, 8`

` Warning message: In p+q : longer object length is not a multiple of shorter object length`

e. `6, 8, 9, 11`

**Exercise 3**

If:

Age <- c(22, 25, 18, 20) Name <- c("James", "Mathew", "Olivia", "Stella") Gender <- c("M", "M", "F", "F")

then what is the R-code for getting the following output;

## Age Name Gender ## 1 22 James M ## 2 25 Mathew M

a.

DataFrame = data.frame(c(Age), c(Name), c(Gender)) subset(DataFrame, Gender == "M")

b.

DataFrame = data.frame(c(Age),c(Name),c(Gender)) subset(Gender=="M"), eval=FALSE

c.

DataFrame = data.frame(Age,Name,Gender) subset(DataFrame,Gender=="M")

d.

DataFrame = data.frame(c(Age,Name,Gender)) subset(DataFrame,Gender=="M")

**Exercise 4**

If

z <- 0:9

then what is the output from the following R-statements:

digits <- as.character(z) as.integer(digits)

a. `Error in subset. object 'z' not found`

b. `0, 1, 2, 3, 4, 5, 6, 7, 8, 9`

c. `"NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA"`

d. `"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"`

e. `0, 0, 0, 0, 0, 0, 0, 0, 0`

**Exercise 5**

Consider the vector:

x <- c(1,2,3,4)

What is the value of k for:

(x+2)[(!is.na(x)) & x > 0] -> k

a. `1, 2, 3, 4`

b. `1, 4, 9, 16`

c. `Error: object 'k' not found`

d. `3, 4, 5, 6`

e. `numeric(0)`

**Exercise 6**

Consider the AirPassenger data set

data(AirPassengers)

Which statement will produce the following output?

## [1] 112 118 132 129 121 135 148 148 136 119 104 118

a. `AirPassengers[time(AirPassengers) >= 1949 & time(AirPassengers) < 1950, 12]`

b. `AirPassengers[AirPassengers >= 1949 & AirPassengers < 1950]`

c. `AirPassengers[time(AirPassengers) >= 1949 & time(AirPassengers) < 1950]`

d. `AirPassengers[AirPassengers >= 1949 & AirPassengers < 1950, 12]`

e. `c[[1]]`

**Exercise 7**

If

x <- c(2, 4, 6, 8)

and

y <- c(TRUE, TRUE, FALSE, TRUE)

What is the value of:

sum(x[y])

a. `20`

b. `8`

c. `14`

d. `NA`

**Exercise 8**

Consider the vector:

x <- c(34, 56, 55, 87, NA, 4, 77, NA, 21, NA, 39)

Which R-statement will count the number of `NA`

values in x?

a. `count(is.na(X))`

b. `length(is.na(x))`

c. `sum(is.na(x))`

d. `count(!is.na(x))`

e. `sum(!is.na(x))`

**Want to practice vectors a bit more? We have more exercise sets on this topic here.**

**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

Iegor says

First of all, thank you for this project! I have coupe of remarks:

1. In fact, there are two correct answers (b and e) in ex. 1 (they are identical).

2. In ex. 3 the style of the code in c and d is a bit strange: no spaces after commas inside subset function 🙂

3. In ex. it might be interesting to coerce to numeric: as.numeric(digits) and include the 1.0 … 9.0 to the answers list.

Apart from that, it is really nice 🙂

r-exercises says

Hi Iegor,

Good catch! Thanks for your nice words!

Basilio Lyma-Young says

In Exercise 7, do you mean sum(x[y]) or is it meant as a tricky question for those paying attention to details 😉

Ashendar says

As far as i can tell it is a typo and should be sum(x[y]).

Kapil says

Do you get reason for sum(x[y]) is 14.

This is new for me .

Onno Dijt says

Hi Kapil,

When we use the sum command on a numeric vector(x), in combination with a vector of TRUE/FALSE (y), it will only sum those values of x who have a corresponding TRUE in the vector y.

In this case thus, we sum only 2,4 and 8, making a total of 14 as those have a corresponding TRUE in vector y.

If we didn’t index with vector y, the sum of x would be 20, but the 6 is not counted because it has a FALSE in vector y.

Kind regards,

Onno

charles says

hi, how can we get the data set in exercise 6(Airpassengers)

Onno Dijt says

Hi Charles,

It appears in your code there is a typo, specifically we load the dataset in this way:

data(AirPassengers)

Where as you typed:

data(Airpassengers)

Have fun!

Andrej Panjkov says

In Exercise 7

a[b] should be x[y] perhaps?

Also, the preamble suggests a review of Chapter 2 from the Intro book, but the data.frame and time.series objects are not covered there.

Onno Dijt says

Hi Andrej,

Yes it appears there is a typo in exercise 7, I will update the post thank you for spotting this!

You make a fair point about the reference to the chapter of the book, we will review if its worth adding that this to the preamble.

viki says

i want to know the answers of exercise

Onno Dijt says

Hi Viki,

In all our exercise sets there is a link to the set of solutions above the header:

Exercise 1,

However to help you more directly, the solutions are available here:

http://r-exercises.com/2015/10/16/vector-exercises-solutions/

Have fun!

snkohari says

Thanks a tonne for maintaining this website! It’s really helpful to a lot of us. {thumbs up}

Antonio Rodríguez says

Hi,

In Exercise 2, I think answer ‘e’ is incomplete, if I sum those two vectors I get:

[1] 6 8 9 11

Warning message:

In p + q : longer object length is not a multiple of shorter object length

swaroop says

help on question 6

i am getting the value as numeric(0)

i don’t understand whats the error.

and the data(AirPassengers) shows

please help me understand the issue.. i am very new to R. i loaded the datasets package.

hemen boro says

first of all thanks for great exercises.

for question number 8

Consider the vector:

x <- c(34, 56, 55, 87, NA, 4, 77, NA, 21, NA, 39)

Which R-statement will count the number of NA values in x?

ans: sum(is.na(x))

but we can also find this ans by using summary function and it will give results like:

Min. 1st Qu. Median Mean 3rd Qu. Max.

4.00 30.75 47.00 46.62 61.25 87.00

NA's

3

Mohit verma says

AirPassenger data set

Mohit verma says

From where we can download AirPassenger data set?

Melanie says

This data set is inbuilt in to R. If you type AirPassengers in to the editor (without any typos) it should automatically appear. You can hit Ctrl Enter and it should bring up the data set for you to view.