Create a vector with the optoins of a die with 10 faces
die <- c(1, 2, 3, 4, 5, 6, 7, 8, 9 ,10)
die
[1] 1 2 3 4 5 6 7 8 9 10
With is.vector, we can see if it is a vector or not
is.vector(die)
[1] TRUE
We create an atomic vector that stores the element 5 (one element).
five <- 5
five
[1] 5
We check if object five a vector
is.vector(five)
[1] TRUE
Checking the lenght of both vectos
length(five)
[1] 1
length(die)
[1] 10
There are different type of data that a vector can storage. Here there are 4 examples, which are doubles, integers, characters, and logicals.
int <- 1L
text <- "ace"
do_uble <- 30 #64 bits to store
logic <- TRUE
R is able to calculate with an amazing accuracy to about 16 significant digits.For example, we may expect the result of the expression below to be zero, but it is not, and in most cases, this rounding error will go unnoticed
sqrt(2)^2 - 2
[1] 4.440892e-16
Other types could be
comp <- c(1 + 1i, 1 + 2i, 1 + 3i)
comp
[1] 1+1i 1+2i 1+3i
r_raw <- raw(3)
## 00 00 00
#Attributes Getting the names of die
names(die)
NULL
Since it is null We are going to assign names to the elemements.
names(die) <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")
names(die)
[1] "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten"
Rrechecking the attributes function.
attributes(die)
$names
[1] "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten"
We are going to chack if names do not affect the values.
names(die) <- c("uno", "dos", "tres", "quatro", "cinco", "seis", "siete", "ocho", "nueve", "diez")
die
uno dos tres quatro cinco seis siete ocho nueve diez
1 2 3 4 5 6 7 8 9 10
Here we can also remove names.
names(die) <- NULL
die
[1] 1 2 3 4 5 6 7 8 9 10
#Creating n dimensional Structures
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.
For example you can reorganize our die into a 2 × 5 matrix. Getting 2 rows and 5 columns
dim(die) <- c(2, 5)
die
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
R will always use the first value in dim for the number of rows and the second value for the number of columns. In general, rows always come first in R operations that deal with both rows and columns. In this case we are going to see 5 rows and 2 columns
dim(die) <- c(5, 2)
die
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
Notice how by default R fills up each matrix by columns.
#hypercube
dim(die) <- c(1, 2, 5)
class(die)
[1] "array"
die
, , 1
[,1] [,2]
[1,] 1 2
, , 2
[,1] [,2]
[1,] 3 4
, , 3
[,1] [,2]
[1,] 5 6
, , 4
[,1] [,2]
[1,] 7 8
, , 5
[,1] [,2]
[1,] 9 10
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
m <- matrix(die, nrow = 5)
m
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
In this case we are going to see 5 rows and 2 columns
m <- matrix(die, nrow = 5, byrow = TRUE)
m
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8
[5,] 9 10
In this case we are going to see 2 rows and 5 columns
m <- matrix(die, nrow = 2, byrow = TRUE)
m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
#Array Function The array function creates an n-dimensional array. Creating 3 dimensional matrix with 4 rows and 2 columns on each of them
ar <- array(c(10:17, 20:27, 30:37), dim = c(4, 2, 3))
ar
, , 1
[,1] [,2]
[1,] 10 14
[2,] 11 15
[3,] 12 16
[4,] 13 17
, , 2
[,1] [,2]
[1,] 20 24
[2,] 21 25
[3,] 22 26
[4,] 23 27
, , 3
[,1] [,2]
[1,] 30 34
[2,] 31 35
[3,] 32 36
[4,] 33 37
Notice that 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, 5)
typeof(die)
[1] "double"
class(die)
[1] "matrix" "array"
Note that an object’s class attribute will not always appear when you run attributes; you may need to specifically search for it with class: attributes(die) Getting the dimension of 2 rows and 5 columns
attributes(die)
$dim
[1] 2 5
You can apply class to objects that do not have a class attribute. class will return a value based on the object’s atomic type. Notice that the “class” of a double is “numeric,” an odd deviation, but one I am thankful for. I think that the most important property of a double vector is that it contains numbers, a property that “numeric” makes obvious:
class("Bobcats")
[1] "character"
class(1)
[1] "numeric"
Getting the type and the class of the date of today at the time of the moment created
now <- Sys.time()
now
[1] "2022-11-21 20:29:07 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 and2022-11-21 20:29:07 EST (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:
unclass(now)
[1] 1669080548
R then gives the double vector a class attribute that contains two classes, “POSIXct” and “POSIXt”. This attribute alerts R functions that they are dealing with a POSIXct time, so they can treat it in a special way. For example, R functions will use the POSIXct standard to convert the time into a user-friendly character string before displaying it. You can take advantage of this system by giving the POSIXct class to random R objects. For example, have you ever wondered what day it was a million seconds after 12:00 a.m. Jan. 1, 1970?
mil <- 1000000
mil
[1] 1e+06
class(mil) <- c("POSIXct", "POSIXt")
mil
[1] "1970-01-12 08:46:40 EST"
#Factors Getting the type and the attributes of the vector gender created with females and males
gender <- factor(c("female", "male", "female", "male"))
typeof(gender)
[1] "integer"
attributes(gender)
$levels
[1] "female" "male"
$class
[1] "factor"
Unclassing the vector gender
unclass(gender)
[1] 1 2 1 2
attr(,"levels")
[1] "female" "male"
gender
[1] female male female male
Levels: female male
Formating the vector genter on character
as.character(gender)
[1] "female" "male" "female" "male"
#Coercion Getting a Logical vector and making it True 1 and False 0
sum(c(TRUE, FALSE, FALSE, TRUE))
[1] 2
#will become:
sum(c(1, 0, 0, 1))
[1] 2
Checking how the True and False work in relation to 1 and 0 and the logical analysis
as.character(1)
[1] "1"
## "1"
as.logical(1)
[1] TRUE
## TRUE
as.numeric(FALSE)
[1] 0
## 0
#Lists Lists do not group together individual values; lists group together R objects, they are used as building blocks to create many more spohisticated types of R objects.
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 119 120 121 122 123
[25] 124 125 126 127 128 129 130
[[2]]
[1] "R"
[[3]]
[[3]][[1]]
[1] TRUE
[[3]][[2]]
[1] FALSE
#Data Frames Data frames are the two-dimensional version of a list. They are far and away the 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
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)
Cheking the Type, classa and structure of data frame 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