2 L02: Basics I

2.1 Basics of R: Data Structures and Subsetting

2.2 Goals

  • Getting to know main data structures in R

2.4 Class

NB: Original worksheets prepared by Lincoln Mullen, GMU (https://dh-r.lincolnmullen.com/worksheets.html)

2.5 Topics: Data Structures & Types

  • Data structures
    • Vectors
    • Matrices
    • Data frames
    • Lists
  • Subsetting operations

2.5.1 Additional notes

  • str() compactly dysplays information about an R object
  • typeof() determines the internal type or storage mode of an R object
  • class() tells you the data structure of an R object:
    • list
    • data.frame
    • matrix
    • vector
      • numeric
        • typeof() > double — a double-precision vector (floats; default numberic vector type)
        • typeof() > integer — a vector of integers
      • character
        • typeof() > character — a vector of strings/characters
  • Checking class/type & Conversion
    • create test <- c(1,2,3,4,5)
      • is.___ tests whether a variable of a ___ class.
        • or type: check the typeof() our test vector; then try test.ch <- as.character(test) and check the type again; can you convert it back into its initial type?).
      • is.___ converts into a ___ class.
      • (___ is either: vector, matrix, data.frame, list)
    • Now try the following commands and check the type and class of the new objects:
      • test.matrix <- as.matrix(test)
      • test.df <- as.data.frame(test)
      • test.list <- as.list(test)
      • test.vector <- as.vector(test.df$test)

2.6 Reference materials

2.7 Homework

  • Finish your worksheet and submit your HW as described below.

2.8 Submitting homework

  • Homework assignment must be submitted by the beginning of the next class;
  • Email your homework to the instructor as attachments.
    • In the subject of your email, please, add the following: 070184-LXX-HW-YourLastName-YourMatriculationNumber, where LXX is the numnber of the lesson for which you submit homework; YourLastName is your last name; and YourMatriculationNumber is your matriculation number.