-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRcode_week1.R
More file actions
164 lines (105 loc) · 2.67 KB
/
Rcode_week1.R
File metadata and controls
164 lines (105 loc) · 2.67 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
class()
typeof()
a <- TRUE
class(a)
typeof(a)
b <- "hi"
class(b)
typeof(b)
c <- factor(b)
c
typeof(c)
class(c)
#from the robjects html file, week 1
#this is to check for integer
typeof(4L)
#this is to check for double
typeof(4)
#this will output double as well
typeof(4.3131)
#type of functions also work on built in functions
typeof(sqrt)
#this will also output function
class(class)
#test for precision, this will output 7
(sqrt(7))^2
#this will output a very tiny number
#caused by the fact that R can store 7, it can't store sqrt{7}
(sqrt(7))^2-7
#below is the largest integer you can store in R
.Machine$integer.max
#Below is the smallest and largest floating point number in R:
.Machine$double.xmin
#largest, use xmax function
.Machine$double.xmax
#division wiht integers
#simple division using integers
25/7
#using the modulo???? operator
#the number of complete
25%/%7
#trying modulo 25 modulo 7
25%%7
#rounding numbers, use the round function
round(168.5555)
#this code will round to one decimal place
round(168.923232, 1)
#round to 2 decimal places
round(163.1456, 2)
#round to nearest multiple of 10
#168 is closest to 200
round(168.323234, -1)
#round to the nearest multiple of 100
#167 is closest to 200
round(167.239283, -2)
#round up using the ceiling function
ceiling(163.454)
#round down (this is what we do with ages)
#use the floor function
floor(163.34839483)
#Numeric R objects
#performing simple math operations
#by storing variables
x <- 2
y <- 6.4
x2 <- x^2
fred <-2*x+5
wilma <- x+y
a.simple.fraction <- (x+1)/(y+2)
#execute the function/variable
a.simple.fraction
#logical objects using boolean
yy <- FALSE
happy <- TRUE
#examples using logical operators
#tip:never create a variable with true or false
2 < 3
#will output false
6 == (1-2)
#output true
6 != (2+3)
#will output true
10 > 1.5
#Logical functions in R
is.logical(2)
is.numeric(2)
is.numeric(2+4)
is.logical(2>4)
is.logical(a.simple.fraction)
is.character(wilma)
class(a.simple.fraction)
typeof(wilma)
#character objects
myname <- "richard"
your.full.name <- 'JUlius Caesar'
#any thing inside quotes are considered character objects
myname
your.full.name
#If a string contains a single quote we need to be sure to use double quotes as the delimiters or R gets confused.
seamus <- 'Seamus 0'Leary''
#that causes an error. Correction is:
seamus <- "Seamus O'Leary"
seamus
#the use of \ is an escape character, it says that what comes next needs to be treated differently to everything else
s <- "I said\"hello\" to the vistor"
s