-
Notifications
You must be signed in to change notification settings - Fork 1
/
script_adv.R
585 lines (398 loc) · 8.17 KB
/
script_adv.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
###############################
# "Introduction to R"
# author: "Rodrigo Amadeu"
# date: "Sep 11th, 2018"
###############################
## Before Start
## Create a folder for your project
## Example: ~/Documents/Introduction_to_R_Class
## Open RStudio
## Create an R script (.R extension)
print("Hello World!")
## Save it
## Programming practices
## Comments!
## Working Directory
getwd()
setwd("?") ##fill with your path to archives
## RStudio: Session -> Set Working Directory -> ...
## Linux =/= Windows =/= Mac
## R as calculator
3+3
(3+3)/3
9^0.5
2^10
## Functions
sqrt(9)
?sqrt
log(10)
log(x = 10,base = 10)
log(x = 10,base = 2)
exp(2)
## Creating objects
x <- 10
x = 10
x*2
x+x
x^x
log(x,x)
## Creating objects and class
x_name = "name"
x = 12
x_name
class(x_name)
class(x)
## Logical class
1<0
1==0
1>0
z=1<0
class(z)
is.logical(z)
is.numeric(x)
is.character(x)
is.character(x_name)
## Missing Value - be careful!
# For every class
NA
NA+1
2*NA
## Vectors
x = c(1.5, 2.1, 2.5, 3.4, 4.3, 6.1) #blank spaces not matter
y = c("A","A","B","B","C","C")
x[6]
y[2]
str(x)
str(y)
sum(x)
mean(x)
median(x)
var(x)
summary(x)
sum(y)
## Factors
y <- as.factor(y)
summary(y)
class(y)
#Creating vector from scratch
?rep
rep(1,10)
rep(NA,5) #NA for missing data
?seq
seq(1,10,1)
seq(1,10,0.5)
## Combining
rep(seq(1,10,.5),each=2)
?sample
sample(seq(1,3,0.5),size = 5)
sample(seq(1,3,0.5),size = 5, replace = TRUE)
## Logical operators on Vector
z = x<3
z
class(z)
which(z)
x[which(z)]
#or
which(x>3)
x[which(x>3)]
## Data frame and object names
df = data.frame(x=x,y=y,z=z)
## Logical and self-explanatory
## No space, ~, `, "
df = data.frame(Yield=x,
Genotype=y,
Disease=z)
df$yield #R is case-sensitive
df$Yield
df$Genotype ## String when in a df -> factor!
df$Disease
str(df)
mean(df$Yield)
tapply(df$Yield,df$Genotype,mean)
tapply(df$Yield,df$Disease,mean)
## Graphics
## Graphics
## Scatterplot
plot(y=df$Yield, x=df$Disease)
## Boxplot
boxplot(df$Yield~df$Genotype)
boxplot(df$Yield~df$Disease)
## REVIEW
## numeric, character, logical, factor
## vector, matrix, data.frame, (array), list
## how to access
## IMPORTING YOUR DATA
## Using RStudio
## Using script
read.table("data.csv")
read.table("data.csv",header = TRUE, na.strings=-9, sep=",")
data=read.table("data.csv",header = TRUE, na.strings=-9, sep=",")
str(data)
write.table(data,file="data_export.csv")
write.table(data,file="data_export.csv",row.names=FALSE,quote=FALSE,na="-9")
## Reading online data
data=read.table("https://www.dropbox.com/s/kapir2p76j4moec/data.csv?raw=1",header = TRUE, na.strings=-9)
data=read.table("https://raw.githubusercontent.com/rramadeu/Tutorials_File/master/Intro_R/data.csv",header=TRUE)
## From google docs
library(gsheet)
data <- gsheet2tbl('https://docs.google.com/spreadsheets/d/1AGGZk-KdRpGh-yLh5SV_77KTVm0vOH57pg2NpiU_Tcg')
## Installing a package
# What is a package?
## CRAN (++ reliable, ++ stable, ++ burocracy)
# https://cran.r-project.org/)
# Package agricolae from cran
#install.packages("agricolae")
## Bioconductor (++ reliable, stable, + burocracy) - Genetics/Molecular
#https://www.bioconductor.org/
# Package popr
#source("http://bioconductor.org/biocLite.R")
#biocLite("goseq")
## Another source? (reliable? stable?)
#Dev Versions or not into big repositories: github platform
#www.github.com/rramadeu/AGHmatrix
## From a tar.gz? RStudio or put the address
install.packages("agricolae") ## From command line
## Or Tools -> Install Packgaes...
## Once installed, you can load it!
library(agricolae) ## To load it always use library not require!
?HSD.test
## EXTRA
## Matrices
x
matrix(x,nrow=2,ncol=3)
matrix(x,nrow=3,ncol=2,byrow=TRUE)
matrix(x,nrow=2,ncol=3,byrow=TRUE)
matrix(x,nrow=3,ncol=2,byrow=FALSE)
matrix(x,nrow=2,ncol=3,byrow=FALSE)
X <- matrix(x,nrow=3,ncol=2) #R is case-sensitive
X[3,1] #row,column
X[3,]
X[,1]
X[,1:2]
rownames(X) <- c("A","B","C")
colnames(X) <- c("R","T")
## Matrix algebra
t(X)
t(X) %*% X
crossprod(X) ## Faster!
X + 1
2*X
solve(t(X) %*% X)
solve(t(X) %*% X) + 1
X*X ## Element-wise Product | Haddamard Product
## Lists
everything <- list(df=df,X=X)
str(everything)
## Lists
everything
everything$df
everything$X
everything[[1]]
everything[[1]][[2]]
everything[[1]][[2]][[1]]
everything[[1]][[2]][[3]]
everything[[1]][[1]][[4]]
everything[[2]]
everything[[2]][2,1]
everything[[2]][2,]
everything[[2]][,1]
everything[[2]][,"R"]
## GRAPHICS
## Loading a package
#install.packages("dataset")
library(datasets)
data(mtcars)
str(mtcars)
?mtcars
## Library
mean(mtcars$mpg)
summary(mtcars)
## Graphic Plotting
head(mtcars)
## Scatterplot
plot(x=mtcars$mpg,y=mtcars$hp)
## Scatterplot
plot(x=mtcars$mpg,y=mtcars$hp, main="Scatterplot mpg x hp", xlab="mpg", ylab="hp")
## Histogram
hist(x=mtcars$mpg, main="Histogram of mpg", xlab="mpg")
## Density
plot(density(mtcars$mpg), main="Density", xlab="mpg")
## Boxplot
boxplot(x=mtcars$mpg, main="Boxplot of mpg", xlab="mpg")
## Boxplot by cylindrade
boxplot(mtcars$mpg ~ mtcars$cyl, main="Boxplot of mpg", xlab="mpg")
## Multiple Scatter Plots
pairs(mtcars)
## Simulating data
#http://www.stat.umn.edu/geyer/old/5101/rlook.html
#Normal Distribution
?rnorm
plot(density(rnorm(100000,0,1)))
#Distribution Family functions
rnorm(10,0,1) #“r”: random, randomly generated numbers
pnorm(0,0,1) #“p”: probability, cumulative density function
qnorm(0.5,0,1) #“q”: quantiles, cumulative density function (quantiles)
dnorm(0,0,1) #"d": density, height of the probability density function
## Simulating data
#http://www.stat.umn.edu/geyer/old/5101/rlook.html
hist(rnorm(1000,0,1))
## Simulating data with seed
set.seed(100)
hist(rnorm(1000,0,1))
set.seed(100)
rnorm(1,0,1)
rnorm(1,0,1)
set.seed(100)
rnorm(1,0,1)
## Correlation Plot
A = rnorm(50,10,6)
B = -(A-rnorm(50,10,2))
plot(A,B)
C = B+rnorm(50,10,10)
plot(A,C)
plot(B,C)
ABC <- cbind(A,B,C)
class(ABC)
dim(ABC)
head(ABC)
head(ABC,10)
tail(ABC)
#Getting the correlation matrix
var(A,B)/sqrt((var(A)*var(B)))
var(ABC[,1],ABC[,2])/sqrt((var(ABC[,1])*var(ABC[,2])))
?cor
cor(A,B)
cor(A,A)
cor.matrix = cor(ABC)
cor.matrix
#Changind the method
library(corrplot)
corrplot(cor.matrix, method="color")
?corrplot ## Help on arguments
#Corr value inside the cell (black for the color of the coef.)
corrplot(cor.matrix, method="color", addCoef.col="black")
## Exporting a graphic using RStudio
## Exporting a graphic with high quality
tiff(filename = "Rplot.tiff", width = 5, height = 5, units="in", res=300)
#Corr value inside the cell (black for the color of the coef.)
corrplot(cor.matrix, method="color", addCoef.col="black")
dev.off()
getwd()
## A little about programming concepts
## For and If structures
## For i
x=10
for( i in 1:10){
x = x+1
}
x
## For ii
x=10
for( i in 1:10){
x = x+1
print(i)
}
x
## For 3
x=10
for( i in 1:10){
x = x+1
print(paste("i=",i,"; x=",x))
}
x
## If 1
x=0
if(x==0){
print("x is equal 0")
}
x=1
if(x==0){
print("x is equal 0")
}
## If 2
x=0
if(x==0){
print("x is equal 0")
}else{
print("x is different than 0")
}
x=1
if(x==0){
print("x is equal to 0")
}else{
print("x is different to 0")
}
# For + If
x=0
for(i in 1:10){
if(x%%2==0){ ## if remainder using 2 as divisor is 0
print(paste(x,"is even"))
}
if(x%%2==1){
print(paste(x,"is odd"))
}
x=x+1
}
#For + If: Another way
x=0
for(i in 1:10){
if(x%%2==0){
print(paste(x,"is even"))
}else{
print(paste(x,"is odd"))
}
x=x+1
}
## While, the condition is on evidence
## e.g.: used on convergence algorhythm
x=1
while(x<10){
x=x+1
}
x
## Repeat, nothing on evidence. Avoid it!
x=1
repeat{
x=x+1
if(x==10){
break
}
}
## Control flow
for(i in 1:10){
if(i==2){
next
}
if(i==7){
break
}
print(i)
}
## Control flow
for(i in 1:10){
if(i==2)
next
print(i)
}
## Without brackets
x=1
if(x%%2==1){
print(paste(x,"is odd"))
}
if(x%%2==1)
print(paste(x,"is odd"))
#CreatingFunctions
## Name, function, arguments, {}, return?
is_even = function(x){
if(x%%2==0){
return(TRUE)
}else{
return(FALSE)
}
}
is_even(10)
is_even(1)
x=10
is_even(x)
## Back to Presentation