-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_processing.R
More file actions
1376 lines (1115 loc) · 33.5 KB
/
R_processing.R
File metadata and controls
1376 lines (1115 loc) · 33.5 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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Declare variables of different types
# Numeric
x <- 28
class(x)
# String
y <- "R is Fantastic"
class(y)
# Boolean
z <- TRUE
class(z)
# is k an integer?
is.integer(x)
# First way to declare a variable: use the `<-`
name_of_variable <- value
# Second way to declare a variable: use the `=`
name_of_variable = value
# Print variable x
x <- 5
x
y <- 2
y
# operations
x-y #subtract
x*y #multiply
x**y #exponent
x%%y #modulus
x%/%y #integer division
# logical operators
x<y #less than
x<=y #less than or equal to
x>y #greater than
x>=y #greater than or equal to
x==y #exactly equal to
x!=y #not equal to
x|y == 7 #x OR y
x&y == 2 #x AND y
isTRUE(x<y) #test if X is TRUE
# An example
x <- c(1:10)
x[(x>8) | (x<5)]
# yields 1 2 3 4 9 10
# How it works
x <- c(1:10)
x
x > 8
x < 5
x > 8 | x < 5
x[c(T,T,T,T,F,F,F,F,T,T)]
# Construct a matrix with 5 rows that contain the numbers 1 up to 10 and byrow = TRUE
matrix_a <-matrix(1:10, byrow = TRUE, nrow = 5)
matrix_a
# Print dimension of the matrix with dim()
dim(matrix_a)
# Construct a matrix with 5 rows that contain the numbers 1 up to 10 and byrow = FALSE
matrix_b <-matrix(1:10, byrow = FALSE, nrow = 5)
matrix_b
# Print dimension of the matrix with dim()
dim(matrix_b)
matrix_c <-matrix(1:12, byrow = FALSE, ncol = 3)
print(matrix_c)
dim(matrix_c)
# Add a Column to a Matrix with the cbind()
# concatenate c(1:5) to the matrix_a
matrix_a1 <- cbind(matrix_a, c(1:5))
# Check the dimension
dim(matrix_a1)
matrix_a1
matrix_a2 <-matrix(13:24, byrow = FALSE, ncol = 3)
matrix_a2
matrix_c <-matrix(1:12, byrow = FALSE, ncol = 3)
matrix_d <- cbind(matrix_a2, matrix_c)
dim(matrix_d)
matrix_c
matrix_c[1,2] #selects the element at the first row and second column.
matrix_c[1:3,2:3] #results in a matrix with the data on the rows 1, 2, 3 and columns 2, 3,
matrix_c[,1] #selects all elements of the first column.
matrix_c[1,] #selects all elements of the first row.
## Categorical Variables
# Create gender vector
gender_vector <- c("Male", "Female", "Female", "Male", "Male")
class(gender_vector)
# Convert gender_vector to a factor
factor_gender_vector <-factor(gender_vector)
class(factor_gender_vector)
# Create a color vector
color_vector <- c('blue', 'red', 'green', 'white', 'black', 'yellow')
# Convert the vector to factor
factor_color <- factor(color_vector)
factor_color #From the factor_color, we can't tell any order.
# Create Ordinal categorical vector
day_vector <- c('evening', 'morning', 'afternoon', 'midday', 'midnight', 'evening')
# Convert `day_vector` to a factor with ordered level
factor_day <- factor(day_vector, order = TRUE, levels =c('morning', 'midday', 'afternoon', 'evening', 'midnight'))
# Print the new variable
factor_day
## Levels: morning < midday < afternoon < evening < midnight
# Append the line to above code
# Count the number of occurence of each level
summary(factor_day)
# continuous variables
dataset <- mtcars
class(dataset$mpg)
# How to Create a Data Frame
# Create a, b, c, d variables
a <- c(10,20,30,40)
b <- c('book', 'pen', 'textbook', 'pencil_case')
c <- c(TRUE,FALSE,TRUE,FALSE)
d <- c(2.5, 8, 10, 7)
# Join the variables to create a data frame
df <- data.frame(a,b,c,d)
df
# Name the data frame
names(df) <- c('ID', 'items', 'store', 'price')
df
# Print the structure
str(df)
# Slice
df
# Select row 1 in column 2
df[1,2]
# Select Rows 1 to 2
df[1:2,]
# Select Columns 1
df[,1]
# Select Rows 1 to 3 and columns 3 to 4
df[1:3, 3:4]
# Slice with columns name
df[, c('ID', 'store')]
# Append a column to dataframe
# Create a new vector
quantity <- c(10, 35, 40, 5)
# Add `quantity` to the `df` data frame
df$quantity <- quantity
df
quantity <- c(10, 35, 40)
# Add `quantity` to the `df` data frame
df$quantity <- quantity
# Select the column ID
df$ID
# Select price above 5
subset(df, subset = price > 5)
# Vector with numeric from 1 up to 5
vect <- 1:5
# A 2x 5 matrix
mat <- matrix(1:9, ncol = 5)
dim(mat)
# select the 10th row of the built-in R data set EuStockMarkets
df <- EuStockMarkets[1:10,]
# Construct list with these vec, mat, and df:
my_list <- list(vect, mat, df)
my_list
# Print second element of the list
my_list[[2]]
## Built-in Data Frame
PATH <-'C:/Users/ankur/Documents/Data Science/Projects/R/R/prison.csv'
PATH <-'C:\\Users\\ankur\\Documents\\Data Science\\Projects\\R\\R\\prison.csv'
df <- read.csv(PATH)[1:5]
head(df, 5)
PATHH <- file.choose()
# Structure of the data
str(df)
# https://cran.r-project.org/web/packages/tibble/vignettes/tibble.html
library(dplyr)
set.seed(1234)
data_frame <- tibble(
c1 = rnorm(50, 5, 1.5),
c2 = rnorm(50, 5, 1.5),
c3 = rnorm(50, 5, 1.5),
c4 = rnorm(50, 5, 1.5),
c5 = rnorm(50, 5, 1.5)
)
# Sort by c1
df <-data_frame[order(data_frame$c1),]
head(df)
# Sort by c3 and c4
df <-data_frame[order(data_frame$c3, data_frame$c4),]
head(df)
# Sort by c3(descending) and c4(acending)
df <-data_frame[order(-data_frame$c3, data_frame$c4),]
head(df)
# Merge with dplyr()
library(dplyr)
df_primary <- tribble(
~ID, ~y,
"A", 5,
"B", 5,
"C", 8,
"D", 0,
"F", 9)
df_secondary <- tribble(
~ID, ~y,
"A", 30,
"B", 21,
"C", 22,
"D", 25,
"E", 29)
left_join(df_primary, df_secondary, by ='ID')
right_join(df_primary, df_secondary, by = 'ID')
inner_join(df_primary, df_secondary, by ='ID')
full_join(df_primary, df_secondary, by = 'ID')
df_primary <- tribble(
~ID, ~year, ~items,
"A", 2015,3,
"A", 2016,7,
"A", 2017,6,
"B", 2015,4,
"B", 2016,8,
"B", 2017,7,
"C", 2015,4,
"C", 2016,6,
"C", 2017,6)
df_secondary <- tribble(
~ID, ~year, ~prices,
"A", 2015,9,
"A", 2016,8,
"A", 2017,12,
"B", 2015,13,
"B", 2016,14,
"B", 2017,6,
"C", 2015,15,
"C", 2016,15,
"C", 2017,13)
left_join(df_primary, df_secondary, by = c('ID', 'year'))
# Data Cleaning functions
library(tidyr)
# Create a messy dataset
messy <- data.frame(
country = c("A", "B", "C"),
q1_2017 = c(0.03, 0.05, 0.01),
q2_2017 = c(0.05, 0.07, 0.02),
q3_2017 = c(0.04, 0.05, 0.01),
q4_2017 = c(0.03, 0.02, 0.04))
messy
# Reshape the data
tidier <- messy %>%
gather(quarter, growth, q1_2017:q4_2017)
tidier
# Reshape the data
messy_1 <- tidier %>%
spread(quarter, growth)
messy_1
# The separate() function splits a column into two according to a separator.
separate_tidier <-tidier %>%
separate(quarter, c("Qrt", "year"), sep ="_")
head(separate_tidier)
# The unite() function concanates two columns into one.
unit_tidier <- separate_tidier %>%
unite(Quarter, Qrt, year, sep ="_")
head(unit_tidier)
## Merge
# Create origin dataframe(
producers <- data.frame(
surname = c("Spielberg","Scorsese","Hitchcock","Tarantino","Polanski"),
nationality = c("US","US","UK","US","Poland"),
stringsAsFactors=FALSE)
# Create destination dataframe
movies <- data.frame(
surname = c("Spielberg",
"Scorsese",
"Hitchcock",
"Hitchcock",
"Spielberg",
"Tarantino",
"Polanski"),
title = c("Super 8",
"Taxi Driver",
"Psycho",
"North by Northwest",
"Catch Me If You Can",
"Reservoir Dogs","Chinatown"),
stringsAsFactors=FALSE)
# Merge two datasets
m1 <- merge(producers, movies, by.x = "surname")
m1
dim(m1)
# Change name of ` movies ` dataframe
colnames(movies)[colnames(movies) == 'surname'] <- 'name'
# Merge with different key value
m2 <- merge(producers, movies, by.x = "surname", by.y = "name")
# Print head of the data
head(m2)
# Check if data are identical
identical(m1, m2)
# Create a new producer
add_producer <- c('Lucas', 'US')
# Append it to the ` producer` dataframe
producers <- rbind(producers, add_producer)
# Use a partial merge
m3 <-merge(producers, movies, by.x = "surname", by.y = "name", all.x = TRUE)
m3
# Compare the dimension of each data frame
dim(m1)
dim(m2)
dim(m3)
set.seed(123)
# Create the data
x = rnorm(1000)
ts <- cumsum(x)
# Stationary the serie
diff_ts <- diff(ts)
par(mfrow=c(1,2))
# Plot the series
plot(ts, type='l')
plot(diff(ts), type='l')
dt <- cars
# number columns
length(dt)
# number rows
length(dt[,1])
# sequence of number from 44 to 55 both including incremented by 1
x_vector <- seq(45,55, by = 1)
#logarithm
log(x_vector)
#exponential
exp(x_vector)
#squared root
sqrt(x_vector)
#factorial
factorial(x_vector)
speed <- dt$speed
speed
# Mean speed of cars dataset
mean(speed)
# Median speed of cars dataset
median(speed)
# Variance speed of cars dataset
var(speed)
# Standard deviation speed of cars dataset
sd(speed)
# Standardize vector speed of cars dataset
head(scale(speed), 5)
# Quantile speed of cars dataset
quantile(speed)
# Summary speed of cars dataset
summary(speed)
# Write functions
square_function <- function(n)
{
# compute the square of integer `n`
n^2
}
# calling the function and passing value 4
square_function(4)
rm(square_function)
square_function
# Environment Scoping
ls(environment())
# Multi arguments
times <- function(x,y) {
x*y
}
times(2,4)
library(tibble)
# Create a data frame
data_frame <- tibble(
c1 = rnorm(50, 5, 1.5),
c2 = rnorm(50, 5, 1.5),
c3 = rnorm(50, 5, 1.5),
)
# Create c1_norm: rescaling of c1
data_frame$c1_norm <- (data_frame$c1 -min(data_frame$c1))/(max(data_frame$c1)-min(data_frame$c1))
# show the first five values
head(data_frame$c1_norm, 5)
data_frame$c1_norm <- (data_frame$c1 -min(data_frame$c1))/(max(data_frame$c1)-min(data_frame$c1))
data_frame$c2_norm <- (data_frame$c2 - min(data_frame$c2))/(max(data_frame$c2)-min(data_frame$c2))
data_frame$c3_norm <- (data_frame$c3 - min(data_frame$c3))/(max(data_frame$c3)-min(data_frame$c3))
normalize <- function(x){
# step 1: create the nominator
nominator <- x-min(x)
# step 2: create the denominator
denominator <- max(x)-min(x)
# step 3: divide nominator by denominator
normalize <- nominator/denominator
# return the value
return(normalize)
}
normalize(data_frame$c1)
data_frame$c1_norm_function <- normalize (data_frame$c1)
data_frame$c2_norm_function <- normalize (data_frame$c2)
data_frame$c3_norm_function <- normalize (data_frame$c3)
nrow(airquality)
length<- nrow(airquality)
length
total_row <- length*0.8
total_row
split <- 1:total_row
split[1:5]
train_df <- airquality[split, ]
head(train_df)
test_df <- airquality[-split, ]
head(test_df)
split_data <- function(df, train = TRUE){
length<- nrow(df)
total_row <- length *0.8
split <- 1:total_row
if (train ==TRUE){
train_df <- df[split, ]
return(train_df)
} else {
test_df <- df[-split, ]
return(test_df)
}
}
train <- split_data(airquality, train = TRUE)
dim(train)
test <- split_data(airquality, train = FALSE)
dim(test)
# Create vector quantity
quantity <- 25
# Set the is-else statement
if (quantity > 20) {
print('You sold a lot!')
} else {
print('Not enough for today')
}
# Create vector quantiy
quantity <- 10
# Create multiple condition statement
if (quantity <20) {
print('Not enough for today')
} else if (quantity > 20 &quantity <= 30) {
print('Average day')
} else {
print('What a great day!')
}
category <- 'A'
price <- 10
if (category =='A'){
cat('A vat rate of 8% is applied.','The total price is',price *1.08)
} else if (category =='B'){
cat('A vat rate of 10% is applied.','The total price is',price *1.10)
} else {
cat('A vat rate of 20% is applied.','The total price is',price *1.20)
}
# Create fruit vector
fruit <- c('Apple', 'Orange', 'Passion fruit', 'Banana')
# Create the for statement
for ( i in fruit){
print(i)
}
# Create an empty list
list <- c()
# Create a for statement to populate the list
for (i in seq(1, 4, by=1)) {
list[[i]] <- i*i
}
print(list)
# Create a list with three vectors
fruit <- list(Basket = c('Apple', 'Orange', 'Passion fruit', 'Banana'),
Money = c(10, 12, 15), purchase = FALSE)
for (p in fruit)
{
print(p)
}
# Create a matrix
mat <- matrix(data = seq(10, 20, by=1), nrow = 6, ncol =2)
# Create the loop with r and c to iterate over the matrix
for (r in 1:nrow(mat))
for (c in 1:ncol(mat))
print(paste("Row", r, "and column",c, "have values of", mat[r,c]))
#Create a variable with value 1
begin <- 1
#Create the loop
while (begin <= 10){
#See which we are
cat('This is loop number',begin)
#add 1 to the variable begin after each loop
begin <- begin+1
print(begin)
}
set.seed(123)
# Set variable stock and price
stock <- 50
price <- 50
# Loop variable counts the number of loops
loop <- 1
m1 <- matrix(C<-(1:10),nrow=5, ncol=6)
m1
a_m1 <- apply(m1, 2, sum)
a_m1
movies <- c("SPYDERMAN","BATMAN","VERTIGO","CHINATOWN")
movies_lower <-lapply(movies, tolower) #list structure
str(movies_lower)
movies_lower <-unlist(lapply(movies,tolower))
str(movies_lower)
dt <- cars
lmn_cars <- lapply(dt, min)
smn_cars <- sapply(dt, min) #unlist
lmn_cars
smn_cars
lmxcars <- lapply(dt, max)
smxcars <- sapply(dt, max)
lmxcars
smxcars
avg <- function(x) {
( min(x) + max(x) ) / 2}
fcars <- sapply(dt, avg)
fcars
below_ave <- function(x) {
ave <- mean(x)
return(x[x > ave])
}
dt_s<- sapply(dt, below_ave)
dt_l<- lapply(dt, below_ave)
identical(dt_s, dt_l)
data(iris)
tapply(iris$Sepal.Width, iris$Species, median)
install.packages("readxl")
library(readxl) # install.packages("readxl") or install.packages("tidyverse")
excel_sheets(readxl_example("datasets.xls"))
# To load all sheets in a workbook, use lapply
path <- readxl_example("datasets.xls")
lapply(excel_sheets(path), read_excel, path = path)
# Store the path of `datasets.xlsx`
example <- readxl_example("datasets.xlsx")
# Import the spreadsheet
df <- read_excel(example)
# Count the number of columns
length(df)
example <- readxl_example("datasets.xlsx")
quake <- read_excel(example, sheet = "quakes")
quake_1 <-read_excel(example, sheet = 4)
identical(quake, quake_1)
# Read the first five row: with header
iris <-read_excel(example, n_max =5, col_names =TRUE)
iris
# Read the first five row: without header
iris_no_header <-read_excel(example, n_max =5, col_names =FALSE)
iris_no_header
# Read rows A1 to B5
example_1 <-read_excel(example, range = "A1:B5", col_names =TRUE)
dim(example_1)
# Read rows 1 to 5
example_2 <-read_excel(example, range =cell_rows(1:5),col_names =TRUE)
dim(example_2)
iris_na <-read_excel(example, na ="setosa")
sum(is.na(iris_na))
# Titanic
setwd("C:/Users/ankur/Documents/Data Science/Projects/R/R")
PATH <- "titanic.csv"
df_titanic <- read.csv(PATH, sep = ",")
# Return the column names containing missing observations
list_na <- colnames(df_titanic)[ apply(df_titanic, 2, anyNA) ]
list_na
library(dplyr)
# Exclude the missing observations
df_titanic_drop <-df_titanic %>%
na.omit()
dim(df_titanic_drop)
dim(df_titanic)
# Create mean
average_missing <- apply(df_titanic[,colnames(df_titanic) %in% list_na],
2,
mean,
na.rm = TRUE)
average_missing# Create a new variable with the mean and median
df_titanic_replace <- df_titanic %>% #create new datasets
mutate(replace_mean_age = ifelse(is.na(age), average_missing[1], age),
replace_mean_fare = ifelse(is.na(fare), average_missing[2], fare))
df_titanic
sum(is.na(df_titanic_replace$Age))
sum(is.na(df_titanic_replace$replace_mean_age))
median_missing <- apply(df_titanic[,colnames(df_titanic) %in% list_na],
2,
median,
na.rm = TRUE)
df_titanic_replace <- df_titanic %>%
mutate(replace_median_age = ifelse(is.na(Age), median_missing[1], Age),
replace_median_fare = ifelse(is.na(Fare), median_missing[2], Fare))
head(df_titanic_replace)
directory <-getwd()
directory
# Create data frame
library(dplyr)
df <-mtcars %>%
select(mpg, disp, gear) %>%
group_by(gear) %>%
summarize(mean_mpg = mean(mpg), mean_disp = mean(disp))
df
write.csv(df, "table_car.csv")
write.csv2(df, "table_car2.csv") #separate the rows with a semicolon.
# conda install -c r r-xlsx
library(xlsx)
write.xlsx(df, "table_car.xlsx")
install.packages("googledrive")
library(googledrive)
# drive_upload(file, path = NULL, name = NULL)
drive_upload("table_car.csv", name ="table_car")
drive_browse("table_car")
x <-drive_get("table_car")
as_id(x)
x
install.packages('rdrop2')
library(rdrop2)
drop_auth()
drop_create('my_first_drop')
drop_upload('table_car.csv', path = "my_first_drop")
# Pearson Correlation
# Spearman Rank Correlation
library(dplyr)
PATH <-"C:/Users/ankur/Documents/Data Science/Projects/R/R/british_household.csv"
data <-read.csv(PATH)
str(data)
data <-read.csv(PATH)
filter(income < 500)
mutate(log_income = log(income),
log_totexp = log(totexp),
children_fac = factor(children, order = TRUE, labels = c("No", "Yes")))
select(-c(X,X.1, children, totexp, income))
glimpse(data)
library("Hmisc")
data_rcorr <-as.matrix(data[, 1: 9])
mat_2 <-rcorr(data_rcorr) #p-value
# mat_2 <-rcorr(as.matrix(data)) returns the same output
mat_2
p_value <-round(mat_2[["P"]], 3)
p_value
install.packages("GGally")
library(GGally)
ggcorr(data)
ggcorr(data,
nbreaks = 6,
low = "steelblue",
mid = "white",
high = "darkred",
geom = "circle")
ggcorr(data,
nbreaks = 6,
label = TRUE,
label_size = 3,
color = "grey50")
ggpair(df, columns = 1: ncol(df), title = NULL,
upper = list(continuous = "cor"),
lower = list(continuous = "smooth"),
mapping = NULL)
library(ggplot2)
ggpairs(data, columns = c("log_totexp", "log_income", "age", "wtrans"), title = "Bivariate analysis of revenue expenditure by the British household", upper = list(continuous = wrap("cor",
size = 3)),
lower = list(continuous = wrap("smooth",
alpha = 0.3,
size = 0.1)),
mapping = aes(color = children_fac))
# Basic scatter plot
ggplot(mtcars, aes(x = drat, y = mpg)) +
geom_point()
# scatter plot with groups
ggplot(mtcars, aes(x = mpg, y = drat)) +
geom_point(aes(color = factor(gear)))
# change axis
ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +
geom_point(aes(color = factor(gear)))
# Scatter plot with fitted values
my_graph <- ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +
geom_point(aes(color = factor(gear))) +
stat_smooth(method = "lm",
col = "#C42126",
se = FALSE,
size = 1)
my_graph
my_graph +
labs(
title = "Plot Mile per hours and drat, in log"
)
A <-2010
paste("The first year is", A)
B <-2018
paste("The first year is", A, "and the last year is", B)
mean_mpg <- mean(mtcars$mpg)
my_graph + labs(
title = paste("Plot Mile per hours and drat, in log. Average mpg is", mean_mpg)
)
my_graph +
labs(
title =
"Relation between Mile per hours and drat",
subtitle =
"Relationship break down by gear class",
caption = "Authors own computation"
)
my_graph +
labs(
x = "Drat definition",
y = "Mile per hours",
color = "Gear",
title = "Relation between Mile per hours and drat",
subtitle = "Relationship break down by gear class",
caption = "Authors own computation"
)
seq(0, 12,4)
my_graph +
scale_x_continuous(breaks = seq(1, 3.6, by = 0.2)) +
scale_y_continuous(breaks = seq(1, 1.6, by = 0.1)) +
labs(
x = "Drat definition",
y = "Mile per hours",
color = "Gear",
title = "Relation between Mile per hours and drat",
subtitle = "Relationship break down by gear class",
caption = "Authors own computation"
)
my_graph +
theme_dark() +
labs(
x = "Drat definition, in log",
y = "Mile per hours, in log",
color = "Gear",
title = "Relation between Mile per hours and drat",
subtitle = "Relationship break down by gear class",
caption = "Authors own computation"
)
directory <-getwd()
directory
my_graph +
theme_dark() +
labs(
x = "Drat definition, in log",
y = "Mile per hours, in log",
color = "Gear",
title = "Relation between Mile per hours and drat",
subtitle = "Relationship break down by gear class",
caption = "Authors own computation"
)
ggsave("my_fantastic_plot.png")
# Run this code to create the function
open_folder <- function(dir) {
if (.Platform['OS.type'] == "windows") {
shell.exec(dir)
} else {
system(paste(Sys.getenv("R_BROWSER"), dir))
}
}
open_folder("C:/Users/ankur/Documents/Data Science/Projects/R/R")
# create box plot
library(dplyr)
library(ggplot2)
# Step 1
data_air <- airquality %>%
#Step 2
select(-c(Solar.R, Temp)) %>%
#Step 3
mutate(Month = factor(Month, order = TRUE, labels = c("May", "June", "July", "August", "September")),
#Step 4
day_cat = factor(ifelse(Day < 10, "Begin", ifelse(Day < 20, "Middle", "End"))))
data_air
glimpse(data_air)
data_air_nona <-data_air %>% na.omit()
# Store the graph
box_plot <- ggplot(data_air_nona, aes(x = Month, y = Ozone))
# Add the geometric object box plot
box_plot +
geom_boxplot()
box_plot +
geom_boxplot()+
coord_flip()
box_plot +
geom_boxplot(outlier.colour = "red",
outlier.shape = 2,
outlier.size = 3) +
theme_classic()
box_plot +
geom_boxplot() +
stat_summary(fun.y = mean,
geom = "point",
size = 3,
color = "steelblue") +
theme_classic()
box_plot +
geom_boxplot() +
geom_dotplot(binaxis = 'y',
dotsize = 1,
stackdir = 'center') +
theme_classic()
ggplot(data_air_nona, aes(x = Month, y = Ozone, color = Month)) +
geom_boxplot() +
theme_classic()
ggplot(data_air_nona, aes(Month, Ozone)) +
geom_boxplot(aes(fill = day_cat)) +
theme_classic()
box_plot +
geom_boxplot() +
geom_jitter(shape = 15,
color = "steelblue",
position = position_jitter(width = 0.21)) +
theme_classic()
box_plot +
geom_boxplot() +
geom_point(shape = 5,
color = "steelblue") +
theme_classic()
box_plot +
geom_boxplot(notch = TRUE) +
theme_classic()
# Bar chart
library(ggplot2)
# Most basic bar chart
ggplot(mtcars, aes(x = factor(cyl))) +
geom_bar()
# Change the color of the bars
ggplot(mtcars, aes(x = factor(cyl))) +
geom_bar(fill = "coral") +
theme_classic()
grDevices::colors()
# Change intensity
ggplot(mtcars,
aes(factor(cyl))) +