-
Notifications
You must be signed in to change notification settings - Fork 0
/
R_Summer2024.R
1934 lines (1557 loc) · 45.3 KB
/
R_Summer2024.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
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
# ----- 05/12/2024------------------------------------------------------------------------------
fruit<- c("apple", "banana","lemon")
price<- c(10, 20, 30)
df<- data.frame(fruit, price) # okay
#df<- data.frame(c(fruit, price)) # it gives one column
str(df)
df[1,2]
df[1,]
df[,1]
df$fruit[2]
mean(df$price[2:3])
#pipe
library(dplyr)
df %>% select(fruit) # %>% read as "then do"
df %>% select(fruit)%>% slice(2:3)
df$fruit
df%>% select(price) # output is columns
df%>% select(price) %>% mean() # gives NA as mean function only works on vector
df%>% pull(price) # output is vector
df%>% pull(price) %>% mean() # gives right answer
df%>% order(price, decreasing = False) # gives error as order works on vector only
df %>% pull(price)%>% order() # now works as pull() creates vector
df %>% arrange(desc(price)) # arrange works on dataframe
df %>% arrange(desc(price)) %>% slice(2) # to find the nth one in dataframe format
df %>% arrange(desc(price)) %>% slice(2) %>% pull(price) # gives only price
# -----------05/13/2024------------------------------------------------------------------
#rep functions
rep(3,3)
rep(1:3,3)
rep(NA,3)
rep(rnorm(2),3)
#sample function
sample(5)
sample(1:5)
a<- c(2,3,4,4,5,3,3,4,3,9,0)
#sample(data, size, replace=T/F)
sample(a,3, replace= TRUE)
#seq function
seq(1:8)
seq(1,20,2.5)
seq(0,1,0.2)
#replace function
df<- data.frame(x= c(1,2,3,4), y= c(5,6,7,8))
new_df<- replace(df$y, df$y>5, 10)
new_df
new_df<- replace(df$y, c(2,4), c(100,200))
new_df
new_df<- replace(df$y, 3, .9)
new_df
#----------5/14/2024--------------------------------------------------------------------
#mutate function (from dplyr package to introduce/update variables in dataset)
df<- data.frame(x= c(1,2,3,4), y= c(5,6,7,8))
library(dplyr)
df<- df %>% mutate(z=x+y)
df
df<- df%>% mutate(x=x+1)
df
#filter function
df<- df%>% filter(z>9)
df
df%>% filter(z>9)%>%select(x,y)
#subset function
df<- data.frame(x= c(1,2,3,4), y= c(5,6,7,8))
df<- df %>% mutate(z=x+y)
df<- df%>% filter(z>9)
df<- df%>% subset(z>9)
df
df1<- df%>% filter(x==1 & z==6) #they give same results, differ in speed for large datasets and subset require
# no library.
df1<- df%>% subset(x==1 & z==6)
#----------5/15/2024-----------------------------------------------------------------------
library(ggplot2)
library(dplyr)
mtcars %>% ggplot(aes(x=hp, y=mpg, col = disp, ))+ geom_point()+
labs(title = "mpg vs hp", x= "hp", y ="mpg")
#adding facet_grid
mtcars %>% ggplot(aes(x=hp, y=mpg, col = disp, shape=factor(cyl)))+ geom_point()+
labs(title = "mpg vs hp", x= "hp", y ="mpg") +facet_grid(.~cyl)
#adding line
mtcars %>% ggplot(aes(x=hp, y=mpg, col = disp, shape=factor(cyl)))+ geom_point()+
labs(title = "mpg vs hp", x= "hp", y ="mpg") +facet_grid(.~cyl) +geom_smooth(method=lm, col="red")
#----------5/16/2024----------------------------------------------------------------------------
# for loop from geeksforgeeks
for (i in 1: 4)
{
print(i ^ 2)
}
# nested for loop
for (i in 1:3)
{
for (j in 1:i)
{
print(i * j)
}
}
# break in for loop
for (i in c(3, 6, 23, 19, 0, 21))
{
if (i == 0)
{
break
}
print(i)
}
print("Outside Loop")
# next in for loop
for (i in c(3, 6, 23, 19, 0, 21))
{
if (i == 0)
{
next
}
print(i)
}
print('Outside Loop')
# create a matrix of data
mat <- matrix(rnorm(100), ncol = 5)
#mat<- as.data.frame(mat)
# set up the plot layout
par(mfrow = c(2, 3))
# loop over columns of the matrix
for (i in 1:5) {
# create a histogram for each column
hist(mat[, i], main = paste("Column", i), xlab = "Values", col = "lightblue")
}
#-----------5/17/2024---------------------------------------------------------------------------------------------
# functions in R from https://www.tutorialspoint.com/r/r_functions.htm
# Create user-defined function with arguments.
f1 <- function(a,b,c) {
result <- a * b + c
print(result)
}
# Call the function by position of arguments.
f1(5,3,11)
# Create a function with arguments.
f2 <- function(a = 3, b = 6) {
result <- a * b
print(result)
}
# Call the function without giving any argument.
f2()
# Call the function with giving new values of the argument.
f2(9,5)
# Create a function with arguments.
f3 <- function(a, b) {
print(a^2)
print(a)
print(b)
}
# Evaluate the function without supplying one of the arguments.
f3(6)
# Create a function to print squares of numbers in sequence.
f4 <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
f4(10)
# Create a function to print squares of numbers in sequence.
f5 <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
# Call the function new.function supplying 6 as an argument.
f5(6)
# Create a function without an argument.
f6 <- function() {
for(i in 1:5) {
print(i^2)
}
}
# Call the function without supplying an argument.
f6()
#---------------5/18/2024-------------------------------------------------------------------------
# map function in R from https://www.statology.org/map-function-in-r/
# The map() function from the purrr package in R can be used to apply some
#function to each element in a vector or list and return a list as a result.
#
# This function uses the following basic syntax:
#
# map(.x, .f)
#
# where:
#
# .x: A vector or list
# .f: A function
library(purrr)
#define vector
data <- 1:3
#apply rnorm() function to each value in vector
data %>%
map(function(x) rnorm(5, x))
#define vector
data <- c(2, 4, 10, 15, 20)
#calculate square of each value in the vector
data %>%
map(function(x) x^2)
#define list of vectors
data <- list(c(1, 2, 3),
c(4, 5, 6),
c(7, 8, NA))
#calculate mean value of each vector in list
data %>%
map(mean, na.rm=TRUE)
#------------------5/19/2024----------------------------------------------------------------------
# if statement from https://www.geeksforgeeks.org/r-if-statement/
# R program to illustrate if statement
# assigning value to variable a
a <- -5
# condition
if(a > 0)
{
print("Positive Number") # Statement
}else{
print("-ve number")
}
x <- 12
# Condition
if (x > 20)
{
print("12 is less than 20") # Statement
}
print("Hello World")
# nested if else
x <- 0
if (x < 0) {
print("Negative number")
} else if (x > 0) {
print("Positive number")
} else
print("Zero")
#-----------------5/20/2024----------------------------------------------------------------------
# while loop from https://www.geeksforgeeks.org/r-while-loop/
# R program to illustrate while loop
result <- c("Hello World")
i <- 1
# test expression
while (i < 6) {
print(result)
# update expression
i = i + 1
}
# R program to illustrate while loop
result <- 1
i <- 1
# test expression
while (i < 6) {
print(result)
# update expression
i = i + 1
result = result + 1
}
# R program to illustrate while loop
result <- c("Hello World")
i <- 1
# test expression
while (i < 6) {
print(result)
if( i == 3){
break}
# update expression
i = i + 1
}
# while loop with next
# Set an initial value for a variable
x <- 1
# Loop while x is less than 10
while (x < 10) {
if (x == 3) {
# Skip iteration when x is 3
x <- x + 1
next
}
print(paste("The current value of x is:", x))
x <- x + 1
}
# Print a message after the loop has finished
print("The loop has ended.")
# while loop with if
x <- 1
while (x <= 10) {
if (x %% 2 == 0) {
print(paste(x, "is even"))
} else {
print(paste(x, "is odd"))
}
x <- x + 1
}
#------------------------5/21/2024------------------------------------------------------------
# with() function
# Create a sample data frame
# The with() function takes two main arguments:
# data: A data frame or list in which to look for variables.
# expr: An expression to be evaluated within the context of the data frame or list.
# Syntax: with(data, expr)
df <- data.frame(
a = c(1, 2, 3, 4, 5),
b = c(6, 7, 8, 9, 10)
)
# Using with() to calculate mean and standard deviation
result <- with(df, {
mean_a <- mean(a)
sd_a <- sd(a)
mean_b <- mean(b)
sd_b <- sd(b)
list(mean_a = mean_a, sd_a = sd_a, mean_b = mean_b, sd_b = sd_b)
})
print(result)
# plotting with with()
# Create a sample data frame
df <- data.frame(
x = rnorm(100),
y = rnorm(100)
)
# Using with() to plot data
with(df, {
plot(x, y, main = "Scatter plot of x and y", xlab = "x", ylab = "y")
})
# using with() in more complex scenarios
# Create a sample data frame
df <- data.frame(
x = 1:10,
y = 2 * (1:10) + rnorm(10)
)
# Using with() for multiple operations
result <- with(df, {
lm_result <- lm(y ~ x) # Fit a linear model
summary(lm_result) # Get the summary of the model
})
print(result)
#-----------------------------5/22/2024------------------------------------------------------
# nest() function in R
#The nest() function in R, which is part of the tidyr package, is used to convert
# a data frame into a nested data frame. This is particularly useful for managing grouped data,
# where you want to keep related observations together in a list-column.
# Nested data frames are useful for performing operations within groups
# and can help simplify complex data manipulation tasks.
# Syntax:
#nest(.data, ..., .key = "data")
# .data: A data frame.
# ...: Columns to nest, typically columns that are not being grouped.
# .key: Name of the new list-column created by nesting.
library(tidyr)
# Example data frame
df <- data.frame(
group = rep(c("A", "B"), each = 3),
value1 = 1:6,
value2 = 6:1
)
print(df)
# Nesting the data frame by 'group'
nested_df <- df %>% nest(data = c(value1, value2))
print(nested_df)
# Access the nested data for group A
nested_df$data[[1]]
# operations on nested data
library(purrr)
# Calculate the mean of value1 and value2 for each group
library(dplyr)
means <- nested_df %>%
mutate(mean_values = map(data, ~ summarise(.x, mean_value1 = mean(value1), mean_value2 = mean(value2))))
print(means)
#
nested_df<-df %>% group_by(group)%>%nest()
nested_df
result <- nested_df %>%
+ mutate(mean_values = map(data, ~ tibble(
+ mean_value1 = mean(.x$value1),
+ mean_value2 = mean(.x$value2)
+ )))
result
result%>%unnest(mean_values)
# you can unnest if needed
# Unnesting the means
unnested_means <- means %>% unnest(mean_values)
print(unnested_means)
#-------------------------------5/23/2024---------------------------------------------------------
#Matrix calculations in R from https://ashki23.github.io/r-functions.html
M = matrix(c(4,4,-2,2,6,2,2,8,4), 3, 3)
N = matrix(c(3,2,1,0,1,-2,4,5,6), 3, 3)
b = matrix(c(0,0,1))
dim(M)
## [1] 3 3
diag(M)
## [1] 4 6 4
diag(3)
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
Matrix::Diagonal(3, c(.1,.2,.3))
## [,1] [,2] [,3]
## [1,] 0.1 . .
## [2,] . 0.2 .
## [3,] . . 0.3
isSymmetric(M)
## [1] FALSE
M %*% N # matrix multiplication
## [,1] [,2] [,3]
## [1,] 18 -2 38
## [2,] 32 -10 94
## [3,] 2 -6 26
identical(M, N) #Test matrices for exact equality
## [1] FALSE
t(M) #transpose of matrix
## [,1] [,2] [,3]
## [1,] 4 4 -2
## [2,] 2 6 2
## [3,] 2 8 4
det(M) # determinant of matrix
## [1] 8
qr(M) #QR decompostion of matrix
## $qr
## [,1] [,2] [,3]
## [1,] -6.0000000 -4.6666667 -5.3333333
## [2,] 0.6666667 -4.7140452 -7.4481914
## [3,] -0.3333333 0.7071068 0.2828427
##
## $rank
## [1] 3
##
## $qraux
## [1] 1.6666667 1.7071068 0.2828427
##
## $pivot
## [1] 1 2 3
##
## attr(,"class")
## [1] "qr"
solve(M) # Inverse of M
## [,1] [,2] [,3]
## [1,] 1.0 -0.5 0.5
## [2,] -4.0 2.5 -3.0
## [3,] 2.5 -1.5 2.0
solve(M,b) # Solve a system
## [,1]
## [1,] 0.5
## [2,] -3.0
## [3,] 2.0
eigen(M) # spectral decompostion of matrix
## eigen() decomposition
## $values
## [1] 9.4185507 4.3878731 0.1935761
##
## $vectors
## [,1] [,2] [,3]
## [1,] 0.3994272 -0.6725085 0.1632537
## [2,] 0.8980978 -0.5844552 -0.8354557
## [3,] 0.1840605 0.4540313 0.5247494
#-----------------------------------5/24/2024-------------------------------
# split and merge data
data = expand.grid(meat = c("grade-1","grade-2","grade-3"), food = c("burger", "steak", "pizza"))
data$value = round(rnorm(nrow(data)), 2)
data
## meat food value
## 1 grade-1 burger 0.33
## 2 grade-2 burger -0.60
## 3 grade-3 burger 0.85
## 4 grade-1 steak 0.92
## 5 grade-2 steak 1.19
## 6 grade-3 steak 0.77
## 7 grade-1 pizza -0.60
## 8 grade-2 pizza -0.39
## 9 grade-3 pizza 0.88
data_by_food = split(data, data$food)
data_by_food
## $burger
## meat food value
## 1 grade-1 burger 0.33
## 2 grade-2 burger -0.60
## 3 grade-3 burger 0.85
##
## $steak
## meat food value
## 4 grade-1 steak 0.92
## 5 grade-2 steak 1.19
## 6 grade-3 steak 0.77
##
## $pizza
## meat food value
## 7 grade-1 pizza -0.60
## 8 grade-2 pizza -0.39
## 9 grade-3 pizza 0.88
class(data_by_food)
## [1] "list"
x = data.frame(ID = 1:6, Product = c(rep("TV", 3), rep("Mobile", 3)))
x
## ID Product
## 1 1 TV
## 2 2 TV
## 3 3 TV
## 4 4 Mobile
## 5 5 Mobile
## 6 6 Mobile
y = data.frame(ID = c(2,4,6), Made_in = c(rep("Japan", 2), rep("China", 1)))
y
## ID Made_in
## 1 2 Japan
## 2 4 Japan
## 3 6 China
merge(x, y, by = "ID") # Inner join
## ID Product Made_in
## 1 2 TV Japan
## 2 4 Mobile Japan
## 3 6 Mobile China
merge(x, y, by = "ID", all = TRUE) # Full outer join
## ID Product Made_in
## 1 1 TV <NA>
## 2 2 TV Japan
## 3 3 TV <NA>
## 4 4 Mobile Japan
## 5 5 Mobile <NA>
## 6 6 Mobile China
#------------------------------------5/25/2024-----------------------------------
# join in R
library(dplyr)
# First data frame
df1 <- data.frame(
id = c(1, 2, 3, 4),
name = c("Alice", "Bob", "Charlie", "David")
)
# Second data frame
df2 <- data.frame(
id = c(2, 3, 4, 5),
score = c(90, 85, 88, 92)
)
#print("df1:")
print(df1)
#print("df2:")
print(df2)
# Left join
left_joined_df <- left_join(df1, df2, by = "id")
#print("Left Joined Data Frame:")
print(left_joined_df)
# Inner join
inner_joined_df <- inner_join(df1, df2, by = "id")
print("Inner Joined Data Frame:")
print(inner_joined_df)
#Example with tibble
# Create data frames
df1 <- tibble(
id = c(1, 2, 3, 4),
name = c("Alice", "Bob", "Charlie", "David")
)
df2 <- tibble(
id = c(2, 3, 4, 5),
score = c(90, 85, 88, 92)
)
# Left join
left_joined_df <- df1 %>% left_join(df2, by = "id")
#print("Left Joined Data Frame:")
print(left_joined_df)
# Inner join
inner_joined_df <- df1 %>% inner_join(df2, by = "id")
print("Inner Joined Data Frame:")
print(inner_joined_df)
#---------------------------------5/26/2024-------------------------------------
# difference between data.frame() and tibble()
#
# data.frame() and tibble() are both functions in R for creating data frames,
# but they have some differences in terms of functionality and behavior. data.frame()
# is a base R function, while tibble() is part of the tibble package,
# which is part of the tidyverse suite of packages. Here are the key differences:
# data.frame(): By default, it prints the entire data frame.
# tibble(): Prints a more compact and readable version of the data frame,
# showing only the first 10 rows and the columns
# that fit on the screen. This can be very helpful for large data frames.
#printing
df <- data.frame(x = 1:20, y = rnorm(20))
print(df)
library(tidyverse)
tb <- tibble(x = 1:20, y = rnorm(20))
print(tb)
#Subsetting
# data.frame
df <- data.frame(x = 1:3, y = 4:6)
print(df$x) # Returns a vector
# tibble
tb <- tibble(x = 1:3, y = 4:6)
print(tb$x) # Returns a tibble
# Column Data Types
# data.frame(): Converts character vectors to factors by default.
# tibble(): Keeps character vectors as character vectors.
df <- data.frame(x = c("a", "b", "c"))
str(df) # x is a factor
tb <- tibble(x = c("a", "b", "c"))
str(tb) # x is a character vector
# Handling Non-Syntactic Names
# data.frame(): Automatically converts non-syntactic names to syntactic names.
# tibble(): Allows non-syntactic names (e.g., names with spaces or special characters)
# but you need to use backticks to refer to them.
df <- data.frame(`a b` = 1:3)
print(names(df)) # Converts to syntactic names
tb <- tibble(`a b` = 1:3)
print(names(tb)) # Keeps non-syntactic names
print(tb$`a b`) # Access using backticks
#--------------------------------5/27/2024------------------------------
# Monte Carlo simulation to estimate the value of π
# Number of random points to generate
n <- 10000
# Generate random points (x, y) in the unit square [0, 1] x [0, 1]
x <- runif(n)
y <- runif(n)
# Calculate the distance of each point from the origin (0, 0)
distance <- sqrt(x^2 + y^2)
# Count the number of points inside the quarter circle (distance <= 1)
inside_circle <- sum(distance <= 1)
# Estimate the value of π
# np= number of points
# ratio of np in quarter circle to np of unit square = ratio of area of quarter circle to area unit area
# inside_circle/ n = (pi/4)/1
pi_estimate <- (inside_circle / n) * 4
# Print the estimated value of π
print(paste("Estimated value of π:", pi_estimate))
# Optional: visualize the points and the quarter circle
plot(x, y, col = ifelse(distance <= 1, "blue", "yellow"), asp = 1,
main = paste("Monte Carlo Simulation (n =", n, ")"),
xlab = "x", ylab = "y")
#symbols(0, 0, circles = 1, add = TRUE, inches = FALSE, border = "red")
library(plotrix)
draw.circle(0, 0, 1, border = "green")
#-------------------------------5/28/2024---------------------------------------------------
# draw different shapes
#triangle
x<- c(0, 1, 0.5) #ordering of coordinates does not matter for triangle
y<- c(0, 0, 1)
plot(x,y)
polygon(x,y)
x<- c(0, .5, 1)
y<- c(0, 1, 0)
plot(x,y)
polygon(x,y)
#tetragon
# sequence of coordinates matter here
x<- c(1, 1, 2, 2) #ordering of coordinates does not create close shape here
y<- c(0, 1, 0, 1)
plot(x,y)
polygon(x,y)
x<- c(1, 2, 2, 1) # here the ordering or sequence of coordinates create close shape
y<- c(0, 0, 1, 1)
plot(x,y)
polygon(x,y)
#---------------------5/29/2024-----------------------------------------
# draw a line
x<- c(2,0)
y<- c(4,5)
plot(x,y)
polygon(x,y)
#draw a circle
symbols(0,0,.75, inches = FALSE) # without inches = False the circles will be same size.
symbols(0,0,.5, inches = FALSE)
#-------------------------------5/30/2024-----------------------------------
#draw a cube
# Install and load the rgl package
if (!require("rgl")) {
install.packages("rgl")
library(rgl)
}
# Define the vertices of a cube
vertices <- rbind(
c(0, 0, 0),
c(1, 0, 0),
c(1, 1, 0),
c(0, 1, 0),
c(0, 0, 1),
c(1, 0, 1),
c(1, 1, 1),
c(0, 1, 1)
)
# Define the edges of the cube
edges <- rbind(
c(1, 2), c(2, 3), c(3, 4), c(4, 1), # bottom square
c(5, 6), c(6, 7), c(7, 8), c(8, 5), # top square
c(1, 5), c(2, 6), c(3, 7), c(4, 8) # vertical edges
)
# Plot the cube
open3d()
plot3d(vertices, type = "s", size = 0.1, xlab = "X", ylab = "Y", zlab = "Z")
for (i in 1:nrow(edges)) {
segments3d(rbind(vertices[edges[i, 1], ], vertices[edges[i, 2], ]), col = "blue")
}
#----------------------------5/31/2024---------------------------------------------------------------
#datetime object in R
# Create a Date object
my_date <- as.Date("2024-05-25")
print(my_date)
# Create a POSIXct object
my_datetime <- as.POSIXct("2024-05-25 12:34:56")
print(my_datetime)
# Create a POSIXlt object
my_datetime_lt <- as.POSIXlt("2024-05-25 12:34:56")
print(my_datetime_lt)
print(my_datetime_lt$sec) # seconds
print(my_datetime_lt$min) # minutes
print(my_datetime_lt$hour) # hours
print(my_datetime_lt$mday) # day of the month
print(my_datetime_lt$mon) # months since January (0-11)
print(my_datetime_lt$year) # years since 1900
print(my_datetime_lt$wday) # day of the week (0-6, Sunday is 0)
print(my_datetime_lt$yday) # day of the year (0-365)
print(my_datetime_lt$isdst)
#---------------------------6/1/2024-------------------------------------
#use of Lubridate
# Install and load the lubridate package
if (!require("lubridate")) {
install.packages("lubridate")
library(lubridate)
}
# Parse a date-time string
my_datetime <- ymd_hms("2024-05-25 12:34:56")
print(my_datetime)
# Extract components
year <- year(my_datetime)
month <- month(my_datetime)
day <- day(my_datetime)
hour <- hour(my_datetime)
minute <- minute(my_datetime)
second <- second(my_datetime)
print(paste("Year:", year))
print(paste("Month:", month))
print(paste("Day:", day))
print(paste("Hour:", hour))
print(paste("Minute:", minute))
print(paste("Second:", second))
#-------------------------------------------2/6/2024---------------------------
#while loop in R
#basic structure
while (condition) {
# Code to execute
}
# Initialize a counter variable
counter <- 1
# Start the while loop
while (counter <= 5) {
# Print the current value of the counter
print(counter)
# Increment the counter by 1
counter <- counter + 1
}
# Initialize a counter variable
counter <- 1
# Start the while loop
while (TRUE) {
# Print the current value of the counter
print(counter)
# Increment the counter by 1
counter <- counter + 1
# Break the loop if counter exceeds 5
if (counter > 5) {
break
}
}
# Initialize a counter variable
counter <- 1
# Start the while loop
while (counter <= 5) {
# Skip even numbers
if (counter %% 2 == 0) {
counter <- counter + 1
next
}
# Print the current value of the counter
print(counter)
# Increment the counter by 1
counter <- counter + 1
}
#---------------------------------------------3/6/2024-------------------------------
#binary search in R
# Binary search function
binary_search <- function(vec, target) {
left <- 1
right <- length(vec)
while (left <= right) {
mid <- floor((left + right) / 2)
if (vec[mid] == target) {
return(mid) # Target found, return its index
} else if (vec[mid] < target) {
left <- mid + 1 # Narrow search to upper half
} else {
right <- mid - 1 # Narrow search to lower half
}
}
return(-1) # Target not found
}
# Sorted vector
sorted_vec <- c(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
# Target value to search
target_value <- 7
# Perform binary search
result <- binary_search(sorted_vec, target_value)
# Print result
if (result != -1) {
print(paste("Target found at index:", result))
} else {
print("Target not found")
}
#-----------------------------------------------6/4/2024---------------------
# linear search in R
# Define the linear search function