Exercises

  1. Determine the type of each of the following vectors:

    1. c(3.2, 2L, 4.7, TRUE)
    2. c(as.integer(3.2), 2L, 5L, TRUE)
    3. c(as.integer(3.2), 2L, "5L", TRUE)
  2. Using a combination of c(), rep() and seq() and other operations, find concise one-line programs to produce each of the following vectors:

    1. all numbers from 4 to 307 that are one more than a multiple of 3;
    2. the numbers 0.01, 0.02, 0.03, …, 0.98, 0.99.
    3. twelve 2’s, followed by twelve 4’s followed by twelve 6’s, …, followed by twelve 10’s, finishing with twelve 12’s.
    4. one 1, followed by two 2’s, followed by three 3’s, …, followed by nine 9’s, finishing with ten 10’s.
  3. Using a combination of c(), rep() and seq() and other operations, find concise one-line programs to produce each of the following vectors:

    1. the numbers 15, 20, 25, …, 145, 150.
    2. the numbers 1.1, 1.2, 1.3, …, 9.8, 9.9, 10.0.
    3. 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.)
    4. 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.)
  4. The following three vectors gives the names, heights and ages of five people, and also say whether or not each person likes Toto:

    person <- c("Akash", "Bee", "Celia", "Devadatta", "Enid")
    age <- c(23, 21, 22, 25, 63)
    height <- c(68, 67, 71, 70, 69)
    likesToto <- c(TRUE, TRUE, FALSE, FALSE, TRUE)

    Use sub-setting with logical vectors to produce vectors of:

    1. the names of all people over the age of 22;
    2. the names of all people younger than 24 who are also more than 67 inches tall;
    3. the names of all people who either don’t like Toto or who are over the age of 30;
    4. the number of people who are over the age of 22.
  5. Consider the four vectors defined in the previous problem. Use sub-setting with logical vectors to produce vectors of:

    1. the names of all people who are less than 70 inches tall;
    2. the names of all people who are between 20 and 30 years of age (not including 20 or 30);
    3. the names of all people who either like Toto or who are under the age of 50;
    4. the number of people who are more than 69 inches tall.
  6. 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 2! What is happening here is that R coerces the logical vector likesToto into a numerical vector of 1’s and 0’s—1 for TRUE, 0 for FALSE—and then sums the resulting vector. Notice that this gives us the number of people who like Toto. With this idea in mind, use sum() along with logical vectors to find:

    1. the number of people younger than 24 who are also more than 67 inches tall;
    2. the number of people who either don’t like Toto or who are over the age of 30.
  7. Read the previous problem, and then use sum() along with logical vectors to find:

    1. the number of people between 65 and 70 inches tall (including 65 and 70);
    2. the number of people who either don’t like Toto or who are under the age of 25.