11.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

11.2.1 Example: Vectors

# Generate two vectors
x <- 10:19
x
y <- c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)
y
# seq(0,18,2)
z <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
z





# Access vector elements
x[1]              # 1st element of x
x[1:5]           # First 5 elements of x
x[-(2:6)]      # All elements but not positions 2 to 6
z[4] # ?
x[y]
x[x< 16 | x >=12]


# Add names to elements of a vector
names(x)
names(x) <- z

# or like this
another.vector <- c(a=1,b=2)
x
names(x)

# Combine vectors rowwise and columnwise
rbind(x,y)
cbind(y,z)

# Q: What did R do to the vectors? What class do they have?

# Access with names
names(x)
x[c("a","c")]


11.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.


11.2.3 Solution: Vectors


11.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.


11.2.5 Solution: Vectors - Combining and subsetting