Exercises
Determine the type of each of the following vectors:
c(3.2, 2L, 4.7, TRUE)
c(as.integer(3.2), 2L, 5L, TRUE)
c(as.integer(3.2), 2L, "5L", TRUE)
Using a combination of
c()
,rep()
andseq()
and other operations, find concise one-line programs to produce each of the following vectors:- all numbers from 4 to 307 that are one more than a multiple of 3;
- the numbers 0.01, 0.02, 0.03, …, 0.98, 0.99.
- twelve 2’s, followed by twelve 4’s followed by twelve 6’s, …, followed by twelve 10’s, finishing with twelve 12’s.
- one 1, followed by two 2’s, followed by three 3’s, …, followed by nine 9’s, finishing with ten 10’s.
Using a combination of
c()
,rep()
andseq()
and other operations, find concise one-line programs to produce each of the following vectors:- the numbers 15, 20, 25, …, 145, 150.
- the numbers 1.1, 1.2, 1.3, …, 9.8, 9.9, 10.0.
- ten A’s followed by ten B’s, …, followed by ten Y’s and finishing with ten Z’s.
(Hint: the special vector
LETTERS
will be useful.) - one a, followed by two b’s, followed by three c’s, …, followed by twenty-five y’s, finishing with twenty-six z’s. (Hint: the special vector
letters
will be useful.)
The following three vectors gives the names, heights and ages of five people, and also say whether or not each person likes Toto:
<- c("Akash", "Bee", "Celia", "Devadatta", "Enid") person <- c(23, 21, 22, 25, 63) age <- c(68, 67, 71, 70, 69) height <- c(TRUE, TRUE, FALSE, FALSE, TRUE) likesToto
Use sub-setting with logical vectors to produce vectors of:
- the names of all people over the age of 22;
- the names of all people younger than 24 who are also more than 67 inches tall;
- the names of all people who either don’t like Toto or who are over the age of 30;
- the number of people who are over the age of 22.
Consider the four vectors defined in the previous problem. Use sub-setting with logical vectors to produce vectors of:
- the names of all people who are less than 70 inches tall;
- the names of all people who are between 20 and 30 years of age (not including 20 or 30);
- the names of all people who either like Toto or who are under the age of 50;
- the number of people who are more than 69 inches tall.
Logical vectors are not numerical vectors, so it would seem that you should not be able to sum their elements. But:
sum(likesToto)
results in the number 3! What is happening here is that R coerces the logical vector
likesToto
into a numerical vector of 1’s and 0’s—1 forTRUE
, 0 forFALSE
—and then sums the resulting vector. Notice that this gives us the number of people who like Toto. With this idea in mind, usesum()
along with logical vectors to find:- the number of people younger than 24 who are also more than 67 inches tall;
- the number of people who either don’t like Toto or who are over the age of 30.
Read the previous problem, and then use
sum()
along with logical vectors to find:- the number of people between 65 and 70 inches tall (including 65 and 70);
- the number of people who either don’t like Toto or who are under the age of 25.