#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
#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
#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"
#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")
#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)
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"
#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"
#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
#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"
#is number 3 larger than number 4? TRUE means yes, FALSE means no
3 > 4
## [1] FALSE
#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"
#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"
#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"
#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
#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 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"
#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
# 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
#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
#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
#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