1

# ======================================================================
# FUNCTION findZero
# Purpose: find zeros in a vector
# Input: x = vector
# Output: counter
# --------------------------------------------------------------------
findZero <- function(x = NULL) {
  counter <- 0
  for(i in seq_along(x)) {
    if(x[i] == 0) {
      counter <- counter + 1
    }
  }
  return(counter)
}

vec_1 <- c(1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0)
vec_2 <- findZero(vec_1)
print(vec_2)
## [1] 5

2

zeros <- length(vec_1[vec_1 == 0])
print(zeros)
## [1] 5

3

# ======================================================================
# FUNCTION makeMat
# Purpose: make a matrix that is the product of the input
# Input: row = int
#       col = int
# Output: matrix
# --------------------------------------------------------------------
makeMat <- function(row = runif(1, 0, 10), col = runif(1, 0, 10)) {
  mat <- matrix(nrow = row, ncol = col)
  for(i in 1:nrow(mat)) {
    for(j in 1:ncol(mat)) {
      mat[i,j] <- i*j
    }
  }
  return(mat)
}

mat_1 <- makeMat(row = 5, col = 10)
print(mat_1)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,]    1    2    3    4    5    6    7    8    9    10
## [2,]    2    4    6    8   10   12   14   16   18    20
## [3,]    3    6    9   12   15   18   21   24   27    30
## [4,]    4    8   12   16   20   24   28   32   36    40
## [5,]    5   10   15   20   25   30   35   40   45    50

4

test <- c(rep('Control', 10), rep('Treatment1', 10), rep('Treatment2', 10))
resp <- c(runif(10, 0, 3), runif(10, 4, 7), runif(10, 8, 10))
dat <- data.frame(test, resp)

# ======================================================================
# FUNCTION shuffleData
# Purpose: shuffle response variable data
# Input: data = data frame
# Output: shuffled data frame
# --------------------------------------------------------------------
shuffleData <- function(data = NULL) {
  newResp <- sample(data$resp)
  data$resp <- newResp
  cont <- group_by(data, test)
  meany <- summarize(cont, mean = mean(resp))
  means <- c(resp[1], resp[2], resp[3])
  return(means)
}

means <- shuffleData(dat)
print(means)
## [1] 0.07362682 0.43951398 2.23187984
meanVec <- rep(NA, 100)
for(i in 1:100) {
  test <- c(rep('Control', 10), rep('Treatment1', 10), rep('Treatment2', 10))
  resp <- c(runif(10, 0, 3), runif(10, 4, 7), runif(10, 8, 10))
  dat <- data.frame(test, resp)
  meanVec[i] <- shuffleData(dat)
}
print(meanVec)
##   [1] 2.00953429 2.31795926 0.28597725 0.51225917 0.19524863 2.20289565
##   [7] 0.16818725 0.92514687 1.02808039 1.35752853 2.57786023 0.32302952
##  [13] 2.05656835 2.66854219 1.97455787 0.22430546 0.04625406 2.92789186
##  [19] 1.71668239 1.24180578 1.09164192 1.81152406 1.51097442 0.05462300
##  [25] 2.91987714 0.42948115 1.55272583 0.11472291 0.20615838 2.98470117
##  [31] 0.07752895 0.47201578 1.14902974 1.15470653 1.60720392 0.20857347
##  [37] 0.69619700 2.31496960 2.18248163 2.14483770 1.87216278 1.29826779
##  [43] 1.07176740 0.54357470 2.67666221 0.93495388 2.29199354 0.45888763
##  [49] 1.91139658 1.93526027 0.17183671 0.72802288 2.22267411 0.33031001
##  [55] 2.69801168 0.89397647 2.84045583 1.34462929 0.88904884 1.82373501
##  [61] 0.97347288 2.19754628 1.98319745 1.72179557 1.16587121 2.57345903
##  [67] 0.92319957 0.51710350 0.72229655 1.83507933 0.91245457 2.21175226
##  [73] 0.90797498 1.83537599 1.84682439 1.53192194 1.53196271 1.87850416
##  [79] 0.76148287 1.28874629 0.64035569 1.22474882 0.37472617 2.23964574
##  [85] 0.31176474 2.72208966 0.79929183 1.06398632 2.25411264 1.58477715
##  [91] 1.78923115 1.59947038 2.67870290 1.13995003 1.90136952 0.61480681
##  [97] 0.18236542 0.37895816 0.86617714 2.72126793
qplot(x = meanVec)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.