-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntroduction.R
More file actions
150 lines (82 loc) · 2.48 KB
/
Introduction.R
File metadata and controls
150 lines (82 loc) · 2.48 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
# Basics of R
# 1 - Assignment Operators
B <- 5
X = 6
# 2 - Normal Printing
print("Hello Pinto")
# 3 - Shortcut to run = Ctrl + Enter
# 4 - Printing Values from 1 to 10
print (1:10)
# 5 - Help from R (use ? or help())
?mean
help(solve)
# 6 - Install new packages (install.packages("package-name"))
install.packages("ggplot2")
# 7 - Enabling the package that you installed (the tick on the left package window will be ticked for ggplot2 package)
library(ggplot2)
# 8 - Arithmetic Operators
5+5 #addition
12-3 #subtraction
4-6
A = 12
B = 7
Ans = A*B
Ans # highlight and run from line 37 to 41 to obtain the result.
2^2 #power (2 to the power 2)
2**3 #power (2 to the power 3)
9%%4 #modulus (Remainder when 9 is divided by 4)
9%/%4 #quotient (When dividing 9 by 4, 4x2 = 8, so 2 is quotient)
# 9 - Logical Operators
A = 10
B = 2
A<B
A<=B
A>B
A>=B
A==B
A!=B
!(A>B)
var1 = A < B
var2 = A > B
isTRUE(A > B)
# 10 - Vectors (can only store same type of element. eg: numeric, Boolean, character, but not together)
vec1 = c(100, 25, 12, 1)
class(vec1) #Numeric vector
vec2 = c("Ben", "Josh", "Lop")
class(vec2) #Character Vector
# 11 - Lists (lists can store different types of elements)
data = list("Red", "Blue", c(23, 19, 7), TRUE, 15.4)
class(data)
# 12 - Matrix
z = matrix(c(1,2,3,4)) #notes the elements in the matrix
Z <- matrix(c(1,2,3,4), nrow = 2, ncol = 2, byrow = FALSE) #byrow = false, means fills the matrix column wise
# 13 - Factors (first create a vector)
vector1 = c ("Jan", "June", "April")
class(vector1) #Character Vector
months = factor(vector1)
class(months) #factor
levels(months) #the categories without repetition
table(months) #get values as a table
# 14 - Excel Sheet Fruit example (same concept as above)
vector2 = c(111,555,222,222,222,222,111,555,555)
vector3 = c(5,2,6,10,7,1,50,9,4)
vector4 = c(120,60,60,1000,50,58,89,89,40)
fruit = factor(vector2, c(111,222,555), c("Apple","Mango","Orange")) #Renaming 111, 222, 555 to Apple, Mango and Orange respectively.
amount = factor(vector3)
price = factor(vector4)
levels(fruit)
levels(amount)
levels(price)
table(fruit)
table(amount)
table(price)
# 15 - Data Frame
ID = c('A01','A02','A03','A04')
Marks = c(100,99,98,75)
Gender = c("Male","Female","Female","Male")
#data_set is the name of the data frame
data_set = data.frame(ID,Marks,Gender)
data_set
# When creating a data frame
# 1) First create vectors
# 2) Create a data frame including the created vectors