Question 1
counter <- 0
vector <- c(0,1,2,3,4,5,6,0,0,4,5,6,0)
for (i in vector) {
if ("0" %in% i == "TRUE") {
counter = counter + 1
}
return(counter)
}
counter
## [1] 1
Question 2
vector <- c(0,1,2,3,4,5,6,0,0,4,5,6,0)
countzeroes <- function(vector){
return(sum(vector==0))
}
countzeroes(vector)
## [1] 4
Question 3
getdata <- function(row,column) {
results <- matrix(nrow=row,ncol=column)
for (i in 1:row) {
for (j in 1:column) {
results[i,j] <- i*j
}
}
return(results)
}
row <- 3
column <- 4
data <- getdata(row,column)
matrix <- matrix(data=data,nrow=row,ncol=column)
Question 4a
n <- 100
means <- c(10,20,30)
group <- rep(1:3, each=n)
response <- c(rnorm(n,mean=means[1]),
rnorm(n,mean=means[2]),
rnorm(n,mean=means[3]))
data <- data.frame(group,response)
data
## group response
## 1 1 11.692187
## 2 1 10.236350
## 3 1 10.675188
## 4 1 8.600147
## 5 1 10.568220
## 6 1 9.297282
## 7 1 8.545246
## 8 1 9.873454
## 9 1 10.416995
## 10 1 9.710488
## 11 1 9.155019
## 12 1 10.601946
## 13 1 11.151490
## 14 1 9.988436
## 15 1 9.485377
## 16 1 9.922805
## 17 1 9.709895
## 18 1 10.409655
## 19 1 8.900673
## 20 1 9.292351
## 21 1 10.620747
## 22 1 9.514015
## 23 1 10.250891
## 24 1 10.297382
## 25 1 10.168545
## 26 1 9.632029
## 27 1 10.115404
## 28 1 8.443547
## 29 1 11.495844
## 30 1 9.990936
## 31 1 9.165865
## 32 1 9.508610
## 33 1 8.709105
## 34 1 10.625152
## 35 1 9.712415
## 36 1 10.799775
## 37 1 9.935095
## 38 1 11.224517
## 39 1 9.370498
## 40 1 9.461181
## 41 1 8.881186
## 42 1 10.964333
## 43 1 9.607896
## 44 1 9.162987
## 45 1 9.753565
## 46 1 9.401167
## 47 1 10.422880
## 48 1 11.474397
## 49 1 10.878502
## 50 1 10.442106
## 51 1 11.673046
## 52 1 11.247146
## 53 1 11.203816
## 54 1 10.426875
## 55 1 10.232474
## 56 1 10.285833
## 57 1 8.201555
## 58 1 8.071748
## 59 1 10.173722
## 60 1 9.462430
## 61 1 11.446551
## 62 1 9.262406
## 63 1 9.303723
## 64 1 9.904724
## 65 1 10.192736
## 66 1 10.464904
## 67 1 10.104059
## 68 1 9.293272
## 69 1 11.217980
## 70 1 10.826429
## 71 1 9.190445
## 72 1 10.600243
## 73 1 10.698416
## 74 1 11.383174
## 75 1 8.992021
## 76 1 7.522726
## 77 1 7.109439
## 78 1 11.267629
## 79 1 12.244947
## 80 1 10.095766
## 81 1 12.343839
## 82 1 9.966302
## 83 1 7.793666
## 84 1 9.978806
## 85 1 12.063794
## 86 1 11.509032
## 87 1 8.490879
## 88 1 9.565155
## 89 1 9.604980
## 90 1 10.503334
## 91 1 10.973635
## 92 1 10.068793
## 93 1 8.809201
## 94 1 10.920685
## 95 1 11.012413
## 96 1 8.034002
## 97 1 8.827438
## 98 1 8.256931
## 99 1 10.921153
## 100 1 8.297466
## 101 2 19.724334
## 102 2 21.450901
## 103 2 19.180724
## 104 2 20.096048
## 105 2 19.914063
## 106 2 21.702235
## 107 2 18.765219
## 108 2 17.969450
## 109 2 19.984320
## 110 2 19.926584
## 111 2 20.151689
## 112 2 20.659935
## 113 2 18.032100
## 114 2 20.072881
## 115 2 20.630095
## 116 2 21.658648
## 117 2 18.241582
## 118 2 19.733089
## 119 2 20.527644
## 120 2 18.545714
## 121 2 18.718738
## 122 2 20.088984
## 123 2 20.386939
## 124 2 19.774880
## 125 2 20.512407
## 126 2 20.437915
## 127 2 18.754403
## 128 2 20.333940
## 129 2 19.089214
## 130 2 19.059622
## 131 2 20.441353
## 132 2 20.235052
## 133 2 19.496231
## 134 2 20.626436
## 135 2 20.492065
## 136 2 18.625255
## 137 2 19.327086
## 138 2 19.998462
## 139 2 20.624381
## 140 2 20.410500
## 141 2 22.442630
## 142 2 20.194517
## 143 2 20.938185
## 144 2 19.263304
## 145 2 18.317223
## 146 2 19.458782
## 147 2 20.962416
## 148 2 19.487833
## 149 2 21.373435
## 150 2 19.506791
## 151 2 20.227462
## 152 2 21.388418
## 153 2 19.045290
## 154 2 18.109282
## 155 2 21.231679
## 156 2 21.861932
## 157 2 21.219394
## 158 2 19.317536
## 159 2 18.884386
## 160 2 20.286607
## 161 2 19.658726
## 162 2 18.685783
## 163 2 20.568937
## 164 2 18.391080
## 165 2 19.050373
## 166 2 21.272179
## 167 2 19.564808
## 168 2 19.587565
## 169 2 21.129167
## 170 2 19.184309
## 171 2 20.287966
## 172 2 19.938995
## 173 2 19.285226
## 174 2 21.479552
## 175 2 20.409527
## 176 2 20.933338
## 177 2 19.708639
## 178 2 19.060992
## 179 2 20.362325
## 180 2 20.864145
## 181 2 21.134500
## 182 2 20.468257
## 183 2 20.040623
## 184 2 18.869706
## 185 2 19.827835
## 186 2 21.131221
## 187 2 21.290062
## 188 2 17.952797
## 189 2 21.596986
## 190 2 19.355045
## 191 2 21.232446
## 192 2 18.861029
## 193 2 20.406964
## 194 2 22.639318
## 195 2 19.876279
## 196 2 21.110629
## 197 2 17.431981
## 198 2 19.200470
## 199 2 20.219503
## 200 2 20.708934
## 201 3 29.789207
## 202 3 31.140547
## 203 3 29.468624
## 204 3 29.427038
## 205 3 29.444183
## 206 3 30.259957
## 207 3 28.980906
## 208 3 30.506931
## 209 3 27.822493
## 210 3 31.725248
## 211 3 29.539439
## 212 3 28.367741
## 213 3 30.501036
## 214 3 29.328724
## 215 3 29.996206
## 216 3 29.364128
## 217 3 31.833984
## 218 3 29.682858
## 219 3 29.232903
## 220 3 29.647101
## 221 3 28.868741
## 222 3 29.776157
## 223 3 30.079392
## 224 3 30.255268
## 225 3 30.631141
## 226 3 30.711474
## 227 3 31.094343
## 228 3 30.787335
## 229 3 29.579963
## 230 3 28.064028
## 231 3 29.200653
## 232 3 28.543435
## 233 3 31.335734
## 234 3 28.656603
## 235 3 30.206780
## 236 3 29.770726
## 237 3 28.480236
## 238 3 31.552451
## 239 3 29.836895
## 240 3 32.418883
## 241 3 29.446455
## 242 3 31.241851
## 243 3 29.707560
## 244 3 29.560082
## 245 3 30.180864
## 246 3 30.327870
## 247 3 30.429168
## 248 3 31.744367
## 249 3 30.985889
## 250 3 31.234006
## 251 3 30.624865
## 252 3 30.976725
## 253 3 30.110881
## 254 3 31.418955
## 255 3 31.056918
## 256 3 30.861752
## 257 3 30.144497
## 258 3 28.331658
## 259 3 29.766020
## 260 3 30.168391
## 261 3 30.360872
## 262 3 29.642146
## 263 3 30.485786
## 264 3 32.466400
## 265 3 30.620704
## 266 3 29.313364
## 267 3 28.386211
## 268 3 30.778896
## 269 3 30.530416
## 270 3 30.075058
## 271 3 30.506115
## 272 3 31.874443
## 273 3 32.502685
## 274 3 29.503982
## 275 3 29.750439
## 276 3 30.267045
## 277 3 29.968910
## 278 3 27.966262
## 279 3 29.794282
## 280 3 29.290574
## 281 3 30.021709
## 282 3 29.755297
## 283 3 28.836487
## 284 3 28.991504
## 285 3 30.452428
## 286 3 29.988893
## 287 3 30.338384
## 288 3 30.727288
## 289 3 29.111123
## 290 3 28.561039
## 291 3 30.545916
## 292 3 31.249392
## 293 3 29.850772
## 294 3 31.265625
## 295 3 30.267416
## 296 3 29.935121
## 297 3 27.691272
## 298 3 29.395013
## 299 3 30.353242
## 300 3 31.298955
Question 4b
# reshuffle + get means!
reshuffle <- function(data) {
for (i in data) {
shuffleresponse <- sample(data[,2])
shuffleresponse
# new data frame using the shuffled responses
data2 <- data.frame(group,shuffleresponse)
data2
# get means of each group
group1mean <- mean(data2[1:n,2])
group2mean <- mean(data2[n:(2*n),2])
group3mean <- mean(data2[(2*n):(3*n),2])
}
newmeans <- data.frame(group1mean,group2mean,group3mean)
return(newmeans)
}
# test that it works
reshuffle(data) # it works!
## group1mean group2mean group3mean
## 1 19.86457 20.05843 20.15549
Question 4c
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
runagain <- function(data) {
# make an empty data frame to fill in later
results <- data.frame(matrix(nrow=n,ncol=4))
colnames(results) <- c("replicate","group1mean","group2mean","group3mean")
# run it n times!
for (i in 1:n) {
# get the new means using my reshuffle function
newmeans <- reshuffle(data)
# fill in the empty data frame
results[i,1:4] <- c(i,newmeans)
}
# gimme my results
return(results)
}
n=100
for4d <- runagain(data)
for4d
## replicate group1mean group2mean group3mean
## 1 1 19.81707 20.62274 19.60901
## 2 2 20.40236 20.78310 18.77306
## 3 3 19.81402 19.84775 20.39012
## 4 4 20.13688 21.15165 18.96940
## 5 5 20.38489 19.33378 20.23732
## 6 6 19.59110 20.19371 20.25262
## 7 7 19.97526 19.53269 20.53153
## 8 8 19.47609 20.56501 20.10180
## 9 9 19.98112 20.29141 19.99946
## 10 10 19.65583 21.24531 19.33279
## 11 11 19.86247 19.50672 20.89471
## 12 12 19.83529 19.24486 20.88856
## 13 13 20.36363 19.73835 19.94221
## 14 14 19.58685 20.23017 20.13890
## 15 15 19.33578 20.69387 20.23105
## 16 16 20.04710 21.13555 18.87765
## 17 17 19.12764 20.07159 20.86396
## 18 18 19.73839 19.42459 21.09563
## 19 19 20.12800 19.24982 20.55943
## 20 20 19.00279 20.97789 19.88599
## 21 21 19.35628 20.98680 19.69060
## 22 22 20.02038 19.62128 20.39818
## 23 23 19.91182 19.51052 20.71982
## 24 24 19.49694 20.83680 19.73092
## 25 25 20.43163 19.93026 19.61231
## 26 26 20.47224 20.38681 19.06904
## 27 27 19.51047 21.40480 18.96350
## 28 28 20.66660 20.38042 19.11727
## 29 29 20.43061 20.86612 18.56541
## 30 30 20.96829 19.39585 19.70769
## 31 31 20.34933 19.94770 19.53829
## 32 32 19.52733 20.78615 19.75684
## 33 33 20.21304 19.93468 19.91390
## 34 34 20.08035 20.59864 19.38320
## 35 35 20.56779 18.65668 20.65208
## 36 36 19.23845 20.43389 20.58728
## 37 37 19.87301 20.06235 20.10970
## 38 38 18.90329 19.71043 21.46016
## 39 39 21.40206 18.98789 19.64450
## 40 40 21.30219 19.04966 19.92957
## 41 41 19.12405 20.81463 19.89106
## 42 42 21.19842 20.15638 18.70814
## 43 43 20.04633 19.20225 20.91153
## 44 44 20.09191 20.66652 19.17369
## 45 45 20.29684 20.11146 19.44914
## 46 46 19.30541 21.16681 19.57229
## 47 47 20.48835 20.04289 19.73610
## 48 48 20.80213 19.82645 19.35154
## 49 49 20.02222 20.32169 19.92525
## 50 50 20.42937 20.23467 19.26360
## 51 51 19.90422 20.71340 19.54828
## 52 52 20.31421 19.51198 20.14215
## 53 53 19.78756 20.48753 19.74691
## 54 54 20.64864 19.77662 19.55944
## 55 55 20.75116 19.68786 19.52415
## 56 56 20.00905 20.21468 19.96990
## 57 57 19.50178 19.73499 20.59672
## 58 58 20.22178 19.75971 19.99742
## 59 59 19.53668 19.61409 20.80587
## 60 60 20.24684 20.25047 19.56068
## 61 61 20.86255 19.45970 19.82669
## 62 62 20.26725 19.55349 20.15086
## 63 63 20.73942 19.82404 19.60885
## 64 64 19.59623 20.93812 19.50813
## 65 65 20.75710 19.80045 19.71640
## 66 66 20.08232 20.22244 19.75478
## 67 67 20.51999 18.86293 20.75693
## 68 68 19.45413 19.75947 20.83721
## 69 69 19.26390 20.35594 20.54477
## 70 70 19.59090 20.10919 20.34900
## 71 71 20.98575 18.96295 20.00079
## 72 72 18.96327 20.24139 20.82515
## 73 73 20.94159 19.28413 19.83795
## 74 74 19.12257 21.13227 19.81338
## 75 75 20.24932 20.03814 19.77141
## 76 76 20.23336 20.81183 18.92033
## 77 77 20.54465 19.97984 19.42302
## 78 78 21.61944 19.44995 18.81412
## 79 79 20.09681 20.66292 19.31061
## 80 80 19.64363 20.44677 19.85619
## 81 81 21.29012 19.85307 18.95016
## 82 82 19.67709 20.91541 19.39178
## 83 83 20.75361 19.63311 19.87843
## 84 84 20.06188 19.52455 20.58747
## 85 85 20.13107 20.01617 19.91075
## 86 86 19.74738 20.43884 19.85277
## 87 87 20.17033 20.57851 19.42109
## 88 88 19.53186 20.06977 20.64114
## 89 89 19.65299 20.57491 19.72537
## 90 90 19.98477 20.41719 19.78048
## 91 91 20.68593 19.88048 19.50361
## 92 92 20.30502 19.25310 20.61328
## 93 93 20.24884 19.72180 20.11543
## 94 94 20.52711 19.82409 19.71513
## 95 95 21.60009 19.66739 18.98177
## 96 96 20.44628 19.46820 20.24469
## 97 97 20.19817 20.50203 19.44353
## 98 98 19.98269 21.74029 18.43320
## 99 99 20.55886 20.57949 19.03382
## 100 100 20.18309 19.38290 20.49210
Question 4d
library(ggplot2)
for4d
## replicate group1mean group2mean group3mean
## 1 1 19.81707 20.62274 19.60901
## 2 2 20.40236 20.78310 18.77306
## 3 3 19.81402 19.84775 20.39012
## 4 4 20.13688 21.15165 18.96940
## 5 5 20.38489 19.33378 20.23732
## 6 6 19.59110 20.19371 20.25262
## 7 7 19.97526 19.53269 20.53153
## 8 8 19.47609 20.56501 20.10180
## 9 9 19.98112 20.29141 19.99946
## 10 10 19.65583 21.24531 19.33279
## 11 11 19.86247 19.50672 20.89471
## 12 12 19.83529 19.24486 20.88856
## 13 13 20.36363 19.73835 19.94221
## 14 14 19.58685 20.23017 20.13890
## 15 15 19.33578 20.69387 20.23105
## 16 16 20.04710 21.13555 18.87765
## 17 17 19.12764 20.07159 20.86396
## 18 18 19.73839 19.42459 21.09563
## 19 19 20.12800 19.24982 20.55943
## 20 20 19.00279 20.97789 19.88599
## 21 21 19.35628 20.98680 19.69060
## 22 22 20.02038 19.62128 20.39818
## 23 23 19.91182 19.51052 20.71982
## 24 24 19.49694 20.83680 19.73092
## 25 25 20.43163 19.93026 19.61231
## 26 26 20.47224 20.38681 19.06904
## 27 27 19.51047 21.40480 18.96350
## 28 28 20.66660 20.38042 19.11727
## 29 29 20.43061 20.86612 18.56541
## 30 30 20.96829 19.39585 19.70769
## 31 31 20.34933 19.94770 19.53829
## 32 32 19.52733 20.78615 19.75684
## 33 33 20.21304 19.93468 19.91390
## 34 34 20.08035 20.59864 19.38320
## 35 35 20.56779 18.65668 20.65208
## 36 36 19.23845 20.43389 20.58728
## 37 37 19.87301 20.06235 20.10970
## 38 38 18.90329 19.71043 21.46016
## 39 39 21.40206 18.98789 19.64450
## 40 40 21.30219 19.04966 19.92957
## 41 41 19.12405 20.81463 19.89106
## 42 42 21.19842 20.15638 18.70814
## 43 43 20.04633 19.20225 20.91153
## 44 44 20.09191 20.66652 19.17369
## 45 45 20.29684 20.11146 19.44914
## 46 46 19.30541 21.16681 19.57229
## 47 47 20.48835 20.04289 19.73610
## 48 48 20.80213 19.82645 19.35154
## 49 49 20.02222 20.32169 19.92525
## 50 50 20.42937 20.23467 19.26360
## 51 51 19.90422 20.71340 19.54828
## 52 52 20.31421 19.51198 20.14215
## 53 53 19.78756 20.48753 19.74691
## 54 54 20.64864 19.77662 19.55944
## 55 55 20.75116 19.68786 19.52415
## 56 56 20.00905 20.21468 19.96990
## 57 57 19.50178 19.73499 20.59672
## 58 58 20.22178 19.75971 19.99742
## 59 59 19.53668 19.61409 20.80587
## 60 60 20.24684 20.25047 19.56068
## 61 61 20.86255 19.45970 19.82669
## 62 62 20.26725 19.55349 20.15086
## 63 63 20.73942 19.82404 19.60885
## 64 64 19.59623 20.93812 19.50813
## 65 65 20.75710 19.80045 19.71640
## 66 66 20.08232 20.22244 19.75478
## 67 67 20.51999 18.86293 20.75693
## 68 68 19.45413 19.75947 20.83721
## 69 69 19.26390 20.35594 20.54477
## 70 70 19.59090 20.10919 20.34900
## 71 71 20.98575 18.96295 20.00079
## 72 72 18.96327 20.24139 20.82515
## 73 73 20.94159 19.28413 19.83795
## 74 74 19.12257 21.13227 19.81338
## 75 75 20.24932 20.03814 19.77141
## 76 76 20.23336 20.81183 18.92033
## 77 77 20.54465 19.97984 19.42302
## 78 78 21.61944 19.44995 18.81412
## 79 79 20.09681 20.66292 19.31061
## 80 80 19.64363 20.44677 19.85619
## 81 81 21.29012 19.85307 18.95016
## 82 82 19.67709 20.91541 19.39178
## 83 83 20.75361 19.63311 19.87843
## 84 84 20.06188 19.52455 20.58747
## 85 85 20.13107 20.01617 19.91075
## 86 86 19.74738 20.43884 19.85277
## 87 87 20.17033 20.57851 19.42109
## 88 88 19.53186 20.06977 20.64114
## 89 89 19.65299 20.57491 19.72537
## 90 90 19.98477 20.41719 19.78048
## 91 91 20.68593 19.88048 19.50361
## 92 92 20.30502 19.25310 20.61328
## 93 93 20.24884 19.72180 20.11543
## 94 94 20.52711 19.82409 19.71513
## 95 95 21.60009 19.66739 18.98177
## 96 96 20.44628 19.46820 20.24469
## 97 97 20.19817 20.50203 19.44353
## 98 98 19.98269 21.74029 18.43320
## 99 99 20.55886 20.57949 19.03382
## 100 100 20.18309 19.38290 20.49210
plot <- qplot(data=for4d,
y=for4d[,2],
geom="histogram",
main="Group Means",
xlim=c(0,100),
ylim=c(0,30),
xlab="Replicate",
ylab="Group 1 Mean Value")
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plot
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_bar()`).
# I'm not sure why this looks so weird
# Repeat for each group