5.1 Atomic Vectors

Make an atomic vector by grouping some values of data together with function c():

#This is to group number 1,2,3,4,5,6 into a vector named die
die <- c(1, 2, 3, 4, 5, 6)

#This is to print vector die
die
## [1] 1 2 3 4 5 6
#This is to test if die is a vector
is.vector(die)
## [1] TRUE

Make an atomic vector with just one value. R saves single values as an atomic vector of length 1:

#This is to make number 5 into a vector named five
five <- 5
five
## [1] 5
#This is to print vector five
is.vector(five)
## [1] TRUE
#This is to return the length of vector five and vector die
length(five)
## [1] 1
length(die)
## [1] 6

Create an integer vector by including a capital L with the input. Create a character vector by surrounding the input in quotation. marks:

#This is to create an integer vector named int with one element which is the integer 1
int <- 1L

#This is to create a character vector that has one element which is the character ace
text <- "ace"

R will recognize the convention of each type of data and create an atomic vector of the corresponding convention. marks:

#This is to create an integer vector named int with integer 1,5
int <- c(1L, 5L)

#This is to create a character vector named text with character ace,hearts
text <- c("ace", "hearts")

R will do math with atomic vectors that contain numbers, but not with atomic vectors that contain character strings: marks:

#This is to sum up all the elements in integer vector int
sum(int)
## [1] 6
#This is to sum up all the elements in the character vector text, which will return an error because character strings can not be added up.
#sum(text)

5.1.1 Doubles


A double vector stores regular numbers. The numbers can be positive or negative, large or small, and have digits to the right of the decimal place or not.

die <- c(1, 2, 3, 4, 5, 6)
die
## [1] 1 2 3 4 5 6
#This is to return the type of vector die, which is double
typeof(die)
## [1] "double"

5.1.2 Integers


Integer vectors store integers, numbers that can be written without a decimal component.

#This is to create an integer vector named int with integers 1,2,4 in it
int <- c(-1L, 2L, 4L)
int
## [1] -1  2  4
#This is to return the type of vector int, which is integer
typeof(int)
## [1] "integer"

each double is accurate to about 16 significant digits. This introduces a little bit of error. In most cases, this rounding error will go unnoticed. However, in some situations, the rounding error can cause surprising results.

#The square root of two cannot be expressed exactly in 16 significant digits. As a result, R has to round the quantity, and the expression resolves to something very close to—but not quite—zero.
sqrt(2)^2 - 2
## [1] 4.440892e-16

5.1.3 Characters


A character vector stores small pieces of text. You can create a character vector in R by typing a character or string of characters surrounded by quotes:

#create a character vector that has 2 elements Hello and World
text <- c("Hello",  "World")
text
## [1] "Hello" "World"
#print type of vector text
typeof(text)
## [1] "character"
#print type of string "Hello"
typeof("Hello")
## [1] "character"

5.1.4 Logicals


Logical vectors store TRUEs and FALSEs, R’s form of Boolean data. Logicals are very helpful for doing things like comparisons:

#is number 3 larger than number 4? TRUE means yes, FALSE means no
3 > 4
## [1] FALSE

Any time you type TRUE or FALSE in capital letters (without quotation marks), R will treat your input as logical data. R also assumes that T and F are shorthand for TRUE and FALSE, unless they are defined elsewhere

#create a logical vector named logic that has three logicals: TRUE, FALSE TRUE
logic <- c(TRUE, FALSE, TRUE)
logic
## [1]  TRUE FALSE  TRUE
#print type of vector logic
typeof(logic)
## [1] "logical"
#print type of logical F
typeof(F)
## [1] "logical"

5.1.5 Complex and Raw


Complex vectors store complex numbers. To create a complex vector, add an imaginary term to a number with i

#create a complex vector named comp with 3 elements: 1+1i, 1+2i, 1+3i
comp <- c(1 + 1i, 1 + 2i, 1 + 3i)
comp
## [1] 1+1i 1+2i 1+3i
#print type of vector comp
typeof(comp)
## [1] "complex"

Raw vectors store raw bytes of data. Making raw vectors gets complicated, but you can make an empty raw vector of length n with raw(n).

#create a empty raw vector that has 3 elements
raw(3)
## [1] 00 00 00
#print type of the raw vector that was just created
typeof(raw(3))
## [1] "raw"

5.3 Matrices


Matrices store values in a two-dimensional array, just like a matrix from linear algebra. To create one, first give matrix an atomic vector to reorganize into a matrix. Then, define how many rows should be in the matrix by setting the nrow argument to a number. matrix will organize your vector of values into a matrix with the specified number of rows. Alternatively, you can set the ncol argument, which tells R how many columns to include in the matrix:

#reorganize vector die into matrix m that has 2 rows and 3 columns
m <- matrix(die, nrow = 2)

#print matrix m
m
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6

matrix will fill up the matrix column by column by default, but you can fill the matrix row by row if you include the argument byrow = TRUE:

#reorganize vector die into matrix m that has 2 rows and 3 columns, filling the matrix row by row.
m <- matrix(die, nrow = 2, byrow = TRUE)

#print matrix m
m
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6

5.4 Arrays


The array function creates an n-dimensional array. For example, you could use array to sort values into a cube of three dimensions or a hypercube in 4, 5, or n dimensions. array is not as customizeable as matrix and basically does the same thing as setting the dim attribute. To use array, provide an atomic vector as the first argument, and a vector of dimensions as the second argument, now called dim:

#create an array that has elements 11,12,13,14,21,22,23,24,31,32,33,34 in it, with dimensions 2,2,3
ar <- array(c(11:14, 21:24, 31:34), dim = c(2, 2, 3))

#print array ar
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

Create the following matrix, which stores the name and suit of every card in a royal flush.

#create a vector with 10 values. 
hand1 <- c("ace", "king", "queen", "jack", "ten", "spades", "spades", 
  "spades", "spades", "spades")

#turn vector hand1 into a matrix with any of the following commands:
m2 <- matrix(hand1, nrow = 5)
m3 <- matrix(hand1, ncol = 2)
dim(hand1) <- c(5, 2)

m2
##      [,1]    [,2]    
## [1,] "ace"   "spades"
## [2,] "king"  "spades"
## [3,] "queen" "spades"
## [4,] "jack"  "spades"
## [5,] "ten"   "spades"
m3
##      [,1]    [,2]    
## [1,] "ace"   "spades"
## [2,] "king"  "spades"
## [3,] "queen" "spades"
## [4,] "jack"  "spades"
## [5,] "ten"   "spades"
hand1
##      [,1]    [,2]    
## [1,] "ace"   "spades"
## [2,] "king"  "spades"
## [3,] "queen" "spades"
## [4,] "jack"  "spades"
## [5,] "ten"   "spades"

5.7 Lists


list creates a list the same way c creates a vector. Separate each element in the list with a comma:

#create a list that contains a numeric vector of length 31 in its first element, a character vector of length 1 in its second element, and a new list of length 2 in its third element. 
list1 <- list(100:130, "R", list(TRUE, FALSE))

#print list list1
list1
## [[1]]
##  [1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
## [20] 119 120 121 122 123 124 125 126 127 128 129 130
## 
## [[2]]
## [1] "R"
## 
## [[3]]
## [[3]][[1]]
## [1] TRUE
## 
## [[3]][[2]]
## [1] FALSE

list creates a list the same way c creates a vector. Separate each element in the list with a comma:

# In the following example, the first element of the list is a character vector (of length 1). The second element is also a character vector, and the third element is a numeric vector:
card <- list("ace", "hearts", 1)

#print list card
card
## [[1]]
## [1] "ace"
## 
## [[2]]
## [1] "hearts"
## 
## [[3]]
## [1] 1

5.8 Data Frames


Give data.frame any number of vectors, each separated with a comma. Each vector should be set equal to a name that describes the vector. data.frame will turn each vector into a column of the new data frame:

#create a data frame that has 3 columns, each with 3 elements.
df <- data.frame(face = c("ace", "two", "six"),  suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3), stringsAsFactors = FALSE)

#print data frame df
df
##   face  suit value
## 1  ace clubs     1
## 2  two clubs     2
## 3  six clubs     3

each data frame is a list with class data.frame.

#print type of data frame df
typeof(df)
## [1] "list"
#print class of data frame df
class(df)
## [1] "data.frame"
#See what types of objects are grouped together by a list (or data frame) with the str function:
str(df)
## 'data.frame':    3 obs. of  3 variables:
##  $ face : chr  "ace" "two" "six"
##  $ suit : chr  "clubs" "clubs" "clubs"
##  $ value: num  1 2 3

each data frame is a list with class data.frame.

#To create a data frame of an entire deck of cards, you need to write three vectors, each with 52 elements:
deck <- data.frame(
  face = c("king", "queen", "jack", "ten", "nine", "eight", "seven", "six",
    "five", "four", "three", "two", "ace", "king", "queen", "jack", "ten", 
    "nine", "eight", "seven", "six", "five", "four", "three", "two", "ace", 
    "king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five", 
    "four", "three", "two", "ace", "king", "queen", "jack", "ten", "nine", 
    "eight", "seven", "six", "five", "four", "three", "two", "ace"),  
  suit = c("spades", "spades", "spades", "spades", "spades", "spades", 
    "spades", "spades", "spades", "spades", "spades", "spades", "spades", 
    "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", 
    "clubs", "clubs", "clubs", "clubs", "clubs", "diamonds", "diamonds", 
    "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", 
    "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "hearts", 
    "hearts", "hearts", "hearts", "hearts", "hearts", "hearts", "hearts", 
    "hearts", "hearts", "hearts", "hearts", "hearts"), 
  value = c(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 13, 12, 11, 10, 9, 8, 
    7, 6, 5, 4, 3, 2, 1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 13, 12, 11, 
    10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
)

#print data frame deck
deck
##     face     suit value
## 1   king   spades    13
## 2  queen   spades    12
## 3   jack   spades    11
## 4    ten   spades    10
## 5   nine   spades     9
## 6  eight   spades     8
## 7  seven   spades     7
## 8    six   spades     6
## 9   five   spades     5
## 10  four   spades     4
## 11 three   spades     3
## 12   two   spades     2
## 13   ace   spades     1
## 14  king    clubs    13
## 15 queen    clubs    12
## 16  jack    clubs    11
## 17   ten    clubs    10
## 18  nine    clubs     9
## 19 eight    clubs     8
## 20 seven    clubs     7
## 21   six    clubs     6
## 22  five    clubs     5
## 23  four    clubs     4
## 24 three    clubs     3
## 25   two    clubs     2
## 26   ace    clubs     1
## 27  king diamonds    13
## 28 queen diamonds    12
## 29  jack diamonds    11
## 30   ten diamonds    10
## 31  nine diamonds     9
## 32 eight diamonds     8
## 33 seven diamonds     7
## 34   six diamonds     6
## 35  five diamonds     5
## 36  four diamonds     4
## 37 three diamonds     3
## 38   two diamonds     2
## 39   ace diamonds     1
## 40  king   hearts    13
## 41 queen   hearts    12
## 42  jack   hearts    11
## 43   ten   hearts    10
## 44  nine   hearts     9
## 45 eight   hearts     8
## 46 seven   hearts     7
## 47   six   hearts     6
## 48  five   hearts     5
## 49  four   hearts     4
## 50 three   hearts     3
## 51   two   hearts     2
## 52   ace   hearts     1

You should avoid typing large data sets in by hand whenever possible. Typing invites typos and errors, not to mention RSI. It is always better to acquire large data sets as a computer file. You can then ask R to read the file and store the contents as an object.