-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_programming.R
More file actions
613 lines (407 loc) · 14 KB
/
Copy pathR_programming.R
File metadata and controls
613 lines (407 loc) · 14 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
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#Anurag Yadav ( R-Programming ) # commonly used Program
help.start() # open manual page
# Variables
Data <- 45 # Enter a numeric
class(Data) # Check the class of the variable ## class "numeric" output
x<-1.5
class(x) # check the class
y<-"a"
class(y) # class "character"
Data2<-c(9, 10, 11,5, 6)# store more then one variable
class(Data2)
data1<-c(9, 10, 11, 12, 5, 6) # calculate mean of object
mean(data1)
help(mean)
data2=c(0,0,0,9,10,11,5,6)
mean(data2, trim=0.1)
x<-c(0:10,50)
x
xm<-mean(x)
c(xm, mean(x,trim=0.10))
y<- c(2:9)
mean(y)
apropos("mean")# function to find all the function with name mean
data2
class(data2)
data3<as.character(data2)
data3
# installing packeges
install.packages("ade4",dependencies = TRUE)
library(ade4)
library(dplyr)
detach (package:ade4)# deatch- unload the package
search() # helps you check which packages/environments are currently loaded and attached.
# VECTOR
D<-c(1,2,3,4,5)# Vector
D
# List
my_list<-list(22, "ab", TRUE)# create a list
my_list<-list(data,"ab",TRUE)# create a list
# Matrix
matrix<-matrix(1:6, nrow=3, ncol=2)
matrix
matrix<-matrix(1:6, nrow=3,ncol=2, byrow=T)
matrix
matrix
f<-1:5
g<-6:10
h<-11:15
cbind(f,g,h)# colum wise bind
rbind(f,g,h)#row wise bind
# data frame
df<-data.frame(name=c("Anurag","John","Ankit","Ajay"),score=c(87,76,91,67))
df
# Subsetting the R_object
data[3] # Print 3rd position from Vector
my_list[[2]] # Print second position from list my_list
matrix[2,1]# print 2nd col 1 value
matrix[3,]# print all the value of row 3
matrix[,2]# print all the value of colum 2
df[3,1]# print info stored in all 3rd row first colum
df[2,] # print second row
df[2:3,] # Print all info second and third row
df[3,1] # print info stored in 3rd row first columns
df[3,] # Print third row
# subset of the dataframe
df1<-subset(df,score>60) # Print Score more than 60
df1
data[3]>2
df$score>60
#Another data frame
a<-c(20,30,40,50)
b<-c('Apple','Banana','textbook','pencil')
c<-c(TRUE, FALSE, TRUE, FALSE)
d<-c(2.5,8,10,7)
df<-data.frame(a,b,c,d)
colnames(df)<-c("ID","ITEMS","STORE","PRICE")
class(df)# class of Data
str(df) # View struc of data
df[1,2] # Select row 1 in colum2
df[1,3] # select row 1 to 3
df[1:3, 3:4] # select row 1 to 3 and colums 3 to 4
# Install packeges
install.packages("dplyr", dependencies = T)
# adding columns to existing dataframe
df$Quantity<-c(10,35,50,4)
df
# add row to existing dataframe
df[5,] <- c(50,'eraser', FALSE, 4.5, 8)
str(df)
df
#Converting logical and numeric # for some dplyr function
df$STORE = as.logical(df$STORE)
df$PRICE = as.numeric(df$PRICE)
df$Quantity = as.numeric(df$Quantity)
str(df)
library(dplyr) # attached packeges dplyr
select(df,ends_with("e")) # using Select function print all the column which end with "e".
select(df,STORE:Quantity)# Select and print all column from store to quantity
select(df,!(PRICE))# Select and print all column except price
filter(df,PRICE>8) # filter function using print price more than 8
filter (df, ITEMS=="Banana") # filter Items as banana
df%>%filter(store=TRUE, PRICE<5) # info of all the items which are available in store and pric is less than 5
filter(df,(STORE == TRUE) & (PRICE <5))# this another way info of all the items which are available in store and pric is less than 5
arrange(df,desc(PRICE)) # arrange- arrange the dataframe based on increasing order
rename(df,cost=PRICE)# rename - chnage the colum name
df
#Convert to numeric Before using mutate()
df$TOTAL<- df$PRICE*df$Quantity # create a new column TOTAL (PRICE * Quantity)
mutate(df,TOTAL=PRICE*Quantity)
print(df)
str(df)
group_by(df,STORE)%>%summarise(mean(Quantity)) # group_by the data frame based or store availability , mean quantity required
group_by(df,STORE)%>%summarise()
?summarise
df[4,3]<-TRUE #sets that specific cell to the logical value TRUE
df
df[5,5]<-NA # it change specific column
df
mean(df$Quantity,na.rm = TRUE) # calculate mean if NA is there in the data frame
mean(df$Quantity)
df$QUANTITY <- as.numeric(df$Quantity)
table(is.na(df)) #Tabulate number of NA in the data frame
new_df <-na.omit(df) #remove rows with NA
new_df
# ##### File Handling ######
getwd() #get the working directory
file.choose() # choose file
# read txt file
df<-read.table( "C:\\Users\\HP\\OneDrive\\Desktop\\R_files(CSV,ETC.)\\r_class1.txt", sep="\t")
df
# reading csv file
file.choose()# chooseing file
df<-read.csv("C:\\Users\\HP\\OneDrive\\Desktop\\R_files(CSV,ETC.)\\drug-target.csv",sep="\t",header=TRUE)
df
# Read a delim file with sep ":"
df_delim<-read.delim("C:\\Users\\HP\\OneDrive\\Desktop\\R_files(CSV,ETC.)\\r_class1_delim.txt",sep=":")
df_delim
# read excel
install.packages("readxl",dependency=TRUE)
library(readxl)
ls("packeges:readxl")
file.choose()
df_xls<-read_excel("C:\\Users\\HP\\OneDrive\\Desktop\\R_files(CSV,ETC.)\\r_class1.xlsx")
df_xls
# read xml file
install.packages("xml2",dependencies = T)
library(xml2)
xml_file <- read_xml("C:\\Users\\HP\\OneDrive\\Desktop\\R_files(CSV,ETC.)\\books.xml")
ls("package:xml2")
xml_list <- as_list(xml_file)
xml_name(xml_file)
xml_parent(xml_file)
xml_text(xml_file)
library(tibble)
title <- xml_text(xml_find_all(xml_file,xpath = "//title"))
author <- xml_text(xml_find_all(xml_file,xpath = "//author"))
df <- tibble(catalog = title, authors = author)
df
#Read json file
file.choose()
install.packages("jsonlite")
library(jsonlite)
jsonfile <- fromJSON("C:\\Users\\HP\\OneDrive\\Desktop\\R_files(CSV,ETC.)\\color.json")
jsonfile$aqua
#Read html file
h <- read_html("C:\\Users\\HP\\OneDrive\\Desktop\\R_files(CSV,ETC.)\\cran.html")
h
# control Structure
# if , while, loop, next
# if condition
data<-sample(1:100,10,replace=T)
data<-c(1,2,3,4,5,6,7,8,9,10)
for(i in 1:10){
print(i)
if (data(i)==7){
print(i)
print("yes")
}
}
# while
x<-0
while(x<=35){
if(x!=7){
print(x)
}
x<-x+1
}
# printing odd even no
Data <- sample(1:100, 10, replace = TRUE)
print(Data)
for(i in Data){
if(i %% 2 == 0){
print(paste(i, "no is even"))
} else {
print(paste(i, " no is odd"))
}
}
############
Data <- sample(1:100, 10, replace = TRUE)
print(Data)
r <- 1
while(r <= length(Data)){
if(Data[r] %% 2 == 0){
print(paste(Data[r], "is even"))
} else {
print(paste(Data[r], "is odd"))
}
r <- r + 1
}
# print 10 to 1 number
i=10
while(i>=1){
print(i)
i=i-1
}
# Next
for ( i in 1:100){
if(i <= 20){
next
}
print(i)
}
# Repeat
#(print print each number until it reaches 20. After that, the loop stops using the break statement)
x <- 10
repeat {
print(x)
x <- x + 1
if(x > 20){
break
}
}
# Function
# Defina a Function
myfirstfun <- function (n)
{
# Compute the square
n*n
}
myfirstfun(9)
# a function to compute power of one number to the other
power <- function(a,b){
a^b
}
power(4,4)
# function to calculate Z-Score
Z_Score <- function(Value, a) {
mu <- mean(a) # mean of vector a
std <- sd(a) # standard deviation of a
z <- (Value - mu) / std
return(z)
}
a <- c(6, 7, 5, 4, 3, 2, 1, 8)
Z_Score(3, a)
# function to print reverse
info <- function(name) {
X <- strsplit(name, "")[[1]] # split name into characters
return(c(rev(X), length(X))) # reverse and show length
}
info("ATGCATGC")
#Descriptive statistics
data <- mtcars
dim(data)
head(data)
shapiro.test(data$mpg) #To test if the data follows a normal distribution
summary(data) #Summarizes the data
quantile(data$qsec) #Prints the quantile
quantile(data$qsec,0.80) #Prints 80 percentile value
hist(data$mpg)
data$mpg
file.choose()
my_file <- read.csv("C:\\Users\\HP\\Downloads\\r_class1.csv",
sep=",",header=T)
head(my_file)
shapiro.test(my_file$Prog_experience)
hist(my_file$Prog_experience)
#One-sample t-test
t.test(mtcars$qsec)
t.test(mtcars$qsec, mu=17)
#One-sample wilcox test
x <- c(20,29,24,19,20,28,23,19,19)
hist(x)
wilcox.test(x)
wilcox.test(x,mu=20)
#Two sample t.test
x <- c(0.80,0.83,1.89,1.04,
1.45,1.38,1.91,1.64,0.73,1.46)
y <- c(0.80,0.83,1.89,1.04,
1.45,1.38,1.91,1.64,0.73,1.46)
y1 <- x+1
t.test(x,y)
t.test(x,y1)
file.choose()
drug <- read.csv("C:\\Users\\HP\\OneDrive\\Desktop\\R_files(CSV,ETC.)\\drug_BMR.csv")
head(drug)
shapiro.test(drug$BMR)
t.test(drug$BMR,drug$Two_weeks)
t.test(drug$BMR,drug$Two_weeks,paired=T)
t.test(drug$BMR,drug$Four_weeks,paired=T)
t.test(drug$BMR,drug$Four_weeks,paired=T,alternative="greater") # Ho: mu0 < mu1
t.test(drug$BMR,drug$Four_weeks,paired=T,alternative="less") #Ho: mu0 > mu1
mean(drug$BMR)
mean(drug$Four_weeks)
#Kolmogorov-Smirnov test #To test the difference is distribution
ks.test(drug$BMR,drug$Four_weeks,alternative="less") #Ho: distribution of 0 > distribution of 1
a <- rnorm(100)
b <- runif(100)
?runif
hist(a)
hist(b)
ks.test(a,b)
#Fisher test
#To test the difference in variance
var.test(a,b)
#Chi-sq test
my_file <- read.csv("/home/bif/rclass/r_class1.csv",sep=",",header=T)
#Ho: There is no association between dept and programming experience
chisq.test(table(my_file$Department, my_file$Prog_experience))
chisq.test(table(my_file$Department, my_file$R_prog))
#correlation
shapiro.test(mtcars$mpg)
head(mtcars)
#H0: The qsec is not correlated to mpg
cor.test(mtcars$mpg,mtcars$qsec,method=c("pearson"))
cor.test(mtcars$mpg,mtcars$qsec,method=c("spearman"))
?cor.test
#Regression
?lm
summary(lm(mtcars$qsec~mtcars$mpg))
#aov
summary(aov(mtcars$mpg~factor(mtcars$am)))
head(mtcars)
?aov
#other data
file.choose()
covid_data <- read.csv("C:\\Users\\HP\\OneDrive\\Desktop\\R_files(CSV,ETC.)\\covid19_data.csv")
head(covid_data)
#Does the number of cases in different states follow normal distribution?
shapiro.test(covid_data$Cases)
#Is there difference in mean, distribution and variance of recovered patients and deaths?
wilcox.test(covid_data$Recovered,covid_data$Death)
ks.test(covid_data$Recovered,covid_data$Death)
var.test(covid_data$Recovered,covid_data$Death)
library(dplyr)
#Is there association between mean number of cases and zones?
#chisq test cannot be used since the one of the data is continous
#We can however comapre the mean of all the groups
a <- covid_data %>% group_by(Zone) %>% summarise(mean(Cases))
table(covid_data$Cases,covid_data$Zone)
summary(aov(covid_data$Cases~factor(covid_data$Zone)))
#Is there a mean difference in number of nurses in different zones?
summary(aov(covid_data$Nurses~factor(covid_data$Zone)))
#Is there a correlation between no. of cases and number of aircraft movements?
cor.test(covid_data$Cases,covid_data$Aircrats_movements,method=c("spearman"))
#Graph Visualization
library(dplyr)
file.choose()
my_data <- read.csv("C:\\Users\\HP\\OneDrive\\Desktop\\R_files(CSV,ETC.)\\covid19_data.csv")
head(my_data)
glimpse(my_data)#Get the glimpse of the data
#Pie charts
my_data_zone = table(my_data$Zone)
percentlabels <- round(100*my_data_zone/sum(my_data_zone),2) #Cal % values
#R code to create the Pie Chart
pie(my_data_zone, col=rainbow(length(my_data_zone)), labels = percentlabels, main = '% of States')
#Legend for the pie chart
legend("right",c("Centre","East","North","North-East","South","West"),fill=rainbow(length(my_data_zone)),cex=0.6)
#Bar plot
x <- group_by(my_data, Zone) %>% summarise(mean(Cases)) #This we have done in the previous class
xVal<-c("Centre","East","North","North-East","South","West")
#R code to create the barplot
barplot(x$`mean(Cases)`,xlab="Zones",ylab="Mean Cases",col=terrain.colors(6),
main="Mean Distribution Of Covid-19 cases (Zone)",border="black",names.arg=xVal)
# Plot the bar chart
my_data1 <- arrange(my_data,Zone)
mycols <- terrain.colors(6)
barplot(my_data$Cases,ylab="Total number of cases",
main="Distribution Of Covid-19 cases (State)",border="black",col = mycols[my_data1$Zone],names.arg=my_data1[,1],cex.names = 0.7,las=2)
#Boxplot
boxplot(my_data$Cases,my_data$Recovered,my_data$Death, ylab="Number",
main="Boxplot",border="black",col = c("Blue","Green","Red"),at = c(1,2,3),
names = c("Cases", "Recovered", "Deaths")) # With outliers
boxplot(my_data$Cases,my_data$Recovered,my_data$Death, ylab="Number",
main="Boxplot",border="black",col = c("Blue","Green","Red"),at = c(1,2,3),
names = c("Cases", "Recovered", "Deaths"),outline=F) #Without outliers
#scatter plot
plot(my_data$Cases,my_data$Aircrats_movements,ylab="Aircraft movements",xlab="# of cases",main="Cases Vs Aircrafts Movements")
plot(my_data$Cases,my_data$Aircrats_movements,ylab="Aircraft movements",xlab="# of cases",main="Cases Vs Aircrafts Movements",pch=16,cex=1.3,col="blue")
plot(my_data$Cases,my_data$Aircrats_movements,xlab="# of cases",ylab="Aircraft movements",main="Cases Vs Aircrafts Movements",pch=16,cex=1.3,col="blue")
abline(lm(my_data$Cases~log(my_data$Aircrats_movements)))
my_data2 <- mutate(my_data, Health_workers = Nurses+Doctors) #Creates a new variable Health_workers combining data from Nurses and Doctors
plot(my_data2$Health_workers,my_data2$Recovered,xlab="Recovered",ylab="Health Workers",main="Recovered Vs Health Workers",pch=16,cex=1.3,col="indianred")
abline(lm(my_data2$Recovered~my_data2$Health_workers))
#correlogram
install.packages("corrplot")
library(corrplot)
my_data_x <- my_data[3:11] # Subsetting the data from 3rd to 11th column
head(my_data_x)
corrplot(corr_matrix,
method = 'number',
type = "lower")
#Histogram
hist(my_data$Cases,col='steelblue',main='Number of Covid-19 Cases',xlab='Cases')
#Heatmap
my_data_cases <- as.matrix(select(my_data,Cases:Death))
rownames(my_data_cases) <- my_data$State #Make row names as States
heatmap(my_data_cases,Colv=NA,col=cm.colors(256),cexCol = 1.0)