-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter 3 code.R
142 lines (107 loc) · 2.91 KB
/
Chapter 3 code.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# doubles
die <- c(1, 2, 3, 4, 5, 6)
typeof(die)
# is.vector() tests if an object is an atomic vector
is.vector(die)
# an atomic vector can have one value
five <- 5
is.vector(five)
# create an integer vector by adding "L"
integer <- c(-1L, 2L, 3L)
typeof(integer)
# floatingpoint errors - should equal zero but does not
sqrt(2)^2-2
# vector with complex numbers
complex <- c(1 + 1i, 1 + 2i, 1 + 3i)
# face names of cards of Royal flush
hand <- c("Ace", "King", "Queen", "Jack", "ten")
# testing for attributes
attributes(die)
# assigning names attribute to a vector
names(die) <- c("one", "two", "three", "four", "five", "six")
die
# change the names attribute of a vector
names(die) <- c("uno", "dos", "tres", "quatro", "cinco", "seis")
names(die)
die
# removing names attribute from a vector
names(die) <- NULL
names(die)
# converting a vector to a 2 x 3 array
dim(die) <- c(2,3)
dim
die
# create a matrix
m <- matrix(die, nrow = 2)
m
# create a matrix by row
m <- matrix(die, nrow = 2, byrow = T)
m
# create an array
ar <- array(c(11:14, 21:24, 31:34), dim = c(2, 2, 3))
ar
# create matrix of royal flush
hand1 <- c("ace", "King", "Queen", "Jack", "ten", "spades", "spades", "spades", "spades", "spades")
matrix(hand1, ncol = 2)
hand1
dim(hand1)
royal_flush <- c("ace", "king", "queen", "jack", "ten", "spades", "spades", "spades", "spades", "spades")
royal_flush_matrix <- matrix(royal_flush, 5, 2)
royal_flush_matrix
# creating object with class matrix by changing dimension
die
dim(die) <- c(2,3)
typeof(die)
class(die)
# representing dates and times
now <- Sys.time()
typeof(now)
class(now)
unclass(now)
# using the POSIXct class
bil <- 1000200000
bil
class(bil) <- c("POSIXct", 'POSIXt')
bil
# creating a factor
gender <- factor(c('male', 'female', 'female', 'male'))
typeof(gender)
attributes(gender)
gender
# convert a factor to a character string
as.character(gender)
typeof(gender)
class(gender)
# exercise - knows that multiple types of data are coerced
# into a single data type when creating a vector
card <- c("ace", "heart", 1)
typeof(card)
# coercion occurs when trying to do mathematical operations
# with logical values
sum(c(TRUE, TRUE, FALSE, FALSE, FALSE, TRUE))
# functions for converting data types
as.character(1)
as.logical(1)
as.numeric(FALSE)
# the "list" object groups together R objects rather than values
list_one <- list(100:130, "R", list(TRUE, FALSE))
list_one
# exercise
list_card <- list("ace", "heart", 1)
list_card
# creating a data frame
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"),
value = c(1, 2, 3))
df
typeof(df)
class(df)
str(df)
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"),
value = c(1, 2, 3), stringsAsFactors = FALSE)
str(df)
head(deck)
# saves a copy of the new file
?write.csv
write.csv(deck, file = "cards.CSV", row.names = FALSE)