game_payoff_matrix = matrix(cbind(c(1, -2, +2, -2, +2), c(+2, 1, -2, +2, -2), c(-2,
+2, 1, -2, +2), c(+2, -2, +2, 1, -2), c(-2, +2, -2, +2, 1)), 5)
game_payoff_matrix [,1] [,2] [,3] [,4] [,5]
[1,] 1 2 -2 2 -2
[2,] -2 1 2 -2 2
[3,] 2 -2 1 2 -2
[4,] -2 2 -2 1 2
[5,] 2 -2 2 -2 1
game_payoff_matrix_df = as.data.frame(game_payoff_matrix)
colnames(game_payoff_matrix_df) = c("scissors", "paper", "rock", "lizard", "spock")
rownames(game_payoff_matrix_df) = c("scissors", "paper", "rock", "lizard", "spock")
game_payoff_matrix_df scissors paper rock lizard spock
scissors 1 2 -2 2 -2
paper -2 1 2 -2 2
rock 2 -2 1 2 -2
lizard -2 2 -2 1 2
spock 2 -2 2 -2 1
Five_shape_game = function(player1, player2) {
if (game_payoff_matrix_df[player1, player2] == +2) {
out11 = "Player 1 wins!"
}
if (game_payoff_matrix_df[player1, player2] == -2) {
out11 = "Player 2 wins!"
}
if (game_payoff_matrix_df[player1, player2] == 1) {
out11 = "No one wins or losses !"
} else (stopifnot())
return(out11)
}[1] "Player 1 wins!"
[1] "Player 2 wins!"
[1] "Player 2 wins!"
[1] "No one wins or losses !"
for_loop_rolling_median = function(x_vector, nn, na.rm = FALSE) {
out00 = seq(0:(length(x_vector) - nn - 1)) * 0 - 99
for (moving in 0:(length(x_vector) - nn - 1)) {
moving_window_element = x_vector[(1 + moving):((nn + 1) + moving)]
if (na.rm == FALSE) {
median(moving_window_element)
out00[moving + 1] = median(moving_window_element)
}
if (na.rm == TRUE) {
if (isTRUE(is.na(moving_window_element))) {
median(moving_window_element)
} else {
median(moving_window_element, na.rm = TRUE)
}
out00[moving + 1] = median(moving_window_element, na.rm = TRUE)
} else {
stopifnot()
}
}
return(out00)
}[1] 2 3 4 5 6 7 8 9
[1] 7 8 9 10 11 12 13
nn = 2
x_vector = c(5, 1, 2, NA, 2, 5, 6, 8, 9, 9)
for_loop_rolling_median(x_vector, nn, na.rm = FALSE)[1] 2 NA NA NA 5 6 8 9
nn = 2
x_vector = c(5, 1, 2, NA, 2, 5, 6, 8, 9, 9)
for_loop_rolling_median(x_vector, nn, na.rm = TRUE)[1] 2.0 1.5 2.0 3.5 5.0 6.0 8.0 9.0
repeat_rolling_median = function(x_vector, nn, na.rm = FALSE) {
out00 = seq(0:(length(x_vector) - nn - 1)) * 0 - 99
moving = 0
repeat {
if (moving > (length(x_vector) - nn - 1))
break
moving_window_element = x_vector[(1 + moving):((nn + 1) + moving)]
if (na.rm == FALSE) {
median(moving_window_element)
out00[moving + 1] = median(moving_window_element)
}
if (na.rm == TRUE) {
if (isTRUE(is.na(moving_window_element))) {
median(moving_window_element)
} else {
median(moving_window_element, na.rm = TRUE)
}
out00[moving + 1] = median(moving_window_element, na.rm = TRUE)
} else {
stopifnot()
}
moving = moving + 1
}
return(out00)
} [1] 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
[1] 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
[19] 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
[37] 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
[55] 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
[73] 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
[91] 99 100 101 102 103 104 105 106 107 108 109 110 111
nn = 5
x_vector = c(5, 1, 2, 5, 8, 11, NA, 5, 1, 2, NA, 2, 5, 4, 6, 8, 7, 2, 6, 4, 33, 55,
1, 9, 4, 6, 18, 9, 29)
repeat_rolling_median(x_vector, nn, na.rm = FALSE) [1] 5.0 NA NA NA NA NA NA NA NA NA NA 5.5 5.5 6.0 6.0 6.5 6.5 5.0 7.5
[20] 6.5 7.5 7.5 7.5 9.0
nn = 4
x_vector = c(5, 1, 2, 5, 8, 11, NA, 5, 1, 2, NA, 2, 5, 4, 6, 8, 7, 2, 6, 4, 33, 55,
1, 9, 4, 6, 18, 9, 29)
repeat_rolling_median(x_vector, nn, na.rm = TRUE) [1] 5.0 5.0 6.5 6.5 6.5 3.5 2.0 2.0 2.0 3.0 4.5 5.0 6.0 6.0 6.0 6.0 6.0 6.0 6.0
[20] 9.0 9.0 6.0 6.0 9.0 9.0
for_loop_table = function(from, to) {
check_positive_Integer_greater_zero = function(x) {
if (is.numeric(x)) {
out1 = (abs(x) - as.integer(abs(x))) == 0
out2 = x > 0
Is_Positive_integer = (out1 && out2)
} else {
stopifnot()
}
return(Is_Positive_integer)
}
if (check_positive_Integer_greater_zero(from) && check_positive_Integer_greater_zero(to)) {
outs = matrix((from:to), nrow = length(seq(from:to)), ncol = length(seq(from:to)))
outA = outs
outB = t(outs)
dim(outA)
outAB = outA * NA
for (jj in 1:dim(outA)[1]) {
for (kk in 1:dim(outA)[1]) {
outAB[jj, kk] = outA[jj, kk] * outB[jj, kk]
}
}
square_outs = as.data.frame(outAB)
colnames(square_outs) = as.character(from:to)
rownames(square_outs) = as.character(from:to)
} else {
square_outs = "please enter positive integer again"
}
return(square_outs)
} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
3 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45
4 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
5 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
6 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90
7 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105
8 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120
9 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135
10 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
11 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165
12 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180
13 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195
14 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210
15 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225
10 11 12 13 14 15 16 17 18 19 20 21 22
10 100 110 120 130 140 150 160 170 180 190 200 210 220
11 110 121 132 143 154 165 176 187 198 209 220 231 242
12 120 132 144 156 168 180 192 204 216 228 240 252 264
13 130 143 156 169 182 195 208 221 234 247 260 273 286
14 140 154 168 182 196 210 224 238 252 266 280 294 308
15 150 165 180 195 210 225 240 255 270 285 300 315 330
16 160 176 192 208 224 240 256 272 288 304 320 336 352
17 170 187 204 221 238 255 272 289 306 323 340 357 374
18 180 198 216 234 252 270 288 306 324 342 360 378 396
19 190 209 228 247 266 285 304 323 342 361 380 399 418
20 200 220 240 260 280 300 320 340 360 380 400 420 440
21 210 231 252 273 294 315 336 357 378 399 420 441 462
22 220 242 264 286 308 330 352 374 396 418 440 462 484
[1] "please enter positive integer again"
[1] "please enter positive integer again"
[1] "please enter positive integer again"
while_loop_table = function(from, to) {
check_positive_Integer_greater_zero = function(x) {
if (is.numeric(x)) {
out1 = (abs(x) - as.integer(abs(x))) == 0
out2 = x > 0
Is_Positive_integer = (out1 && out2)
} else {
stopifnot()
}
return(Is_Positive_integer)
}
if (check_positive_Integer_greater_zero(from) && check_positive_Integer_greater_zero(to)) {
outs = matrix((from:to), nrow = length(seq(from:to)), ncol = length(seq(from:to)))
outA = outs
outB = t(outs)
dim(outA)
outAB = outA * NA
jj = 1
while (jj <= dim(outA)[1]) {
kk = 1
while (kk <= dim(outA)[1]) {
outAB[jj, kk] = outA[jj, kk] * outB[jj, kk]
kk = kk + 1
}
jj = jj + 1
}
square_outs = as.data.frame(outAB)
colnames(square_outs) = as.character(from:to)
rownames(square_outs) = as.character(from:to)
} else {
square_outs = "please enter positive integer again"
}
return(square_outs)
} 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
5 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110
6 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126 132
7 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154
8 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152 160 168 176
9 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171 180 189 198
10 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220
11 55 66 77 88 99 110 121 132 143 154 165 176 187 198 209 220 231 242
12 60 72 84 96 108 120 132 144 156 168 180 192 204 216 228 240 252 264
13 65 78 91 104 117 130 143 156 169 182 195 208 221 234 247 260 273 286
14 70 84 98 112 126 140 154 168 182 196 210 224 238 252 266 280 294 308
15 75 90 105 120 135 150 165 180 195 210 225 240 255 270 285 300 315 330
16 80 96 112 128 144 160 176 192 208 224 240 256 272 288 304 320 336 352
17 85 102 119 136 153 170 187 204 221 238 255 272 289 306 323 340 357 374
18 90 108 126 144 162 180 198 216 234 252 270 288 306 324 342 360 378 396
19 95 114 133 152 171 190 209 228 247 266 285 304 323 342 361 380 399 418
20 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400 420 440
[ reached 'max' / getOption("max.print") -- omitted 2 rows ]
lapply_cov = function(df) {
sd_over_mean = function(df_col) {
covariance = sd(df_col)/mean(df_col)
}
out1 = lapply(df, sd_over_mean)
out1_df = as.data.frame(out1)
return(out1_df)
} Sepal.Length Sepal.Width Petal.Length Petal.Width
1 0.1417113 0.1425642 0.4697441 0.6355511
Petal.Length Petal.Width
1 0.4697441 0.6355511
moment_generating = function(i) {
mom = function(tst) {
if (i == 1) {
outss = mean(tst - mean(tst))
}
if (i == 2) {
v1 = (tst - mean(tst))
outss = mean(v1 * v1)
}
return(outss)
}
}[1] 0
[1] 10266.67