Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter. Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
BRIEF - Save new types of data, like character strings and logical values - Save a data set as a vector, matrix, array, list, or data frame - Load and save your own data sets with R - Extract individual values from a data set - Change individual values within a data set
die <- c(1, 2, 3, 4, 5, 6) # 1 2 3 4 5 6
die
## [1] 1 2 3 4 5 6
is.vector(die)
## [1] TRUE
five <- 5 # We create an atomic vector that stores 5 (one element).
five
## [1] 5
is.vector(five)
## [1] TRUE
length(five) # gets or sets the length of vectors (including lists) and factors (and other R objects). In simple terms, length returns the length of an atomic vector.
## [1] 1
length(die)
## [1] 6
int <- 1L
text <- "ace"
do_uble <- 30 #64 bits to store
logic <- TRUE
Floating-point errors arise due to each double accuracy to about 16 significant digits. 3. Example #1
sqrt(2)^2 - 2
## [1] 4.440892e-16
comp <- c(1 + 1i, 1 + 2i, 1 + 3i)
comp
## [1] 1+1i 1+2i 1+3i
r_raw <- raw(3)
## 00 00 00
NOTE:The most common attributes to give an atomic vector are names, dimensions (dim), and classes. ________________________________________________________________________________________________________________
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"
names(die) <- c("uno", "dos", "tres", "quatro", "cinco", "seis") # Names do not affect the values
die
## uno dos tres quatro cinco seis
## 1 2 3 4 5 6
names(die) <- NULL #to remove names
die
## [1] 1 2 3 4 5 6
#Creating n dimensional Structures
NOTES: - A vector is a one-dimensional array. - A matrix is a two-dimensional array; therefore is the same thing as a matrix. - Modifying the dim attribute of an atomic vector into either a matrix or an array with more than three dimensions.
dim(die) <- c(2, 3)
die
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
NOTE: - R will always use the first value in dim for the number of rows and the second value for the number of columns.
dim(die) <- c(3, 2)
die
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
NOTE: - by default R fills up each matrix by columns.
#hypercube
dim(die) <- c(1, 2, 3)
class(die)
## [1] "array"
die
## , , 1
##
## [,1] [,2]
## [1,] 1 2
##
## , , 2
##
## [,1] [,2]
## [1,] 3 4
##
## , , 3
##
## [,1] [,2]
## [1,] 5 6
NOTE: - If you’d like more control over how the data is stored, you can use one of R’s helper functions, matrix or array. They do the same thing as changing the dim attribute, but they provide extra arguments to customize the process.
#Matrix Function 8-a.
m <- matrix(die, nrow = 2)
m
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
8-b.
m <- matrix(die, nrow = 2, byrow = TRUE)
m
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
#Array Function 9. The array function creates an n-dimensional array.
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
9-a. NOTE: Changing the dimensions of your object will not change the type of the object, but it will change the object’s class attribute:
dim(die) <- c(2, 3)
typeof(die)
## [1] "double"
class(die)
## [1] "matrix" "array"
9-b. NOTHE: object’s class attribute will not always appear when you run attributes; you may need to specifically search for it with class:
attributes(die)
## $dim
## [1] 2 3
class("Hello")
## [1] "character"
class(5)
## [1] "numeric"
11-a.
now <- Sys.time()
now
## [1] "2022-11-21 21:27:10 EST"
typeof(now)
## [1] "double"
class(now)
## [1] "POSIXct" "POSIXt"
POSIXct is a framework for representing dates and times. Time is represented by the number of seconds that have passed between now and 12:00 AM January 1st 1970 (in the Universal Time Coordinated (UTC) zone).
You can see this number by removing the class attribute of now, or by using the un class function, which does the same thing:
11-b.
unclass(now)
## [1] 1669084031
mil <- 1000000
mil
## [1] 1e+06
class(mil) <- c("POSIXct", "POSIXt")
mil
## [1] "1970-01-12 08:46:40 EST"
#Factors
gender <- factor(c("male", "female", "female", "male"))
typeof(gender)
## [1] "integer"
attributes(gender)
## $levels
## [1] "female" "male"
##
## $class
## [1] "factor"
unclass(gender)
## [1] 2 1 1 2
## attr(,"levels")
## [1] "female" "male"
gender
## [1] male female female male
## Levels: female male
as.character(gender)
## [1] "male" "female" "female" "male"
#Coercion
sum(c(TRUE, TRUE, FALSE, FALSE)) #will become:
## [1] 2
sum(c(1, 1, 0, 0))
## [1] 2
as.character(1) # "1"
## [1] "1"
as.logical(1) # TRUE
## [1] TRUE
as.numeric(FALSE) # 0
## [1] 0
#Lists
list1 <- list(100:130, "R", list(TRUE, FALSE))
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
#Data Frames (DF) 16. DF are 2-dimensional version of a list; most useful storage structure for data analysis, and they provide an ideal way to store an entire deck of cards. You can think of a data frame as R’s equivalent to the Excel spreadsheet because it stores data in a similar format.
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3))
df
16-a. Data frames cannot combine columns of different lengths.
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3),
stringsAsFactors = FALSE)
df
typeof(df)
## [1] "list"
class(df)
## [1] "data.frame"
str(df)
## 'data.frame': 3 obs. of 3 variables:
## $ face : chr "ace" "two" "six"
## $ suit : chr "clubs" "clubs" "clubs"
## $ value: num 1 2 3
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3),
stringsAsFactors = FALSE)
df