Question 1

Using a for loop, write a function to calculate the number of zeroes in a numeric vector. Before entering the loop, set up a counter variable counter <- 0. Inside the loop, add 1 to counter each time you have a zero in the matrix. Finally, use return(counter) for the output.

#Create counter function#
count <- function(x){
  counter <- 0
  for(i in 1:length(x)){
    if(x[i] == 0){
      counter <- counter+1
    }
    else{
      counter <- counter+0
    }
  }
  return(counter)
}

#Create dummy data + run function
rv1 <- rep(0,20)
count(rv1)
## [1] 20
#created a vector of 20 zeros, counter returns 20 zeros

I created a function to count every 0. Then I made a mock vector consisting of 20 zeros. When I ran the function on this vector, it returned a count of 20, so I know the function works. Woohoo!

Question 2

Use subsetting instead of a loop to rewrite the function as a single line of code.

#create function
count2 <- function(x){
  return(length(x[which(x == 0)]))
}

#run function
rv2 <- c(rep(0,10),rep(1,10)) #create dataset with 10 zeros
count2(rv2) #returns count of 10
## [1] 10

I created a function that uses subsetting [] and the which() function to select only 0s from the vector. Then I use the length statement in place of a counter to tell us how many zeros there are.

Question 3

Write a function that takes as input two integers representing the number of rows and columns in a matrix. The output is a matrix of these dimensions in which each element is the product of the row number x the column number.

make_matrix <- function(a=4,b=5){
  m <- matrix(nrow = a, ncol = b)
  for(i in 1:nrow(m)){
    for(j in 1:ncol(m)){
      m[i,j] <- i*j
    }
  }
  return(m)
}
make_matrix()
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    2    4    6    8   10
## [3,]    3    6    9   12   15
## [4,]    4    8   12   16   20

Question 4

In the next few lectures, you will learn how to do a randomization test on your data. We will complete some of the steps today to practice calling custom functions within a for loop. Use the code from the March 31st lecture (Randomization Tests) to complete the following steps: a.) Simulate a dataset with 3 groups of data, each group drawn from a distribution with a different mean. The final data frame should have 1 column for group and 1 column for the response variable.

groupA <- cbind(rep('groupA',100),rnorm(n = 100, mean = 60, sd = 5))
groupB <- cbind(rep('groupB',100),rnorm(n = 100, mean = 45, sd = 5))
groupC <- cbind(rep('groupC',100),rnorm(n = 100, mean = 81, sd = 5))
df1 <- rbind(groupA,groupB,groupC)
df1
##        [,1]     [,2]              
##   [1,] "groupA" "60.253700615324" 
##   [2,] "groupA" "57.7702468986275"
##   [3,] "groupA" "60.5922874458184"
##   [4,] "groupA" "58.2335014350331"
##   [5,] "groupA" "56.9630371113766"
##   [6,] "groupA" "66.7430627306205"
##   [7,] "groupA" "64.7501642281992"
##   [8,] "groupA" "65.5868364067949"
##   [9,] "groupA" "47.3036520658878"
##  [10,] "groupA" "60.6377235037324"
##  [11,] "groupA" "63.8730434750239"
##  [12,] "groupA" "56.5433366583046"
##  [13,] "groupA" "58.4388709914737"
##  [14,] "groupA" "62.1407529790012"
##  [15,] "groupA" "63.2368568545219"
##  [16,] "groupA" "58.3035431867856"
##  [17,] "groupA" "60.0596339942669"
##  [18,] "groupA" "62.251746943177" 
##  [19,] "groupA" "55.743119110269" 
##  [20,] "groupA" "56.26142416322"  
##  [21,] "groupA" "53.8308935594701"
##  [22,] "groupA" "56.7561113172125"
##  [23,] "groupA" "48.9876380506777"
##  [24,] "groupA" "64.9269350258635"
##  [25,] "groupA" "61.8887062035854"
##  [26,] "groupA" "66.8151911919758"
##  [27,] "groupA" "58.8960387615353"
##  [28,] "groupA" "58.2753936232227"
##  [29,] "groupA" "53.4900113494662"
##  [30,] "groupA" "57.1569776059592"
##  [31,] "groupA" "62.1950930419412"
##  [32,] "groupA" "63.5405760178324"
##  [33,] "groupA" "62.8448221313861"
##  [34,] "groupA" "53.6655544140354"
##  [35,] "groupA" "64.0601267890241"
##  [36,] "groupA" "58.4020225976491"
##  [37,] "groupA" "56.5097774429993"
##  [38,] "groupA" "59.1891064085755"
##  [39,] "groupA" "67.0492032585431"
##  [40,] "groupA" "55.9024568897366"
##  [41,] "groupA" "56.0265296209706"
##  [42,] "groupA" "54.251088591235" 
##  [43,] "groupA" "63.9304674198329"
##  [44,] "groupA" "63.2972662362167"
##  [45,] "groupA" "54.6468792730316"
##  [46,] "groupA" "53.9480786689005"
##  [47,] "groupA" "60.0070674699"   
##  [48,] "groupA" "65.1898307102438"
##  [49,] "groupA" "61.9400030087745"
##  [50,] "groupA" "53.7454472690175"
##  [51,] "groupA" "61.6863409718493"
##  [52,] "groupA" "61.4465746955722"
##  [53,] "groupA" "56.3511034837828"
##  [54,] "groupA" "60.6344752993313"
##  [55,] "groupA" "57.8828819175026"
##  [56,] "groupA" "62.768096101437" 
##  [57,] "groupA" "60.3321365782773"
##  [58,] "groupA" "60.9496530244393"
##  [59,] "groupA" "64.0284122616633"
##  [60,] "groupA" "63.9044033627915"
##  [61,] "groupA" "63.9041764876471"
##  [62,] "groupA" "61.5345847661154"
##  [63,] "groupA" "56.808388002376" 
##  [64,] "groupA" "68.1687110107562"
##  [65,] "groupA" "57.8302540010143"
##  [66,] "groupA" "61.7913007867389"
##  [67,] "groupA" "53.2297718786151"
##  [68,] "groupA" "65.0649520705063"
##  [69,] "groupA" "59.714473088295" 
##  [70,] "groupA" "58.934256853279" 
##  [71,] "groupA" "59.0890543797531"
##  [72,] "groupA" "59.3467125933199"
##  [73,] "groupA" "55.2925838841666"
##  [74,] "groupA" "55.244500248267" 
##  [75,] "groupA" "50.1021402349977"
##  [76,] "groupA" "58.2699977894617"
##  [77,] "groupA" "67.4892489645275"
##  [78,] "groupA" "61.6567732591227"
##  [79,] "groupA" "55.5065194025226"
##  [80,] "groupA" "61.2500858835225"
##  [81,] "groupA" "69.7937992653095"
##  [82,] "groupA" "55.9345383342435"
##  [83,] "groupA" "60.2617698857335"
##  [84,] "groupA" "63.9371893653267"
##  [85,] "groupA" "64.0430453213541"
##  [86,] "groupA" "60.4890715966203"
##  [87,] "groupA" "63.8470434093944"
##  [88,] "groupA" "58.6527436047852"
##  [89,] "groupA" "55.6649104456577"
##  [90,] "groupA" "54.7993579658006"
##  [91,] "groupA" "63.7239366284331"
##  [92,] "groupA" "55.4875346306583"
##  [93,] "groupA" "47.6086315564214"
##  [94,] "groupA" "54.7126439667477"
##  [95,] "groupA" "61.7948673868331"
##  [96,] "groupA" "53.0511900394713"
##  [97,] "groupA" "61.498323656494" 
##  [98,] "groupA" "57.3363972728489"
##  [99,] "groupA" "52.869723078015" 
## [100,] "groupA" "60.5747951383412"
## [101,] "groupB" "42.0127121208645"
## [102,] "groupB" "43.7726719627646"
## [103,] "groupB" "49.3274182277483"
## [104,] "groupB" "51.8957319103724"
## [105,] "groupB" "38.6412956428827"
## [106,] "groupB" "38.5423053504525"
## [107,] "groupB" "47.3766701484476"
## [108,] "groupB" "46.8221641173563"
## [109,] "groupB" "46.7302313935118"
## [110,] "groupB" "35.4490853738854"
## [111,] "groupB" "44.6154004987943"
## [112,] "groupB" "40.8514669326195"
## [113,] "groupB" "41.7272928359017"
## [114,] "groupB" "39.347304491356" 
## [115,] "groupB" "41.5373566582093"
## [116,] "groupB" "46.8670041950352"
## [117,] "groupB" "40.9318742520295"
## [118,] "groupB" "46.3021584121236"
## [119,] "groupB" "40.8466984752176"
## [120,] "groupB" "39.9758038932255"
## [121,] "groupB" "50.9346600906734"
## [122,] "groupB" "44.8790172480369"
## [123,] "groupB" "39.8627463608982"
## [124,] "groupB" "44.3228521656661"
## [125,] "groupB" "42.2870505859816"
## [126,] "groupB" "37.9073388205075"
## [127,] "groupB" "44.4500582606151"
## [128,] "groupB" "39.8485050167344"
## [129,] "groupB" "47.1801682484288"
## [130,] "groupB" "34.6612435821585"
## [131,] "groupB" "51.2594409806134"
## [132,] "groupB" "47.940099038639" 
## [133,] "groupB" "43.2055337136222"
## [134,] "groupB" "43.1047294139757"
## [135,] "groupB" "54.0373124046987"
## [136,] "groupB" "40.7026638915219"
## [137,] "groupB" "40.3485446604031"
## [138,] "groupB" "39.9362226477575"
## [139,] "groupB" "47.3000588528703"
## [140,] "groupB" "44.1427514039715"
## [141,] "groupB" "43.478725531437" 
## [142,] "groupB" "49.1548079786107"
## [143,] "groupB" "47.4876131755849"
## [144,] "groupB" "45.6557785435585"
## [145,] "groupB" "48.3502717311746"
## [146,] "groupB" "39.3728777241063"
## [147,] "groupB" "37.6171316358636"
## [148,] "groupB" "46.0539430941755"
## [149,] "groupB" "43.4019455136026"
## [150,] "groupB" "37.4329240111608"
## [151,] "groupB" "37.7440684499174"
## [152,] "groupB" "55.0988602457309"
## [153,] "groupB" "51.7359800257631"
## [154,] "groupB" "44.0570841306579"
## [155,] "groupB" "41.8809574985452"
## [156,] "groupB" "42.9401858873554"
## [157,] "groupB" "50.5898551953362"
## [158,] "groupB" "39.1334579862811"
## [159,] "groupB" "49.06468824366"  
## [160,] "groupB" "47.378933460443" 
## [161,] "groupB" "49.3520248586407"
## [162,] "groupB" "46.8425982115543"
## [163,] "groupB" "40.7247927885745"
## [164,] "groupB" "52.295475256145" 
## [165,] "groupB" "35.8707935670473"
## [166,] "groupB" "49.6647575863111"
## [167,] "groupB" "41.4896004932378"
## [168,] "groupB" "41.891120577354" 
## [169,] "groupB" "51.5515728621105"
## [170,] "groupB" "46.4316712860057"
## [171,] "groupB" "48.3478571008229"
## [172,] "groupB" "47.3312290970181"
## [173,] "groupB" "54.4611723015378"
## [174,] "groupB" "44.1476341214664"
## [175,] "groupB" "46.5124831826073"
## [176,] "groupB" "44.5532880768693"
## [177,] "groupB" "44.460532641042" 
## [178,] "groupB" "35.6385045989743"
## [179,] "groupB" "48.8954183280248"
## [180,] "groupB" "47.0153124755398"
## [181,] "groupB" "48.649831405295" 
## [182,] "groupB" "42.8549628146104"
## [183,] "groupB" "52.3691245816777"
## [184,] "groupB" "41.8039237576157"
## [185,] "groupB" "40.0471838415301"
## [186,] "groupB" "39.1910573058129"
## [187,] "groupB" "41.073825319174" 
## [188,] "groupB" "54.3424966025612"
## [189,] "groupB" "40.5230467871868"
## [190,] "groupB" "40.9702643166225"
## [191,] "groupB" "45.8333379544583"
## [192,] "groupB" "48.2812102962904"
## [193,] "groupB" "43.9326804160683"
## [194,] "groupB" "37.3919976199224"
## [195,] "groupB" "56.7087776817592"
## [196,] "groupB" "45.9703333833895"
## [197,] "groupB" "51.4133534920431"
## [198,] "groupB" "45.6390392286454"
## [199,] "groupB" "48.4953385733006"
## [200,] "groupB" "46.4591687327162"
## [201,] "groupC" "79.2856471361043"
## [202,] "groupC" "82.8171065247143"
## [203,] "groupC" "73.2920085283571"
## [204,] "groupC" "73.2114602494743"
## [205,] "groupC" "75.0515691987735"
## [206,] "groupC" "79.285389401081" 
## [207,] "groupC" "71.2871522594158"
## [208,] "groupC" "77.7375686263766"
## [209,] "groupC" "85.6819697307991"
## [210,] "groupC" "80.8175108738112"
## [211,] "groupC" "87.414443871034" 
## [212,] "groupC" "94.5872354219441"
## [213,] "groupC" "72.757502226626" 
## [214,] "groupC" "90.3981422925825"
## [215,] "groupC" "78.5798799984674"
## [216,] "groupC" "71.9100743338744"
## [217,] "groupC" "87.6149612058521"
## [218,] "groupC" "83.4537522068921"
## [219,] "groupC" "85.9847972540295"
## [220,] "groupC" "77.4669486112216"
## [221,] "groupC" "82.4926179654562"
## [222,] "groupC" "84.3307762097815"
## [223,] "groupC" "83.5863920412736"
## [224,] "groupC" "85.2018137705448"
## [225,] "groupC" "85.2070916436042"
## [226,] "groupC" "78.5740164614705"
## [227,] "groupC" "75.687469093989" 
## [228,] "groupC" "82.3063663516868"
## [229,] "groupC" "84.497528923929" 
## [230,] "groupC" "77.2401512970411"
## [231,] "groupC" "86.3253223848664"
## [232,] "groupC" "82.1953898813537"
## [233,] "groupC" "80.3717047868767"
## [234,] "groupC" "72.3311908791932"
## [235,] "groupC" "87.2959785024082"
## [236,] "groupC" "79.0425835178979"
## [237,] "groupC" "83.4135290206959"
## [238,] "groupC" "78.0458313179997"
## [239,] "groupC" "75.2540428082108"
## [240,] "groupC" "71.0985388008034"
## [241,] "groupC" "75.4741506383577"
## [242,] "groupC" "79.2767200647281"
## [243,] "groupC" "84.9701443164084"
## [244,] "groupC" "82.5381699896876"
## [245,] "groupC" "85.0371404277255"
## [246,] "groupC" "76.5848929558827"
## [247,] "groupC" "84.0892875571752"
## [248,] "groupC" "78.3879437000265"
## [249,] "groupC" "83.1195494106602"
## [250,] "groupC" "77.3336638519019"
## [251,] "groupC" "86.2311562692452"
## [252,] "groupC" "93.2228913379428"
## [253,] "groupC" "78.7878917856773"
## [254,] "groupC" "81.4026449930129"
## [255,] "groupC" "91.0368123222184"
## [256,] "groupC" "83.8577284807707"
## [257,] "groupC" "83.4338123662754"
## [258,] "groupC" "72.8443518881837"
## [259,] "groupC" "72.8370583259913"
## [260,] "groupC" "83.6084173236494"
## [261,] "groupC" "77.5213126710593"
## [262,] "groupC" "85.4045477015979"
## [263,] "groupC" "79.5904641064427"
## [264,] "groupC" "81.8120445663885"
## [265,] "groupC" "79.4458339992621"
## [266,] "groupC" "79.4452485111593"
## [267,] "groupC" "80.0640105761596"
## [268,] "groupC" "77.4185169149421"
## [269,] "groupC" "76.9291458783723"
## [270,] "groupC" "84.0687842380864"
## [271,] "groupC" "81.7220104674822"
## [272,] "groupC" "73.6892232861063"
## [273,] "groupC" "88.275240288043" 
## [274,] "groupC" "83.7172261669244"
## [275,] "groupC" "87.0014338950962"
## [276,] "groupC" "89.0831444199873"
## [277,] "groupC" "85.4017493245009"
## [278,] "groupC" "81.4345671721032"
## [279,] "groupC" "84.034198764548" 
## [280,] "groupC" "77.0226959957706"
## [281,] "groupC" "81.683849447921" 
## [282,] "groupC" "84.1395848805419"
## [283,] "groupC" "82.4905473063294"
## [284,] "groupC" "75.07223582438"  
## [285,] "groupC" "77.5166676883989"
## [286,] "groupC" "78.7182526340465"
## [287,] "groupC" "89.1178987262114"
## [288,] "groupC" "85.2184216585152"
## [289,] "groupC" "77.0560833840919"
## [290,] "groupC" "81.9727109581137"
## [291,] "groupC" "74.1771368619339"
## [292,] "groupC" "91.2850031558981"
## [293,] "groupC" "79.4142953368025"
## [294,] "groupC" "84.8701123163485"
## [295,] "groupC" "81.4061731772551"
## [296,] "groupC" "80.7705137764519"
## [297,] "groupC" "83.5861873120592"
## [298,] "groupC" "88.4984610446743"
## [299,] "groupC" "70.8889644340233"
## [300,] "groupC" "75.7523822098773"
colnames(df1) <- c('group','variable')
df1 <- as.data.frame(df1)
df1$variable <- as.numeric(df1$variable)

b.) Write a custom function that 1) reshuffles the response variable, and 2) calculates the mean of each group in the reshuffled data. Store the means in a vector of length 3.

library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.3
## 
## Attaching package: 'dplyr'
## The following object is masked _by_ '.GlobalEnv':
## 
##     count
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
shuffle <- function(df = df1){
  colnames(df) <- c('group','variable')
  variable <- (sample(df$variable, replace = F))
  group <- df$group
  df2 <- data.frame(group,variable)
  df3 <- df2 %>% group_by(group) %>% summarize(mean = mean(variable))
  return(df3)
}
shuffle(df1)
## # A tibble: 3 x 2
##   group   mean
##   <chr>  <dbl>
## 1 groupA  61.6
## 2 groupB  61.6
## 3 groupC  62.0

c.) Use a for loop to repeat the function in b 100 times. Store the results in a data frame that has 1 column indicating the replicate number and 1 column for each new group mean, for a total of 4 columns.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.3
## -- 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.3
## Warning: package 'tibble' was built under R version 4.1.3
## Warning: package 'tidyr' was built under R version 4.1.3
## Warning: package 'readr' was built under R version 4.1.3
## Warning: package 'purrr' was built under R version 4.1.3
## Warning: package 'stringr' was built under R version 4.1.3
## Warning: package 'forcats' was built under R version 4.1.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
results <- data.frame()
for(i in 1:100){
  df3 <- shuffle(df1)
  df3 <- df3 %>% pivot_wider(names_from = group, values_from = mean)
  #rownames_to_column(df3)
  df3 <-cbind(i,df3)
  results <- rbind(results, df3)
}
results  
##       i   groupA   groupB   groupC
## 1     1 60.57271 62.65417 62.03525
## 2     2 63.60456 61.07764 60.57993
## 3     3 64.60746 61.57030 59.08438
## 4     4 62.94881 60.11078 62.20254
## 5     5 63.76257 59.53625 61.96331
## 6     6 61.98128 61.16206 62.11880
## 7     7 62.52573 62.12671 60.60969
## 8     8 62.55096 59.31268 63.39849
## 9     9 60.37746 61.59642 63.28825
## 10   10 62.37006 62.13160 60.76048
## 11   11 63.30200 62.14144 59.81869
## 12   12 60.23848 62.19740 62.82625
## 13   13 59.97127 63.61077 61.68009
## 14   14 60.84141 63.78658 60.63415
## 15   15 62.06499 62.28335 60.91379
## 16   16 59.73437 65.84061 59.68715
## 17   17 60.00135 63.23426 62.02652
## 18   18 60.35438 61.91546 62.99230
## 19   19 62.63120 59.44860 63.18233
## 20   20 61.01956 61.91715 62.32542
## 21   21 61.07364 61.46210 62.72639
## 22   22 60.92328 61.55669 62.78217
## 23   23 61.50998 62.73375 61.01841
## 24   24 60.47716 61.43432 63.35065
## 25   25 60.85491 61.37483 63.03239
## 26   26 63.42172 58.52013 63.32029
## 27   27 64.15163 59.45047 61.66003
## 28   28 62.39331 60.87441 61.99442
## 29   29 63.79339 60.30591 61.16284
## 30   30 61.01742 61.73677 62.50794
## 31   31 63.77192 59.68457 61.80563
## 32   32 61.02758 62.63286 61.60169
## 33   33 63.20733 61.97837 60.07643
## 34   34 62.15719 62.70752 60.39742
## 35   35 59.94167 60.93368 64.38678
## 36   36 63.80308 61.99425 59.46480
## 37   37 60.65175 61.93497 62.67541
## 38   38 62.88468 61.17071 61.20675
## 39   39 61.26606 63.30127 60.69480
## 40   40 61.31500 62.89238 61.05475
## 41   41 60.79264 62.14620 62.32329
## 42   42 61.45697 60.37428 63.43089
## 43   43 63.03152 61.19249 61.03812
## 44   44 62.87919 63.03101 59.35193
## 45   45 62.50838 61.46042 61.29333
## 46   46 60.23134 62.93512 62.09568
## 47   47 59.99260 63.20666 62.06287
## 48   48 60.09725 65.47272 59.69217
## 49   49 62.65823 61.59775 61.00615
## 50   50 62.27049 61.21446 61.77718
## 51   51 61.11077 61.40977 62.74159
## 52   52 61.99112 59.54990 63.72111
## 53   53 60.86744 63.01368 61.38101
## 54   54 62.36012 62.47514 60.42687
## 55   55 60.47572 64.01045 60.77596
## 56   56 63.11579 62.71174 59.43460
## 57   57 62.23749 61.99440 61.03024
## 58   58 59.26544 62.13924 63.85745
## 59   59 60.78086 61.83284 62.64844
## 60   60 62.89887 60.45764 61.90562
## 61   61 58.64581 63.95164 62.66468
## 62   62 62.32951 61.80338 61.12924
## 63   63 63.94004 61.36253 59.95956
## 64   64 61.91081 63.62086 59.73046
## 65   65 62.33923 63.26424 59.65866
## 66   66 62.62157 61.68013 60.96043
## 67   67 62.93362 60.42536 61.90315
## 68   68 64.17382 60.72898 60.35933
## 69   69 58.58927 63.30640 63.36647
## 70   70 61.82066 61.56981 61.87166
## 71   71 62.69699 60.99916 61.56598
## 72   72 61.25234 61.70092 62.30887
## 73   73 60.02831 62.92935 62.30447
## 74   74 63.46900 60.53612 61.25701
## 75   75 63.37184 61.21894 60.67136
## 76   76 61.55050 62.98174 60.72989
## 77   77 62.85796 62.88045 59.52372
## 78   78 60.20928 60.75438 64.29847
## 79   79 62.32914 62.17564 60.75736
## 80   80 61.38779 62.33992 61.53442
## 81   81 60.21795 61.47901 63.56517
## 82   82 63.83165 61.17930 60.25118
## 83   83 58.87870 64.31353 62.06990
## 84   84 63.13540 60.28956 61.83717
## 85   85 63.52743 62.26903 59.46567
## 86   86 61.88707 60.12502 63.25004
## 87   87 65.10561 59.58993 60.56659
## 88   88 60.84527 61.69772 62.71914
## 89   89 63.47016 60.46795 61.32403
## 90   90 64.87488 60.81351 59.57374
## 91   91 59.24618 64.28975 61.72620
## 92   92 61.92001 63.16692 60.17520
## 93   93 63.43852 60.26938 61.55424
## 94   94 59.66428 61.94733 63.65051
## 95   95 62.73204 62.14639 60.38370
## 96   96 61.12922 62.38806 61.74485
## 97   97 63.04104 60.48584 61.73525
## 98   98 61.15375 60.42406 63.68431
## 99   99 61.48145 60.13766 63.64302
## 100 100 60.17001 63.70929 61.38283

d.) Use qplot() to create a histogram of the means for each reshuffled group. Or, if you want a challenge, use ggplot() to overlay all 3 histograms in the same figure. How do the distributions of reshuffled means compare to the original means?

library(ggplot2)
xlabel <- c('GroupA','GroupB','GroupC')
loop.vector <- 2:4
for( i in loop.vector){
  plot <- qplot(results[,i], geom='histogram',xlab = xlabel[i-1])
  print(plot)
}
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

I choose to go a slightly different route to add a challenge by making all 3 graphs at once using a for loop. I also added in a line of code so that all my histograms would have a properly labeled x axis. Based on the original meanse assigned to each group (A = 60, B = 45, C = 81), the distributions for all reshuffled groups are slightly off. Group A seems to have an almost normal distirubtion with a mean somewhere below 60. Group B has a more skewed distribution with some potential outliers. Group C has a fairly normaly distribution but is far from its original mean of 81.