# 1 Atomic Vectors

``````die <- 1:6
is.vector(die)``````
``## [1] TRUE``
``length(die)``
``## [1] 6``

Six types of atomic vectors: doubles, integers, characters, logicals, complex, raw.

``````# creating an integer and a character:
int <- c(1L, 5L)
text <- c("ace", "hearts")

sum(int)``````
``## [1] 6``
``sum(text)``
``## Error in sum(text): invalid 'type' (character) of argument``
``typeof(int)``
``## [1] "integer"``
``typeof(die)``
``## [1] "integer"``
``typeof(text)``
``## [1] "character"``
``````# creating complex vectors:
comp <- c(1 + 1i, 1 + 2i, 1 + 3i)
typeof(comp)``````
``## [1] "complex"``
``````# creating raw vectors:
raw(3)``````
``## [1] 00 00 00``
``typeof(raw)``
``## [1] "closure"``

# 2 Attributes

``attributes(die)``
``## NULL``
``````# "names" is a very common attribute:
names(die)``````
``## NULL``
``````names(die) <- c("one", "two", "three", "four", "five", "six")
names(die)``````
``## [1] "one"   "two"   "three" "four"  "five"  "six"``
``attributes(die)``
``````## \$names
## [1] "one"   "two"   "three" "four"  "five"  "six"``````
``die``
``````##   one   two three  four  five   six
##     1     2     3     4     5     6``````
``````names(die) <- NULL

# "dim" turns a vector into an n-dimensional array:
dim(die) <- c(2,3)
die``````
``````##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6``````
``````# even a hypercube with 1 by 2 by 3 dimensions:
dim(die) <- c(1, 2, 3)
die``````
``````## , , 1
##
##      [,1] [,2]
## [1,]    1    2
##
## , , 2
##
##      [,1] [,2]
## [1,]    3    4
##
## , , 3
##
##      [,1] [,2]
## [1,]    5    6``````

# 3 Matrices

``````m <- matrix(die, nrow = 2)
m``````
``````##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6``````
``````# to fill up the matrix row by row:
m <- matrix(die, nrow = 2, byrow = TRUE)
m``````
``````##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6``````

# 4 Arrays

``````ar <- array(c(11:14, 21:24, 31:34), dim = c(2, 2, 3))
ar``````
``````## , , 1
##
##      [,1] [,2]
## [1,]   11   13
## [2,]   12   14
##
## , , 2
##
##      [,1] [,2]
## [1,]   21   23
## [2,]   22   24
##
## , , 3
##
##      [,1] [,2]
## [1,]   31   33
## [2,]   32   34``````

# 5 Class

Changing the dimensions of my object donâ€™t change the type of the object, but it changes the class of the object.

``````dim(die) <- c(2,3)
typeof(die)``````
``## [1] "integer"``
``class(die)``
``## [1] "matrix"``
``class("Hello")``
``## [1] "character"``
``class(4)``
``## [1] "numeric"``

## 5.1 Dates and Times

``````# data type and class of time:
now <- Sys.time()
now``````
``## [1] "2020-05-22 22:09:42 EDT"``
``typeof(now)``
``## [1] "double"``
``class(now)``
``## [1] "POSIXct" "POSIXt"``
``````# POSIXct counts the time since 12:00AM January 1st 1970. To see this time:
unclass(now)``````
``## [1] 1590199782``
``````# what day it was a million second after 12:00AM Jan 1st 1970?:
mil <- 1000000
mil``````
``## [1] 1e+06``
``````class(mil) <- c("POSIXct", "POSIXt")
mil``````
``## [1] "1970-01-12 08:46:40 EST"``

## 5.2 Factors

Râ€™s way of storing categorical variables.

``````# creating a factor:
gender <- factor(c("male", "female", "male"))
typeof(gender)``````
``## [1] "integer"``
``attributes(gender)``
``````## \$levels
## [1] "female" "male"
##
## \$class
## [1] "factor"``````
``unclass(gender)``
``````## [1] 2 1 2
## attr(,"levels")
## [1] "female" "male"``````
``gender``
``````## [1] male   female male
## Levels: female male``````
``````# factor makes the categorical info coded as numbers

# factor to character:
as.character(gender)``````
``## [1] "male"   "female" "male"``