Homework 10

  1. Setting counter variable and for loop to calculate the number of zeros in a numeric vector
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.2
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v stringr 1.4.0
## v tidyr   1.2.0     v forcats 0.5.1
## v readr   2.1.2
## Warning: package 'ggplot2' was built under R version 4.1.2
## Warning: package 'tibble' was built under R version 4.1.2
## Warning: package 'tidyr' was built under R version 4.1.2
## Warning: package 'readr' was built under R version 4.1.2
## Warning: package 'purrr' was built under R version 4.1.2
## Warning: package 'stringr' was built under R version 4.1.2
## Warning: package 'forcats' was built under R version 4.1.2
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
####################################################
# FUNCTION: count_zeros
# purpose: calculate the number of zeroes in a numeric vector
# input: numeric vector
# output: numeric value
# ----------------------------------
count_zeros <- function(size=100) {
  num_vec <- floor(runif(size,
                         min=0,
                         max=10))
  counter <- 0
  for(i in 1:length(num_vec)){
    if(num_vec[i]==0){
      counter <- (counter + 1)
    }
  }
return(counter)
}

count_zeros()
## [1] 10
  1. Subsetting instead for question 1
num_vec <- floor(runif(100, min=0, max=10))
sum(num_vec==0)
## [1] 7

Question 3

####################################################
# FUNCTION: matrix_math
# purpose: multiply row and column numbers in matrix
# input: two integers representing the number of rows and columns in a matrix
# output:  matrix of these dimensions
# ----------------------------------

two_integers <- floor(runif(2,
                         min=1,
                         max=5))

matrix_math <- function(x=NULL,y=NULL) {
  new_matrix <- matrix(data=NA,
                       nrow=x,
                       ncol=y)
  for(i in 1:nrow(new_matrix)){
    for(j in 1:ncol(new_matrix)){
    new_matrix[i,j] <- i*j
    }
  }
  return(new_matrix)
}

x1 <- two_integers[1]
y1 <- two_integers[2]
matrix_math(x=x1,y=y1)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    2    4    6
## [3,]    3    6    9

Question 4

dataset <- c(rep("Group1",3),
             rep("Group2",3),
             rep("Group3",3))
print(dataset)
## [1] "Group1" "Group1" "Group1" "Group2" "Group2" "Group2" "Group3" "Group3"
## [9] "Group3"
response <- c(runif(4) +1, runif(5) + 10)

df1 <- data.frame(Group=dataset, Response.Variable=response)
print(df)
## function (x, df1, df2, ncp, log = FALSE) 
## {
##     if (missing(ncp)) 
##         .Call(C_df, x, df1, df2, log)
##     else .Call(C_dnf, x, df1, df2, ncp, log)
## }
## <bytecode: 0x0000000023e79db0>
## <environment: namespace:stats>
####################################################
# FUNCTION: 4b
# purpose: reshuffles the response variable
# calculates the mean of each group in the reshuffled data
# and store the means
# input: numeric vector
# output: vector of length 3
# ----------------------------------
four_b <- function(df=df1) {
  df2 <- data.frame(Group=df1$Group, Response.Variable=sample(x=df1$Response.Variable,size=length(df1$Response.Variable)))
  with_means <- group_by(df2, Group)
  grouped_data <- summarize(with_means, meanRes = mean(Response.Variable, na.rm=T), N = n())
  return(grouped_data)
}

stats <- data.frame(NA)


for(i in 1:100){
  stats[i,2:4]<- four_b() %>%
  pivot_wider(names_from=Group,
              values_from = meanRes) %>%
  select(Group1:Group3)
stats[i,1] <- i

}

names(stats)[1] <- "Rep"
print(stats)
##     Rep    Group1    Group2    Group3
## 1     1  7.542136  4.431518  7.475646
## 2     2  1.338706 10.545603  7.564991
## 3     3  4.154976  7.769701  7.524623
## 4     4  1.286965  7.671698 10.490638
## 5     5  7.391637  1.286965 10.770699
## 6     6  7.397532  7.593877  4.457892
## 7     7  4.281774  4.529815 10.637712
## 8     8  4.365783  4.220710 10.862807
## 9     9  4.500771 10.582746  4.365783
## 10   10  7.527849  7.489640  4.431811
## 11   11  7.602444  1.286965 10.559891
## 12   12  7.619957  7.299528  4.529815
## 13   13  7.262075  7.671698  4.515528
## 14   14  4.394065  7.368781  7.686454
## 15   15  4.449031 10.490638  4.509632
## 16   16  4.431518  7.475646  7.542136
## 17   17  4.431518 10.637712  4.380071
## 18   18  4.457892  4.500771 10.490638
## 19   19  7.368781  4.529815  7.550704
## 20   20  7.593877  4.304720  7.550704
## 21   21  4.394065  7.475646  7.579589
## 22   22  4.070966  7.622627  7.755707
## 23   23  7.671698  4.380071  7.397532
## 24   24  7.608633  4.070966  7.769701
## 25   25  4.281774 10.582746  4.584781
## 26   26  4.435036  7.602444  7.411820
## 27   27  4.379777  7.593877  7.475646
## 28   28  7.542136  7.475646  4.431518
## 29   29  7.452791  7.616732  4.379777
## 30   30  4.154976  4.445805 10.848519
## 31   31  4.380071  7.602444  7.466785
## 32   32  7.671698  7.262075  4.515528
## 33   33  4.523626  7.262075  7.663599
## 34   34  4.394065  4.206716 10.848519
## 35   35  7.622627  7.391637  4.435036
## 36   36  4.435036 10.582746  4.431518
## 37   37  1.338706 10.559891  7.550704
## 38   38  4.168970  4.417524 10.862807
## 39   39  7.475646  1.202955 10.770699
## 40   40  7.593877 10.568459  1.286965
## 41   41  4.435036  4.220710 10.793554
## 42   42  4.351027 10.568459  4.529815
## 43   43  7.472883  4.486777  7.489640
## 44   44  4.380071 10.848519  4.220710
## 45   45  7.313816  4.471886  7.663599
## 46   46  7.489640  7.527849  4.431811
## 47   47  4.431518 10.582746  4.435036
## 48   48  7.489640  7.593877  4.365783
## 49   49  4.445805 10.637712  4.365783
## 50   50 10.545603  7.616732  1.286965
## 51   51  7.391637  7.527849  4.529815
## 52   52  7.663599  4.445805  7.339896
## 53   53  4.500771  7.383538  7.564991
## 54   54  1.188961 10.637712  7.622627
## 55   55 10.582746  7.579589  1.286965
## 56   56  4.435036  7.397532  7.616732
## 57   57  4.394065  4.509632 10.545603
## 58   58  1.286965  7.593877 10.568459
## 59   59  4.373882 10.559891  4.515528
## 60   60  4.584781  4.296061 10.568459
## 61   61  7.542136  4.220710  7.686454
## 62   62  7.691880  4.281774  7.475646
## 63   63  4.296061  4.515528 10.637712
## 64   64  7.452791  7.602444  4.394065
## 65   65  7.677593  7.262075  4.509632
## 66   66  4.529815  7.339896  7.579589
## 67   67  7.313816  7.686454  4.449031
## 68   68 10.862807  4.206716  4.379777
## 69   69  4.435036  7.411820  7.602444
## 70   70  7.700742  4.168970  7.579589
## 71   71  7.452791  4.523626  7.472883
## 72   72  7.677593  4.296061  7.475646
## 73   73  4.380071  7.299528  7.769701
## 74   74 10.559891  1.286965  7.602444
## 75   75 10.545603  1.338706  7.564991
## 76   76  7.391637  4.584781  7.472883
## 77   77  4.486777  7.247788  7.714736
## 78   78  7.564991  4.417524  7.466785
## 79   79  7.368781  4.379777  7.700742
## 80   80  7.247788  7.671698  4.529815
## 81   81  7.677593  7.564991  4.206716
## 82   82  7.677886  7.391637  4.379777
## 83   83  7.677886  4.431518  7.339896
## 84   84  7.299528  7.700742  4.449031
## 85   85  4.070966 10.848519  4.529815
## 86   86  4.471886  7.383538  7.593877
## 87   87  4.220710 10.862807  4.365783
## 88   88  1.286965 10.862807  7.299528
## 89   89  7.677593  7.616732  4.154976
## 90   90  7.593877  7.475646  4.379777
## 91   91  7.579589  7.714736  4.154976
## 92   92  7.317041  4.607636  7.524623
## 93   93  7.691880  4.281774  7.475646
## 94   94  4.379777  7.616732  7.452791
## 95   95  7.391637  7.663599  4.394065
## 96   96  4.449031  7.391637  7.608633
## 97   97  7.472883  7.466785  4.509632
## 98   98  4.304720 10.862807  4.281774
## 99   99  7.593877  7.383538  4.471886
## 100 100  7.313816  7.677593  4.457892
qplot(stats$Group1)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(stats$Group2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(stats$Group3)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.