Study guide: R Variables Vectors and Random Numbers

Course Outline

  1. R Data Types and Type Conversion
  2. Variable Names and Data Structures
  3. Creating and Transforming Vectors
  4. Factors and Ordered Categories
  5. Random Number Generation in R

1. R Data Types and Type Conversion

★ Must-know

  • R's primary data types are:
    • numeric
    • integer
    • character
    • logical
    • complex

📌 R treats numbers as double-precision numeric values by default, whereas the L suffix explicitly marks an integer, as in 42L.

Further detail

  • Character values are text enclosed in single or double quotes, while logical values are case-sensitive TRUE or FALSE values.

  • Complex values contain real and imaginary parts, with the imaginary part denoted by i, as in 3 + 4i.

  • In R, is.xxxx functions can check a data type and as.xxxx functions can transform a value's type.

2. Variable Names and Data Structures

★ Must-know

📌 An R variable name starts with a letter or a dot not immediately followed by a number; later characters may be letters, numbers, dots, or underscores.

  • Reserved words to avoid include:

    • if
    • else
    • TRUE
    • FALSE
    • NULL
    • for
    • function
  • R data structures include: vectors of elements of the same type, factors for categorical variables, lists of elements of different types, matrices of elements of the same type, data frames whose columns can hold different types

Further detail

📌 R variable names are case-sensitive, so varName, VarName, and VARNAME are different names.

  • Valid R variable names include myVariable, data.frame, .hiddenVar, and var_123, while 1stVar, my-var, and NULL are invalid.

3. Creating and Transforming Vectors

Key Concepts & Definitions

  • Vector: An ordered collection of elements of the same type; c(2, 6, 1, 3, 11) creates a vector with those five values.

Essential Points

  • The t() function transposes a vector or matrix; transposing the vector v <- c(2, 6, 1, 3, 11) displays its values as a row.

  • seq(from=-4, to=5, by=1.5) generates -4.0, -2.5, -1.0, 0.5, 2.0, 3.5, and 5.0.

  • In seq(from=-4, to=5), omitting by makes the step size 1 and generates the integers from -4 through 5.

  • rep(2, 5) repeats the value 2 five times, and rep(3, 4) repeats the value 3 four times.

  • seq(4, 5, length.out=5) generates five equally spaced values: 4.00, 4.25, 4.50, 4.75, and 5.00.

4. Factors and Ordered Categories

Key Concepts & Definitions

  • Factor: A vector used to represent a categorical variable; its labels have underlying numeric codes.
  • Ordered factor: A categorical factor whose levels have a specified order.

Essential Points

  • The factor() function converts a character vector of categories into a factor with category levels.

  • For gender <- c("male", "female", "male", "male", "male", "female"), factor(gender) has levels female and male.

  • ordered(edu, levels=c("h", "u", "p")) orders the levels as h < u < p.

  • The labels argument can display the ordered levels h, u, and p as high school, undergraduate, and postgraduate, respectively.

Memory Hook

Factors show category labels on the surface and numeric codes behind them.

5. Random Number Generation in R

★ Must-know

  • R names distribution functions with dxxx for density or mass, pxxx for cumulative probabilities, qxxx for quantiles, and rxxx for random variate generation.

  • Calling set.seed(number) sets the seed used for subsequent pseudo-random generation; for example, set.seed(23) can precede rnorm(5).

Further detail

  • R distribution families in the handout are beta, binomial (including Bernoulli), Cauchy, chi-squared, exponential, F, gamma, geometric, hypergeometric, log-normal, multinomial, negative binomial, normal, Poisson, Student's t, uniform, and Weibull.

  • rnorm(1) generates one random value from the standard normal distribution, equivalent to rnorm(n=1, mean=0, sd=1).

  • rnorm(n=31, mean=14, sd=2) generates 31 random values from a normal distribution with mean 14 and standard deviation 2.

  • runif(1, min=1, max=6) generates one random value from a uniform distribution between 1 and 6, and round(runif(1, min=1, max=6), 0) rounds such a value to an integer.

  • The random package can generate integers, random sequences, and random strings; randomNumbers(n=100, min=1, max=100, col=5, base=10, check=TRUE) generates 100 numbers between 1 and 100 in five columns.

  • Distribution parameters and characteristics vary by distribution and must be supplied appropriately to its R function.

  • randomSequence(min=1, max=20, col=1, check=TRUE) generates the sequence from 1 to 20 in random order.

  • randomStrings(n=10, len=5, digits=TRUE, upperalpha=TRUE, loweralpha=TRUE, unique=TRUE, check=TRUE) generates 10 unique strings of length 5 using digits and uppercase and lowercase characters.

Test yourself

Take a 11-question practice quiz on R Variables Vectors and Random Numbers, with every answer explained.

1. In R, what type is assigned to each value in `a <- 42` and `b <- 42L`, respectively?

2. Which set lists R’s five primary data types?

Take the quiz →

Study with flashcards

Memorize the key concepts of R Variables Vectors and Random Numbers with 38 flashcards.

What are R's primary data types?

Numeric, integer, character, logical, and complex.

How does R represent numbers by default, and what marks an integer explicitly?

Numbers default to double-precision numeric values; the L suffix marks an integer, as in 42L.

What may begin an R variable name?

A letter or a dot not immediately followed by a number.

See the flashcards →

Related classes

Make your own study guides

Upload your class notes and Revizly writes a study guide, a quiz and flashcards in about a minute.

Study guide maker