-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicsOfR_1.R
417 lines (315 loc) · 8.12 KB
/
BasicsOfR_1.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# Title: Intro to R and RStudio 1
# Goal: Learn basic data types, functions and data structures
# Course: AI in Finance
# Date: 30.9.2022
# Author: Martina HalouskovC/Sujit Thapa
# License: GPL>=3
##############################################################
##### Mathematical operations (using R as a calculator) #####
##############################################################
# Using R as a calculator
2+2
6/3
2^3 # this is a comment
(5-2)*2
sqrt(16)
log(10) # log base e
# Numeric variables
a=3
b=8
a+b
a*b
(a+b)^2
c=a+b+10
c
d <- a+b + 10 # spaces are not important, we can use = or <-
d
e=3
E # R is case sensitive (E is not e)
# Notice: a, b, c, d and e are now in our environment (top right window)
##############################################################
#################### Data types, vectors #####################
##############################################################
# There are 4 students, their heights are: 165 151 182 and 177 centimeters
# Store these numbers in a data vector with the c() function
heights=c(165,151,182,177)
# Need some help?
?c()
# Look at the data
heights
# [1] refers to the first observation
heights[1]
# We can combine vectors
x=c(1.5, 3.2, -4.7)
y=1:10
z=c(x,y)
z
# Vectors have types!
class(x)
class(y)
# Note: If the data consists of only whole numbers, the type is "integer", otherwise we use "numeric"
is.numeric(x)
is.character(x)
# Character vectors (plain text)
# Lets give these 4 students names
students=c("John", "Sarah", "Tim", "Jane")
class(students)
students[2]
# We can convert data types
x.char=as.character(x)
x.char
x.num=as.numeric(x.char)
x.num
# Logical vectors
# Do these four students speak English?
english=c(TRUE,FALSE,FALSE,TRUE)
class(english)
# How many of them speak English?
sum(english)
# Logical operators
x == 0
students == "John"
students != "John"
heights > 160
?Logic
# Factors
group = c("Group A", "Group B")
group = as.factor(group)
group
class(group)
##############################################################
################### Working with vectors #####################
##############################################################
# Lets explore the vector with heights some more and apply some basic functions
heights
# Length of vector
length(heights)
# Sum
sum(heights)
# What is the average height?
sum(heights)/length(heights)
mean(heights)
# Who is the smallest?
min(heights)
which(heights == min(heights))
# Which student is the tallest?
max(heights)
which(heights == max(heights))
# Min and max
range(heights)
# Standard deviation
sd(heights)
# The summary function produces basic descriptive statistics
summary(heights)
# Boxplot - our first graph!
p=boxplot(heights)
p
# We can sort the heights
sort(heights)
sort(heights, decreasing = T)
sort(heights, decreasing = F)
# Cumulative sum
cumsum(heights)
# Heights of these students the next year
heights.next = c(170,151,183,179)
heights.next[2]-heights.next[1]
# How much did they grow?
heights.next - heights
# How much did they grow on average?
mean(heights.next - heights)
# Notice, functions are applied to each entry in the vector
##############################################################
############## Conditional Statements and Loops ##############
##############################################################
# Conditional Statements (Similar to IF in excel)
# Test, whether a variable is equal to 1
variable=2
if (variable == 1){
print("Yes")
}
# Great! Now lets add "else"
variable=1
if (variable > 0){
print("Yes")
} else {
print("No")
}
# Try setting variable equal to any value you want
variable= c("SUJIT", "SHYAM", "GHANSHYAM")
# If "variable" is greater than 4, divide it by 2 and print it
# If not, print a message that tells us, the variable is smaller then 4
if(variable == "Shyam"){
print("gotcha2")
}else{
print("sorry")
}
# FOR LOOPS
for (i in 1:10){
print(i)
}
for (i in 1:10){
j=i*2
print(j)
}
for (i in seq(5,15)){
print(i)
}
# Let's combine loops with if statements
for (i in 1:30){
if((i %% 2) == 0) {
print(paste(i,"is even"))
} else {
print(paste(i,"is odd"))
}
}
# WHILE LOOPS
i=1
while (i < 10) {
print(i)
i=i+1
}
# FINAL CHALANGE - create a function
my_function <- function(x){
x^2/2
}
my_function(3)
# Define a function, that returns:
# the sum of all values in a vector, if the vector has at least 5 values
# or the mean of all values if there are less then 5 values
my_func <- function(x){
if (length(x) >= 5){
sum(x)
} else {
mean(x)
}
}
x=c(seq(1:14))
my_func(x)
##############################################################
############## Data structures - DATAFRAMES #################
##############################################################
# Constructing data frames (something like an excel spreadsheet)
vec1 = rep(c("A","B","C","D","E","F"), 10)
vec2 = rnorm(n=60, mean=10, sd=5)
vec3 = rep(c("TRUE","FALSE"),30)
DT=data.frame("grades"=vec1,
"points"=vec2,
"logicvec"=vec3)
# Explore
View(DT)
head(DT)
tail(DT)
# What are the dimensions?
dim(DT)
dim(DT)[2]
# Number of rows
nrow(DT)
#Number of columns
ncol(DT)
# Column names and row names
names(DT)
colnames(DT)
rownames(DT)
# Add new row names
rownames(DT)=paste("row", seq(60))
####
DT["row 2", ]
# Accessing elements
# 1. By names
DT$grades
DT$points
DT$logicvec
# 2. By indices
# DT[row,column]
# Look at a single row
DT[2,]
# Look at a single column
DT[,2]
# Look the value in first row and first column
DT[1,1]
DT["row 1","grades"]
# Look at multiple rows and multiple columns
DT[c(3,5),1:3]
DT[c(3,5),c(1,2,3)]
# Setting new values
# 1. By names
DT$empty=NA
DT$points2=DT$points*2
DT$diff=DT$points2 - DT$points
View(DT)
# 2. By indices
DT[,7]=rnorm(n=60, mean=0, sd=1)
DT[61,]=c(1:7)
# 3. With logical operators
nerds = DT[DT$grades == "A",]
nerdsss = DT[DT["grades"] == "A",]
top = DT[DT$points >= 20,]
# 4. Subsetting
topdiff = subset(DT, points>= 20, select = c(points,diff))
# Combine dataframes
rbind(nerds,top)
cbind(top,topdiff)
##############################################################
########### Working directory, Save and load files ##########
##############################################################
# CSV
# Save this dataset as a csv to a folder called "data"
getwd()
my_wd=getwd()
setwd("./data")
write.csv(x = DT, file = "DT.csv")
# Load this dataset back to a new object
newDT=read.csv("DT.csv")
rownames(newDT)=newDT$X
newDT=newDT[,2:8]
# RData
save(DT, file="DT.RData")
load("DT.RData")
# RDS
saveRDS(DT, file="DT")
DT=readRDS("DT")
setwd(my_wd)
##############################################################
################ Data structures - LISTS ####################
##############################################################
# Vectors or any datatypes and datastructures can be combined into lists
# Lists can be named and nested
mylist = list("grades"=vec1,
"points"=vec2,
"logicvec"=vec3,
"nerds"=nerds,
"gradelist"=list("A"=1,"B"=2,"C"=3, "D"=4, E="5", "F"=6))
class(mylist)
str(mylist)
# Accessing lists
mylist[[1]] # by index
mylist$logicvec # by a name
mylist[[4]][1,1] # first column & first row in fourth list element
mylist$nerds[1,1]
mylist[[5]] # fifth element is a list within a list
mylist[[5]][[1]]
length(mylist[[2]])
mean(mylist[[2]])
##############################################################
############## Data structures - MATRICES ###################
##############################################################
A=matrix(1:10,nrow=2)
B=matrix(11:20,ncol=5)
A
B
is.matrix(A)
is.matrix(B)
A+B
A-B
A*B
3*A
A%*%B
A %*% t(B)
C = t(A) %*% B
topdiff.m=as.matrix(topdiff)
topdiff.m
k=c(1,8,6,3,11)
l=c(9,7,6,2,2)
m=rbind(k,l)
n=cbind(k,l)
is.matrix(n)