-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorkshop2.R
More file actions
108 lines (83 loc) · 1.63 KB
/
Workshop2.R
File metadata and controls
108 lines (83 loc) · 1.63 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
arr <- 1:10
# array with 20 elements each with 2
arrTemp <- array(2,20)
arrTemp
# array with 12 elements, repeating sequence of 4,5,6
array(c(4,5,6), 12)
array(c(1,2,3,5,6), 12)
# matrix with 1 row with 5 4s
array(4,c(1,5))
array(4,c(3,5)) # 3 rows with 5 4s
length(table(c(1,2)))
strsplit("string",split = "")
c("s","t")
# built in functions
mean(arr)
median(arr)
length(arr)
min(arr)
prod(arr)
sum(arr)
sum(arr ^ 2) # squares every element and then sums them
arr2 <- c(5, 4, 3, 2, 1)
arr + arr2
arr * arr2
sum(arr * arr2)
prod(arr) ^ (1 / length(arr2))
# custom functions ==========
my.mean <- function(arr) {
output <- sum(arr) / length(arr)
output
}
my.geometricMean <- function (arr) {
output <- prod(arr) ^ (1 / length(arr))
output
}
my.harmonicMean <- function(arr) {
output <- length(arr) / sum(arr ^ (-1))
output
}
my.harmonicMean2 <- function(arr) {
output <- length(arr) / sum(1 / arr)
output
}
# conditions
my.arrayLength <- function(arr) {
if (length(arr) >= 10) {
"array is larger than 10"
}
else if (length(arr) >= 5) {
"array is larger than 5"
}
else {
"array is less than 5"
}
}
# custom functions end ===========
mean(arr)
my.mean(arr)
my.geometricMean(arr)
my.harmonicMean(arr)
my.harmonicMean2(arr)
my.harmonicMean(c(9, 13, 2))
my.geometricMean(c(9, 13, 2))
# rounding results
x <- c(0.6, 0.9, 0.26, 0.7)
round(my.geometricMean(x), 3)
round(my.mean(x), 3)
round(my.harmonicMean(x), 3)
# misc
table(arr)
unique(c(1, 2, 3, 3))
# conditions
my.arrayLength(arr)
my.arrayLength(c(1, 2, 3, 4))
# For loop
for (a in arr) {
print(a)
}
for (i in 1:11) {
print(arr[i])
}
arr[0]
arr[1]