-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab2.R
240 lines (199 loc) · 4.85 KB
/
lab2.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
rm(list=ls())
name <- "Rabnawaz Jansher"
liuid <- "rabsh696"
library(markmyassignment)
# Set assignment Path
lab_path <- "https://raw.githubusercontent.com/STIMALiU/AdvRCourse/master/Labs/Tests/lab2.yml"
set_assignment(lab_path)
#1.1.1
sheldon_game <- function(player1, player2)
{
alt <- c("rock", "lizard", "spock", "scissors", "paper")
stopifnot(player1 %in% alt, player2 %in% alt)
alt1 <- which(alt %in% player1)
alt2 <- which(alt %in% player2)
if(alt1 == alt2)
return ("Draw!")
if(any(alt1+c(1,3) %% 5== alt2))
return("Player 1 wins!")
else
return("Player 2 wins!")
}
sheldon_game("spock","scissors")
mark_my_assignment(tasks = "sheldon_game")
#1.2.1
my_moving_median<- function(x, n, ...)
{
if(!is.vector(x) || !is.numeric(x) || !is.numeric(n) || is.integer(n) )
stop()
vector_length <- length(x)
new_vector <- NA
for(iterate in 1:vector_length)
{
if(vector_length >= (iterate + n ) )
new_vector[iterate] <- ifelse( !missing(...) ,
median(x[iterate:(iterate + n)],na.rm = ...),
median(x[iterate:(iterate + n)]))
}
new_vector
}
my_moving_median(c(5,1,2,NA,2,5,6,8,9,9), 2, na.rm=TRUE)
mark_my_assignment(tasks = "my_moving_median")
#1.2.2
for_mult_table<-function(from,to)
{
if(length(from)!=1 || length(to) != 1)
stop()
iterator <- from:to
#creating a matrix
multiplication_table <- matrix(c(from:to),nrow = (to-from)+1, ncol = (to-from)+1 )
i<- 1; j<-1 # i and j index
for(mrow in iterator) #row iterator to matrix length
{
for(mcol in iterator) #col iterator to matrix length
{
multiplication_table [i,j]<- mrow * mcol
j<- j +1
}
i<-i+1
j<-1
}
multiplication_table
}
for_mult_table(1,10)
mark_my_assignment(tasks = "for_mult_table")
#1.3.1
find_cumsum<- function(x, find_sum)
{
total_sum<-0; j<-1; result<-0 #variable initilize
if(!(is.vector(x)) || !(is.numeric(find_sum) || !(is.numeric(x) )) || length(find_sum) != 1 )
stop()
while(total_sum < find_sum && length(x) >= j) #while loop untill sum greater than find_sum and length of vector greater than j Iterator
{
total_sum<- total_sum+x[j]
result<- total_sum
j<- j+1
}
result
}
find_cumsum(x=1,find_sum=1000)
mark_my_assignment(tasks = "find_cumsum")
#1.3.2
while_mult_table<-function(from, to)
{
stopifnot(length(from) == 1 , length(to) == 1)
multiplication_matrix<- matrix(c(from:to),nrow = (to-from)+1,ncol = (to-from)+1 )
i<-1; j<-1 #i and j index
temp_copy_from = from
while(temp_copy_from <= to)
{
f<-from
while(f<=to)
{
multiplication_matrix[i,j]<- temp_copy_from*f
j<- j +1; f<-f+1
}
temp_copy_from<- temp_copy_from+1
i<-i+1; j<-1
}
multiplication_matrix
}
while_mult_table(10,12)
mark_my_assignment(tasks = "while_mult_table")
#1.4.1
repeat_find_cumsum<- function(x, find_sum)
{
total_sum<-0; j<-1; result<-0
if(!(is.vector(x)) || !(is.numeric(find_sum) || !(is.numeric(x) )) || length(find_sum) != 1 )
stop()
j<- length(x)
iterator <- 1
repeat
{
if(total_sum > find_sum || j <= 0)
break
else
{
total_sum <- total_sum + x[iterator]
result<- total_sum
j<- j-1
iterator<- iterator+1
}
}
result
}
repeat_find_cumsum(x=1:100,find_sum=500)
mark_my_assignment(tasks = "repeat_find_cumsum")
#.4.2
repeat_my_moving_median<- function(x, n, ...)
{
vector_length <- length(x)
new_vector <- NA
iterate<-1
if(!is.vector(x) || !is.numeric(x) || !is.numeric(n) || is.integer(n) )
stop()
repeat
{
if(iterate < vector_length)
{
if(vector_length >= (iterate+n))
{
new_vector[iterate] <- ifelse( !missing(...) , median(x[iterate:(iterate + n)],na.rm = ...), median(x[iterate:(iterate + n)]))
}
iterate<-iterate+1
}
else
break
}
return(new_vector)
}
repeat_my_moving_median(x= 1:10,n=2)
mark_my_assignment(tasks = "repeat_my_moving_median")
#1.5.1
in_environment<-function(env)
{
v_result <- ls(env)
v_result
}
env <- search()[length(search())]
env
funs<- in_environment(env)
funs[1:5]
mark_my_assignment(tasks = "in_environment")
#1.6.1
cov<- function(X)
{
if(!is.data.frame(X))
{
stop()
}
v_result<- NA
column_name <- names(X)
for(y in column_name)
{
mean_value<- lapply(X[y],function(y){mean(y)})
sd_value<- lapply(X[y],function(y){sd(y)})
v_result[y]<- (as.numeric(unlist(sd_value))/as.numeric(unlist(mean_value)))
}
v_result<- v_result[!is.na(v_result)]
v_result
}
data(iris)
cov(iris[3:4])
mark_my_assignment(tasks = "cov")
#1.7.1
moment <- function(i) {
if(!(is.numeric(i)) )
stop()
result <- function(x)
{
expected_mean <- x-mean(x)
mean((expected_mean)^i)
}
result
}
m1 <- moment(1)
m2 <- moment(2)
m1(1:100)
m2(1:100)
mark_my_assignment(tasks = "moment")