2. R Objects

library(learnr)
knitr::opts_chunk$set(echo = FALSE)

2.1 Object Assignment

  1. Write R code to assign the value 20 to the name num_1.

  1. Which of the following is a valid object name in R?

  2. 2.True

  3. else
  4. I_am_not_a_valid_name
  5. I_am_a_Pretty#_name

  6. Write R code to get the list of all objects in the environment.


2.2 Numeric Vector, Character Vector, & Logical Vector

  1. Write R code to create a numeric vector named vec_1 with values (7, 24, 8, 26), get its length, and find out its type.

  1. Write R code to create a character vector named char_1 with values ("I", "am", "learning", "R!"), get its length, find out its type, and concatenate the vector into a single string with space as the separator.

  1. For the char_1 defined in Q2, find the number of characters in each string, and convert each string to upper case.

  1. Create a length-2 logical vector representing whether vec_1 and char_1 are of character type.

  1. Let class1 <- c(7, TRUE). Which of the following is the class of class1?

  2. numeric

  3. logical
  4. character

  5. Let class2 <- c(7, TRUE, "char"). Which of the following is the class of class2?

  6. numeric

  7. logical
  8. character

2.3 Create Vectors with Patterns

  1. Write R code to create consecutive integers from -10 to 10.

  1. Write R code to create a decreasing sequence from 5 to 1 with increment -0.5.

  1. Write R code to create an equally-spaced sequence from 2 to 6 with length 10.

  1. Write R code using rep() function to create the string

c("sheep","pig", "cat","sheep","pig", "cat","sheep","pig", "cat")


  1. Write R code using rep() function to create the string

c("sheep","sheep","pig","pig","pig","pig","cat","cat","cat")


2.4 Sort, Rank, and Order

Suppose x <- rep(1:3, 1:3).

  1. Write R code to sort x in descending order.

  1. Write R code to get the rank of each element in x. When elements are ties, use the minimum rank.

  1. Write R code to get the order of each element of x.

  1. When sorting characters, which of the following is true?

  2. digits < letters < symbols

  3. symbols < digits < letters
  4. symbols < letters < digits

2.5 Statistical Functions on Vectors

Suppose x <- c(5, 2, 4, 1, 2, 1)

  1. Write R code to reproduce each element of the summary vector summary(x)

  1. Write R code to generate the cumulative sum, cumulative product, cumulative minimum, and cumulative maximum vector of x.

  1. Write R code to generate a vector consisting of the 0.1, 0.2, ..., 0.9 quantiles of x.

  1. Write R code to calculate the sample variance and sample standard deviation of x.

2.6 Comparisons, Vector Subsetting & Change Values



Try the r02pro package in your browser

Any scripts or data that you put into this service are public.

r02pro documentation built on May 31, 2023, 8:09 p.m.