Introduction to R: Vectors and Operators

Malte Bonart

Vectors

Introduction

  • vector: basic data structure in R
  • one dimensional array of the same data type
  • three data types: numeric, character, logical
  • c() function to combine elements to a vector

Numeric, character, logical

  • character: elements are specified in quotes: "hello"
  • logical: keywords TRUE or FALSE
  • numeric: regular numbers

Numeric, character, logical

c(3+4, 2^3, sqrt(123), exp(2))
[1]  7.000000  8.000000 11.090537  7.389056
c(TRUE, FALSE, FALSE)
[1]  TRUE FALSE FALSE
c("hi", "by")
[1] "hi" "by"

Length of a vector

  • vectors always have a defined length
m <- seq(1, 12)
length(m)
[1] 12

Creation

  • the c() function: c(1, 2, 12, 3)
  • the rep() function: rep("A", times = 4)
  • the seq() function seq(from = 1, to = 4, by = 0.25)

Sequences

  • c() to combine values to a vector of the same data type.
  • rep() to repeat the values of a given vector
  • seq() to generate sequences with fixed length
  • x:y short for seq(x, y, by = 1)

Casting and testing

  • using the functions as.numeric(), is.numeric(), as.character(), is.character(), … one can test and cast an object from one type to another
as.numeric("4")
[1] 4
is.logical(0)
[1] FALSE
is.logical(TRUE)
[1] TRUE

Names of a vector

month <- 1:12
month
 [1]  1  2  3  4  5  6  7  8  9 10 11 12
names(month) <- c("Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez")
month
Jan Feb Mär Apr Mai Jun Jul Aug Sep Okt Nov Dez 
  1   2   3   4   5   6   7   8   9  10  11  12 

Random numbers

  • rnorm() random numbers from the normal distribution
  • rbinom() - random numbers from the binomial distribution
  • runif() - uniform distribution
  • rt() - t distribution
  • sample() - sample values with or without replacement from a vector

Operators

Overview

  • Numeric operators: \(numeric \times numeric = numeric\)
  • Relational operators: \(numeric \times numeric = logical\)
  • Logical operators: \(logical \times logical = logical\)
  • Assignment Operator <-: \(name \times value = object\)

Numeric operators: \(numeric \times numeric = numeric\)

3^2
[1] 9
-3+5
[1] 2
3*7
[1] 21
9/3
[1] 3

Relational operators: \(numeric \times numeric = logical\)

4 < 5
[1] TRUE
3 != 4
[1] TRUE
5 == 2
[1] FALSE
"a" < "b" 
[1] TRUE
"hallo" == 10
[1] FALSE

Logical operators: \(logical \times logical = logical\)

TRUE | FALSE
[1] TRUE
TRUE & FALSE
[1] FALSE
!TRUE
[1] FALSE

Assignment Operator \(name \times value = object\)

y <- 10
x <- 2
x
[1] 2

Vectorization

  • Most operators and many functions work for vectors
  • This is usefull for fast calculations and checking on large vectors
numbers <- c(1, 2, 5, -100, 23, 43)
sqrt(numbers)
Warning in sqrt(numbers): NaNs produced
[1] 1.000000 1.414214 2.236068      NaN 4.795832 6.557439
numbers < 0
[1] FALSE FALSE FALSE  TRUE FALSE FALSE

Missing values

Missing values

  • very common when working with real datasets
  • Keyword: NA

Examples

val <- c(3, 5, 2, NA)
sum(val)
[1] NA
sum(val, na.rm = T)
[1] 10
is.na(val)
[1] FALSE FALSE FALSE  TRUE
sum(is.na(val))
[1] 1