14.5 Objects: Classes and their structure

  • class(): Query object type
  • Objects class that can store homogenous content (see Wickham)
    • Vectors: Integer; Numerical; Logical; Character
    • Matrix: Two dimensions
    • Array: Several matrices
  • …heterogeneous, varying content…
    • List: List of other objects
    • Data frame: “List” of vectors of equal length
  • Attributes, i.e. metadata
    • Objects can have additional attributes, e.g. names
    • Factor
      • Vector with predefined values
      • Stores categorical data
  • Convert objects, e.g. as.numeric()
  • Functions (no data structure!)


14.5.1 Overview of structure of object classes

## [1] "integer"
## [1] 1 2 3 4 5 6 7 8 9
## [1] "numeric"
## [1] 1.3 2.4 3.5
## [1] "logical"
## [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
## [1] "character"
## [1] "a" "b" "c" "d" "f"
## [1] "matrix"
1 2 3
11 12 13
## [1] "array"
## , , 1
## 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    6   11   16   21
## [2,]    2    7   12   17   22
## [3,]    3    8   13   18   23
## [4,]    4    9   14   19   24
## [5,]    5   10   15   20   25
## 
## , , 2
## 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   26   31   36   41   46
## [2,]   27   32   37   42   47
## [3,]   28   33   38   43   48
## [4,]   29   34   39   44   49
## [5,]   30   35   40   45   50
## [1] "list"
## $a
## [1] 4 5 6 7 8
## 
## $b
## [1] 1 2 3
## 
## $c
## [1] "Ger" "FR"  "It"  "SE"
## [1] "data.frame"
Fertility Agriculture Examination
Courtelary 80.2 17.0 15
Delemont 83.1 45.1 6
Franches-Mnt 92.5 39.7 5
Moutier 85.8 36.5 12
Neuveville 76.9 43.5 17
Porrentruy 76.1 35.3 9
Broye 83.8 70.2 16
Glane 92.4 67.8 14
Gruyere 82.4 53.3 12
Sarine 82.9 45.2 16
## [1] "factor"
##  [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
## Levels: 1 2 3 4 5
## [1] "ordered" "factor"
##  [1] A B C A B C A B C A B C A B C A B C A B C A B C A B C A B C
## Levels: A < B < C
## [1] "function"
## function(){x^2}
## [1] "function"
## function (x, ...) 
## UseMethod("mean")
## <bytecode: 0x00000000189a47f0>
## <environment: namespace:base>


14.5.2 Vectors: Numerical, logical and character

  • Classes: "integer", "numeric", "logical", "character"


  • Numerical vectors: Sequence of numbers
  • Logical vectors
    • …take on the values TRUE and FALSE
    • …are often the results of comparisons
    • e.g. c(1,2,3) >= 2 results in FALSE TRUE TRUE
    • …are often used as input for various operations
  • Character vectors
    • …are sequences of letters and/or words
    • e.g. c("Markus", "Matthias", "David", "Till") gives the vector "Markus" "Matthias" "David" "Till"
    • names(object) <- charaktervektor: Name more complex dataclasses, e.g. a dataframe


  • Some functions/operations for vectors
    • c(): “concatenate”, e.g. c(1.2,"Hans",5.0,6.7)
    • length(): Get vector length
    • :: indicates from/to for numbers
    • rep("Peter",2): Repeat 5 two times
    • seq(5,8): Sequence from 5 to 8
    • vector[positions]
      • Access elements by inserting a numeric or logical vector for positions
    • Display
      • e.g. [1] 1.20 3.50 5.00 6.70 8.00 10.00 13.55
      • [1] = Position of the first element displayed in that line in the vector (show it!)
    • Special values
      • Inf and -Inf: Positive and negative infinity
      • NaN: “Not a number”, e.g. calculate 0/0
      • NA: Missing value
    • Combine vectors
      • rbind(): Combine vectors line-by-line
      • cbind(): Combine vectors column-by-column

14.5.2.1 Example: Vectors

# Generate two vectors
x <- 10:19
x
##  [1] 10 11 12 13 14 15 16 17 18 19
y <- c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)
y
##  [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
# seq(0,18,2)
z <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
z
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
# Access vector elements
x[1]              # 1st element of x
## [1] 10
x[1:5]           # First 5 elements of x
## [1] 10 11 12 13 14
x[-(2:6)]      # All elements but not positions 2 to 6
## [1] 10 16 17 18 19
z[4] # ?
## [1] "d"
x[y]
## [1] 10 12 14 16 18
x[x< 16 | x >=12]
##  [1] 10 11 12 13 14 15 16 17 18 19
# Add names to elements of a vector
names(x)
## NULL
names(x) <- z

# or like this
another.vector <- c(a=1,b=2)
x
##  a  b  c  d  e  f  g  h  i  j 
## 10 11 12 13 14 15 16 17 18 19
names(x)
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
# Combine vectors rowwise and columnwise
rbind(x,y)
a b c d e f g h i j
x 10 11 12 13 14 15 16 17 18 19
y 1 0 1 0 1 0 1 0 1 0
cbind(y,z)
y z
TRUE a
FALSE b
TRUE c
FALSE d
TRUE e
FALSE f
TRUE g
FALSE h
TRUE i
FALSE j
# Q: What did R do to the vectors? What class do they have?

# Access with names
names(x)
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
x[c("a","c")]
##  a  c 
## 10 12


14.5.2.2 Exercise: Vectors

  1. Use your usual r-script to save the code for the following exercises.
  2. Create a vector of length 50 that contains the numbers from 1 to 5 repeated for 10 times.
  3. Create a vector x of length 404 in which the numbers 199 to 400 are repeated two times. Generate a new vector y, that contains the elements of x on the positions 53, 78 and 99.
  4. Create a character vector Freunde that contains the names of your three best friends.


14.5.2.3 Solution: Vectors


14.5.2.4 Exercise: Vectors - Combining and subsetting (HOMEWORK)

  1. Create a vector x of length 101, that starts with the number 33 and ends with the number 133.
  2. Extract the elements on the positions 26 to 50 and save them in a new object y. Extract the elements on position 1 to 25 and save them in a new vector z. Join the two vectors both column by column and subsequently line by line (rows!) and save the results in two new objects colyz and colyz. What class do the last two objects that you created posses?
  3. Extract the elements (from x) that are smaller than 57 or greater/equal than 83 and save them in a new object subgroup.


14.5.2.5 Solution: Vectors - Combining and subsetting


14.5.3 Factors and lists

  • Factors
    • Class: "factor"
    • factor(): Create an unordered factor
    • ordered(): Create an ordered factor
    • A way to store data for categorical (nominal/ordinal) variables
    • Vector with attributes
    • levels(): Display categories
    • as.numeric(): Convert factor to numeric vector


  • Lists
    • Class: "list"
    • list(): Create a list
    • Collections of arbitrary objects
    • Have a certain length and list elements can carry names
    • list$switzerland: Access element switzerland of the list list
    • list[2]: Access second element of the list list
    • Sometimes results of estimations are stored as lists
  • We’ll see more object classes later on


14.5.3.1 Example: Factors and lists

# Q: How do i get help for the function factor()?

# Create factor (nominal variable)
f <- factor(rep(1:2,10),levels=c(1,2), labels=c("SPD", "CDU"))

# Q: How do I go about to understand what happens in the function?

f
##  [1] SPD CDU SPD CDU SPD CDU SPD CDU SPD CDU SPD CDU SPD CDU SPD CDU SPD
## [18] CDU SPD CDU
## Levels: SPD CDU
levels(f)
## [1] "SPD" "CDU"
as.numeric(f)
##  [1] 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
# Create factor (ordinal variable)
o <- ordered(rep(1:3,10),levels=c(1,2,3), labels=c("low", "medium", "high"))
o
##  [1] low    medium high   low    medium high   low    medium high   low   
## [11] medium high   low    medium high   low    medium high   low    medium
## [21] high   low    medium high   low    medium high   low    medium high  
## Levels: low < medium < high
as.numeric(o)
##  [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
as.character(o)
##  [1] "low"    "medium" "high"   "low"    "medium" "high"   "low"   
##  [8] "medium" "high"   "low"    "medium" "high"   "low"    "medium"
## [15] "high"   "low"    "medium" "high"   "low"    "medium" "high"  
## [22] "low"    "medium" "high"   "low"    "medium" "high"   "low"   
## [29] "medium" "high"
# Create a list
participants <- list(Teacher= "Rudi", 
                     Women = c("Daniela","Johanna"),
                     Men = c("Simon", "Peter", "usw."))

participants
## $Teacher
## [1] "Rudi"
## 
## $Women
## [1] "Daniela" "Johanna"
## 
## $Men
## [1] "Simon" "Peter" "usw."
length(participants)
## [1] 3
# Access elements or subsets of that list
participants$Teacher
## [1] "Rudi"
participants[1]
## $Teacher
## [1] "Rudi"
participants[[1]]
## [1] "Rudi"
participants[["Teacher"]]
## [1] "Rudi"
# Q: How can I access the list element "Women"?
# Q: How can i access the Johanna who is in the element "Women"?


14.5.3.2 Exercise: Factors and lists (HOMEWORK)

  1. Create a list mylist with two elements. The first element first contains the numbers 5 to 105. The second element second contains the numbers -1 to -50. Create another vector x that contains the 70th value of the first element of mylist and the 30th element of the second element of mylist.
  2. Create a list anotherlist with four elements: A, B, C, D. A contains the number 2. B contains a vector with the number 1 to 10. C contains a character vector with the names “Bernd” “Hans” “Peter”. D contains a vector with the numbers 1 to 100.
  3. Extract the third Element C from the list you just generated and save it in a new object names.
  4. Extract the vector elements 25 to 35 out of the fourth element D of the list and save them in an object names xyz.
  5. Create vector using the following code: test <- rep(1:10,10). Convert test to an ordered factor. Check which categories the factor has and whether it’s ordered.


14.5.3.3 Solution: Objects: Factors and lists