Question 1:

# create function
count_zeros <- function(x){ 
  counter <- 0
  for (i in x) {
    if (i == 0) {
      counter <- counter + 1
    }
  }
  return(counter) 
} 

# test function out
vector <- c(0,3,4,5,0,3,4,8,0,9,8)
count_zeros(x=vector)
## [1] 3

Question 2:

# same thing but with subsetting
count_zeros2 <- function(x) {
  return(length(x[x==0]))
}

# test function out
vector <- c(0,3,4,5,0,3,4,8,0,9,8)
count_zeros2(x=vector)
## [1] 3

Question 3:

# create function
make_matrix <- function(rows,columns) {
  result <- matrix(NA, nrow = rows, ncol = columns)
  for (i in 1:nrow(result)) {
    for (j in 1:ncol(result)) {
      result[i,j] <- i*j
    }
  }
  return(result)
}

# test function out
make_matrix(6,7)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]    1    2    3    4    5    6    7
## [2,]    2    4    6    8   10   12   14
## [3,]    3    6    9   12   15   18   21
## [4,]    4    8   12   16   20   24   28
## [5,]    5   10   15   20   25   30   35
## [6,]    6   12   18   24   30   36   42

Question 4a:

nGroup <- 3 # number of groups
nName <- c("Group1","Group2", "Group3") # names of groups
nSize <- c(30,30,30) # number of observations
nMean <- c(20,40,60) # mean of each group

response <- c(rnorm(n=nSize[1],mean=nMean[1]),
            rnorm(n=nSize[2],mean=nMean[2]),
            rnorm(n=nSize[3],mean=nMean[3]))
groups <- rep(nName,nSize)
my_data <- data.frame(groups,response)
my_data
##    groups response
## 1  Group1 20.98736
## 2  Group1 20.35682
## 3  Group1 18.91492
## 4  Group1 17.35389
## 5  Group1 18.78212
## 6  Group1 21.26621
## 7  Group1 19.13908
## 8  Group1 19.34372
## 9  Group1 19.32326
## 10 Group1 19.29871
## 11 Group1 20.10966
## 12 Group1 20.94779
## 13 Group1 19.42432
## 14 Group1 18.30466
## 15 Group1 21.12526
## 16 Group1 20.08149
## 17 Group1 19.24151
## 18 Group1 20.46510
## 19 Group1 18.74101
## 20 Group1 20.49334
## 21 Group1 21.06185
## 22 Group1 21.07860
## 23 Group1 17.75116
## 24 Group1 21.89418
## 25 Group1 19.32812
## 26 Group1 18.79867
## 27 Group1 20.51564
## 28 Group1 18.89493
## 29 Group1 19.79619
## 30 Group1 21.31274
## 31 Group2 41.50852
## 32 Group2 38.77072
## 33 Group2 39.65302
## 34 Group2 40.00718
## 35 Group2 38.14949
## 36 Group2 39.90804
## 37 Group2 40.50609
## 38 Group2 40.44337
## 39 Group2 38.18485
## 40 Group2 38.91920
## 41 Group2 40.72416
## 42 Group2 39.95543
## 43 Group2 41.43085
## 44 Group2 40.21547
## 45 Group2 38.02838
## 46 Group2 40.60385
## 47 Group2 40.98466
## 48 Group2 38.55334
## 49 Group2 40.41761
## 50 Group2 39.24833
## 51 Group2 40.58264
## 52 Group2 40.07959
## 53 Group2 39.45937
## 54 Group2 42.83763
## 55 Group2 39.74690
## 56 Group2 39.40884
## 57 Group2 39.87646
## 58 Group2 39.24406
## 59 Group2 41.12461
## 60 Group2 41.12620
## 61 Group3 60.21598
## 62 Group3 59.70984
## 63 Group3 58.97528
## 64 Group3 62.04678
## 65 Group3 59.24393
## 66 Group3 59.92538
## 67 Group3 58.96092
## 68 Group3 60.45675
## 69 Group3 60.74937
## 70 Group3 60.61075
## 71 Group3 59.17827
## 72 Group3 60.13490
## 73 Group3 61.55484
## 74 Group3 59.04167
## 75 Group3 61.03914
## 76 Group3 59.87566
## 77 Group3 59.33599
## 78 Group3 61.18022
## 79 Group3 60.51470
## 80 Group3 60.46660
## 81 Group3 59.44917
## 82 Group3 59.46656
## 83 Group3 61.01790
## 84 Group3 61.28862
## 85 Group3 60.51384
## 86 Group3 59.68498
## 87 Group3 60.63514
## 88 Group3 58.89747
## 89 Group3 57.86814
## 90 Group3 61.88089

Question 4b:

shuffleMeans <- function(data=my_data){
  shuffled <- sample(data$response)
  data$shuffled <- shuffled
  result <- c(mean(shuffled[data$groups=="Group1"]),mean(shuffled[data$groups=="Group2"]),mean(shuffled[data$groups=="Group3"]))
  return(result)
}

shuffleMeans()
## [1] 39.91269 42.66624 37.34610

Question 4c:

data100replicates <- data.frame(Replicate = 1:100, Group1 = 100, Group2 = 100, Group3 = 100)

for (i in 1:100){
  new_means <- shuffleMeans(my_data)
  data100replicates[i, 2:4] <- new_means
}
print(data100replicates)
##     Replicate   Group1   Group2   Group3
## 1           1 39.14921 37.35746 43.41836
## 2           2 37.48816 37.09644 45.34043
## 3           3 37.12755 40.60533 42.19215
## 4           4 42.06185 40.53081 37.33237
## 5           5 42.49819 42.01097 35.41586
## 6           6 40.49045 38.06812 41.36646
## 7           7 38.14221 38.77873 43.00409
## 8           8 40.64948 36.70067 42.57488
## 9           9 42.01349 36.09768 41.81386
## 10         10 40.95213 39.07688 39.89602
## 11         11 39.17917 44.12256 36.62330
## 12         12 37.37436 41.33221 41.21846
## 13         13 44.52809 38.03214 37.36480
## 14         14 36.00356 34.73230 49.18917
## 15         15 37.12006 40.67889 42.12609
## 16         16 37.39752 40.45010 42.07740
## 17         17 45.53214 36.56639 37.82650
## 18         18 39.26849 40.19051 40.46602
## 19         19 38.65428 41.30838 39.96237
## 20         20 37.08775 44.79257 38.04471
## 21         21 43.32475 35.41927 41.18101
## 22         22 42.83012 41.80352 35.29139
## 23         23 42.22287 40.39596 37.30620
## 24         24 42.72521 35.66169 41.53813
## 25         25 39.26188 40.73772 39.92543
## 26         26 38.58309 41.95494 39.38699
## 27         27 35.44961 43.83251 40.64291
## 28         28 40.04000 36.40265 43.48238
## 29         29 37.35812 42.74470 39.82221
## 30         30 39.94265 39.33705 40.64534
## 31         31 40.48739 44.65293 34.78471
## 32         32 40.29739 40.53305 39.09459
## 33         33 42.67527 38.54925 38.70051
## 34         34 41.47437 37.29578 41.15488
## 35         35 45.26327 35.91289 38.74887
## 36         36 38.52594 41.85665 39.54244
## 37         37 41.74725 40.79415 37.38363
## 38         38 40.18960 44.00259 35.73284
## 39         39 35.75206 39.96079 44.21218
## 40         40 41.95651 39.40960 38.55891
## 41         41 43.00406 37.22130 39.69967
## 42         42 42.64674 41.98412 35.29416
## 43         43 41.41919 35.97166 42.53418
## 44         44 39.86922 40.03624 40.01956
## 45         45 40.88374 33.57934 45.46196
## 46         46 43.30341 38.73243 37.88919
## 47         47 39.78274 42.93529 37.20699
## 48         48 39.17457 38.81672 41.93374
## 49         49 38.90982 38.37487 42.64035
## 50         50 36.55801 40.83446 42.53256
## 51         51 36.29314 42.87636 40.75553
## 52         52 41.15638 41.86398 36.90467
## 53         53 39.96964 38.11891 41.83648
## 54         54 39.91469 38.64880 41.36154
## 55         55 40.20204 39.97874 39.74425
## 56         56 41.72007 37.70112 40.50384
## 57         57 39.09037 39.58987 41.24479
## 58         58 39.47918 37.18108 43.26477
## 59         59 37.72252 41.26097 40.94154
## 60         60 39.97903 41.91979 38.02621
## 61         61 41.49425 38.29792 40.13286
## 62         62 38.34398 40.91897 40.66208
## 63         63 44.07741 36.44075 39.40687
## 64         64 42.15451 38.95448 38.81604
## 65         65 37.42892 39.89730 42.59881
## 66         66 34.84105 41.31658 43.76740
## 67         67 41.87262 37.95128 40.10113
## 68         68 41.44354 40.51903 37.96246
## 69         69 42.42828 38.89736 38.59939
## 70         70 42.17433 37.84033 39.91036
## 71         71 41.93120 37.93577 40.05807
## 72         72 37.73555 38.65206 43.53742
## 73         73 41.16907 36.05430 42.70166
## 74         74 42.68593 38.74674 38.49235
## 75         75 39.54619 36.03241 44.34643
## 76         76 37.78267 40.00961 42.13274
## 77         77 40.65107 40.31532 38.95864
## 78         78 38.67277 42.09921 39.15305
## 79         79 38.60060 39.18392 42.14051
## 80         80 42.53745 39.95449 37.43309
## 81         81 38.97107 36.66673 44.28723
## 82         82 39.52220 37.11247 43.29036
## 83         83 42.01015 38.10435 39.81053
## 84         84 38.83111 41.89034 39.20357
## 85         85 40.12409 37.42729 42.37366
## 86         86 46.08450 38.73322 35.10730
## 87         87 36.11770 40.51923 43.28809
## 88         88 39.26840 38.57925 42.07738
## 89         89 41.06683 38.76233 40.09586
## 90         90 38.64585 37.31199 43.96719
## 91         91 40.39911 40.51921 39.00671
## 92         92 41.12684 40.73132 38.06686
## 93         93 42.52557 37.46502 39.93444
## 94         94 37.43209 36.48843 46.00451
## 95         95 36.07506 43.24424 40.60573
## 96         96 40.66670 37.92686 41.33147
## 97         97 40.15844 44.83391 34.93268
## 98         98 39.58546 41.49653 38.84304
## 99         99 37.93181 46.76474 35.22849
## 100       100 40.53930 36.81488 42.57085

Question 4d

# plot for group 1
qplot(data=data100replicates,x=Group1,geom="histogram") +
  labs(x = "Mean", y = "Frequency", title = "Group 1")

# plot for group 2
qplot(data=data100replicates,x=Group2,geom="histogram") +
  labs(x = "Mean", y = "Frequency", title = "Group 2")

# plot for group 3
qplot(data=data100replicates,x=Group3,geom="histogram") +
  labs(x = "Mean", y = "Frequency", title = "Group 3")

The distributions of the reshuffled means look quite similar across groups. The distributions of the original means looked quite different across groups. This makes sense because we reshuffled the data across groups, which reduced the differences in our response that we originally saw between the groups.