# Chapter 1 Data in R

Reference : R for Beginners.

## 1.1 Data Type

R has 6 data types.

• logical
• integer
• numeric (double)
• complex
• character
• (null)

R will save data when you assign it to the variable. With `n <- 15`, `n` is set to `numeric`,

``````n <- 15
class(n)``````
``## [1] "numeric"``

We can use various operations(methods) with `numeric` type variables. For example,

``````# addition
n + 4``````
``## [1] 19``
``````# multiplication
n * 3``````
``## [1] 45``
``````# power
n ^ 2``````
``## [1] 225``
``````# division
n / 7``````
``## [1] 2.142857``
``````# Inf, -Inf, NaN(Not a Number)
n / 0        ``````
``## [1] Inf``

Note also that we can get access to stored data `15` by the variable name `n`. In addition, we can coerce the object `n` to be a `integer` type variable.

``````n <- as.integer(n)
class(n)``````
``## [1] "integer"``

By enclosing a string with double quotes, we create a `character` type object,

``````ch <- "Hello"
class(ch)``````
``## [1] "character"``
``````ch <- "Double quotes \" \" in double quotes"
cat(ch)``````
``## Double quotes " " in double quotes``

To define a `logical` object, we type

``````bool <- TRUE
class(bool)``````
``## [1] "logical"``

R can interpret 0 as `FALSE`, other numbers as `TRUE`

``````m <- as.logical(1 + 5i)
m``````
``## [1] TRUE``
``class(m)``
``## [1] "logical"``
``````m <- as.logical(0)
m``````
``## [1] FALSE``
``````bool2 <- bool + bool  # implicit conversion
bool2``````
``## [1] 2``
``class(bool2)``
``## [1] "integer"``

Google NaN NULL NA in R or type `?NA ?NaN ?NULL`.

## 1.2 Data Structure

### 1.2.1 Vector

• Define Vectors
``````x <- vector("numeric", length = 10) # zero vector numeric(10)
x``````
``##  [1] 0 0 0 0 0 0 0 0 0 0``
``class(x)``
``## [1] "numeric"``
``length(x)``
``## [1] 10``
``````y <- 1 : 10 # by = 1
y``````
``##  [1]  1  2  3  4  5  6  7  8  9 10``
``````z <- seq(from = 1, to = 9, by = 2)
z``````
``## [1] 1 3 5 7 9``
``````w <- c(1, 3, 4, 2)  # concatenation
w``````
``## [1] 1 3 4 2``
• Manipulating Vectors
``x + y    # vector addition``
``##  [1]  1  2  3  4  5  6  7  8  9 10``
``z * 2    # scalar multiplication``
``## [1]  2  6 10 14 18``
``y + 1    # elementwise addition``
``##  [1]  2  3  4  5  6  7  8  9 10 11``
``y ^ 2    # elementwise exponentiation``
``##  [1]   1   4   9  16  25  36  49  64  81 100``
``````w <- c(z, x)
w``````
``##  [1] 1 3 5 7 9 0 0 0 0 0 0 0 0 0 0``
``w[1]     # index in R starts from 1``
``## [1] 1``
``w[c(1, 4, 2)]``
``## [1] 1 7 3``
``w[w > 5]``
``## [1] 7 9``
``mean(w) ``
``## [1] 1.666667``
• Note : Vector can contain only one type
``````v <- c(1, "2")
# v[1] + 1``````

### 1.2.2 Lists

Unlike vectors, lists can contain several data types.

``````# list with names
ls_1 <- list(a = 13, b = "char", c = c(1,4,3))
ls_1\$a         # access data using name``````
``## [1] 13``
``ls_1[[1]]      # access data using index``
``## [1] 13``
``class(ls_1[1]) ``
``## [1] "list"``
``````ls_2 <- list(1, 3, "2")           # list without names
ls_2``````
``````## [[1]]
## [1] 1
##
## [[2]]
## [1] 3
##
## [[3]]
## [1] "2"``````
``````names(ls_2) <- c("A", "B", "C")   # assign names to list
ls_2``````
``````## \$A
## [1] 1
##
## \$B
## [1] 3
##
## \$C
## [1] "2"``````
``str(ls_2)                 # str() : compactly display the "structure" of an object``
``````## List of 3
##  \$ A: num 1
##  \$ B: num 3
##  \$ C: chr "2"``````
``rm(list = ls())           # input of the argument 'list' is not a list``

### 1.2.3 Matrix

• Define Matrix
``````mat <- matrix(nrow = 2, ncol = 4)
class(mat)``````
``## [1] "matrix"``
``dim(mat)``
``## [1] 2 4``
``str(mat)``
``##  logi [1:2, 1:4] NA NA NA NA NA NA ...``
``attributes(mat)``
``````## \$dim
## [1] 2 4``````
• Assign elements of a matrix
``````A <- matrix(1:16, nrow = 2, ncol = 8)
A  # columnwise construction``````
``````##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    1    3    5    7    9   11   13   15
## [2,]    2    4    6    8   10   12   14   16``````
``````dim(A) <- c(4, 4) # reshape matrix
A``````
``````##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]    4    8   12   16``````
``````B <- matrix(seq(15, -30, -3), 4, 4) # argument names can be dropped
B``````
``````##      [,1] [,2] [,3] [,4]
## [1,]   15    3   -9  -21
## [2,]   12    0  -12  -24
## [3,]    9   -3  -15  -27
## [4,]    6   -6  -18  -30``````
``diag(4)    # identity matrix of dimension 4 * 4``
``````##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1``````
``diag(c(1, 2, 3 ,4))  # diagonal matrix with an input vector``
``````##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    2    0    0
## [3,]    0    0    3    0
## [4,]    0    0    0    4``````
``diag(B)    # diagonal part of matrix B``
``## [1]  15   0 -15 -30``
• Elementwise operations
``````A > B
A == B
A != B
A + B
A * B     # This is not a matrix multiplcation
A ^ 2``````
• Examples of built-in matrix operations
``````A %*% B         # Matrix multiplication
A %o% B         # Outer product. AB'
crossprod(A,B)  # A'B
crossprod(A)      # A'A
t(A)              # Transpose
# solve(A, b)       # solution of b = Ax by x = inv(A)b
# solve(A)      # inv(A)

# install.packages("MASS")
library(MASS)
MASS::ginv(A)   # Moore - Penrose inverse
x <- eigen(A)     # eigenvalues
x\$values
x\$vectors
cbind(A,B)      # bind matrices by columns
rbind(A,B)      # bind matrices by rows
rowMeans(A)``````

Google Array, Factor, DataFrame in R or type `?array ?factor ?data.frame`.