## putting everything in codeblocks otherwise markdown does not show output##
die <- c(1, 2, 3, 4, 5, 6) ## Creating atomic vector named die ##
die ## Showing content of atomic vector ##
## [1] 1 2 3 4 5 6
is.vector(die) ## Verifying that the object die is an atomic vector##
## [1] TRUE
five <- 5 ## Creating atomic vector with single value ##
five ## Showing content of atomic vector ##
## [1] 5
is.vector(five) ## Verifying that the object five is an atomic vector##
## [1] TRUE
length(five) ## Verifying that the object five has only one value ##
## [1] 1
length(die) ## Verifying the length of object die ##
## [1] 6
int <- 1L ## this creates an integer vector##
text <- "ace" ## this creates a character vector##
int <- c(1L, 5L) ## c can be used to make vectors with more than one element#
text <- c("ace", "hearts")## previous vector was an integer and this is a character##
sum(int) ## adds the elements of the int vector above##
## [1] 6
#sum(text) ## gives an error msg showing sum function cant be applied to characters##
die <- c(1, 2, 3, 4, 5, 6)
die
## [1] 1 2 3 4 5 6
typeof(die)## states what type of object die is##
## [1] "double"
is.double(die)
## [1] TRUE
##Integers##
int<- c(-1L,2L,4L) ## capital L with each vector element specifies element type as integer#
int
## [1] -1 2 4
typeof(int)
## [1] "integer"
sqrt(2)^2 -2
## [1] 4.440892e-16
## CHARACTERS
text <- c("Hello", "World")
text
## [1] "Hello" "World"
typeof(text)
## [1] "character"
typeof("Hello")
## [1] "character"
## LOGICALS
3 > 4
## [1] FALSE
logic <- c(TRUE, FALSE, TRUE)
logic
## [1] TRUE FALSE TRUE
typeof(logic)
## [1] "logical"
typeof(F)
## [1] "logical"
## COMPLEX
comp <- c(1 + 1i, 1 + 2i, 1 + 3i)
comp
## [1] 1+1i 1+2i 1+3i
typeof(comp)
## [1] "complex"
## RAW
raw(3)
## [1] 00 00 00
typeof(raw(3))
## [1] "raw"
## 5.3 MATRICES ##
m <- matrix(die, nrow = 2) ## CREARES A MATRIX M WITH 2 ROWS##
m
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
m <- matrix(die, nrow = 2, byrow = TRUE) ## byrow fills the matrix by rows first##
m
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## ARRAY
ar <- array(c(11:14, 21:24, 31:34), dim = c(2, 2, 3)) ## 3 2 dimensional arrays#
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
## LISTS
list1 <- list(100:130, "R", list(TRUE, FALSE)) ## List of 3 r objects#
list1 ## Displays 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
card <- list("ace", "hearts", 1)
card
## [[1]]
## [1] "ace"
##
## [[2]]
## [1] "hearts"
##
## [[3]]
## [1] 1
## 5.8 DATAFRAMES##
## Creating a dataframe with three different lists face, suit and value
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3))
df
## face suit value
## 1 ace clubs 1
## 2 two clubs 2
## 3 six clubs 3
typeof(df)
## [1] "list"
class(df)
## [1] "data.frame"
str(df) ## structure of 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)
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)
)
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