# Libraries for this assignment

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(purrr)
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows

1. Some data with Flippers

1a. Load the library palmerpenguins after installing it

library(palmerpenguins)            # Load library palmerpenguins

1b. Show the head of the dataset penguins

penguins %>%                       # penguins dataset
  head() %>%                       # head of penguins dataset
  kbl() %>%                        
  kable_styling()                  # Using KableExtra table formatting
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Torgersen 39.1 18.7 181 3750 male 2007
Adelie Torgersen 39.5 17.4 186 3800 female 2007
Adelie Torgersen 40.3 18.0 195 3250 female 2007
Adelie Torgersen NA NA NA NA NA 2007
Adelie Torgersen 36.7 19.3 193 3450 female 2007
Adelie Torgersen 39.3 20.6 190 3650 male 2007

1c. What do you learn by using str() and summary() on penguins()

penguins %>%                         
  summary()                      # Summary of penguins dataset 
##       species          island    bill_length_mm  bill_depth_mm  
##  Adelie   :152   Biscoe   :168   Min.   :32.10   Min.   :13.10  
##  Chinstrap: 68   Dream    :124   1st Qu.:39.23   1st Qu.:15.60  
##  Gentoo   :124   Torgersen: 52   Median :44.45   Median :17.30  
##                                  Mean   :43.92   Mean   :17.15  
##                                  3rd Qu.:48.50   3rd Qu.:18.70  
##                                  Max.   :59.60   Max.   :21.50  
##                                  NA's   :2       NA's   :2      
##  flipper_length_mm  body_mass_g       sex           year     
##  Min.   :172.0     Min.   :2700   female:165   Min.   :2007  
##  1st Qu.:190.0     1st Qu.:3550   male  :168   1st Qu.:2007  
##  Median :197.0     Median :4050   NA's  : 11   Median :2008  
##  Mean   :200.9     Mean   :4202                Mean   :2008  
##  3rd Qu.:213.0     3rd Qu.:4750                3rd Qu.:2009  
##  Max.   :231.0     Max.   :6300                Max.   :2009  
##  NA's   :2         NA's   :2

What the summary shows;

  1. It shows the descriptive statistics of the dataset. This includes the min, max, mean, median, and quartiles of each column.

  2. It shows the names of each column in the dataset

  3. It also identifies columns with missing values(NA)

penguins %>%
  str()                           # structure of penguins dataset
## tibble [344 x 8] (S3: tbl_df/tbl/data.frame)
##  $ species          : Factor w/ 3 levels "Adelie","Chinstrap",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ island           : Factor w/ 3 levels "Biscoe","Dream",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ bill_length_mm   : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ...
##  $ bill_depth_mm    : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ...
##  $ flipper_length_mm: int [1:344] 181 186 195 NA 193 190 181 195 193 190 ...
##  $ body_mass_g      : int [1:344] 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ...
##  $ sex              : Factor w/ 2 levels "female","male": 2 1 1 NA 1 2 1 2 NA NA ...
##  $ year             : int [1:344] 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ...

What the structure explains;

  1. It shows the dimensions of the dataset = 344 x 8

  2. It shows the class of each column. For example; ‘species’, ‘island’ and ‘sex’ are factors.

1d. What are the quantiles of bill depth across the whole data set? What do those quantiles mean?

penguins %>%                      # load penguins data
  select(bill_depth_mm) %>%       # select the bill depth column using the select() from dplyr.
  quantile(na.rm = TRUE)          # calculate the quantiles using the quantile()
##   0%  25%  50%  75% 100% 
## 13.1 15.6 17.3 18.7 21.5

The quantiles divide the distribution of bill_depth_mm into equal groups. 50% shows the median while 75% and 100% shows the median of the lower and upper half of the data.

2. DIPLYR

2a. If I have a vector, c(1,4,7,NA,9), what is its mean? Note, the NA is going to cause a problem. Look at ?mean to learn how to solve it.

c(1,4,7,NA,9) %>%                 # create a vector
mean(na.rm = TRUE)                # calculate the mean using the mean() and setting na.rm = TRUE to remove missing values
## [1] 5.25

2b. What is the mean, sd, and median of body mass across the data set? Note, these NAs are going to cause some problems, so you might need to look at the documentation for the relevant functions.

penguins %>%                                         # load penguin data
  summarise(
    mean_body_mass = mean(body_mass_g, na.rm = TRUE),# Calculate the mean, sd and median of the body mass and summarize results into a data frame 
    sd_body_mass = sd(body_mass_g, na.rm = TRUE),
    med_body_mass = median(body_mass_g, na.rm = TRUE)) %>%
  kbl() %>%                        
  kable_styling()                  # Using KableExtra table formatting
mean_body_mass sd_body_mass med_body_mass
4201.754 801.9545 4050

2c. Repeat 2b, but, show us how these quantities differ by species

penguins %>%
  group_by(species) %>% #group all species in the data using group_by function
  summarise(
    mean_body_mass = mean(body_mass_g, na.rm = TRUE),  # repeat 2b above
    sd_body_mass = sd(body_mass_g, na.rm = TRUE),
    med_body_mass = median(body_mass_g, na.rm = TRUE)) %>%
  kbl() %>%                        
  kable_styling()                  # Using KableExtra table formatting
## `summarise()` ungrouping output (override with `.groups` argument)
species mean_body_mass sd_body_mass med_body_mass
Adelie 3700.662 458.5661 3700
Chinstrap 3733.088 384.3351 3700
Gentoo 5076.016 504.1162 5000

2d. Repeat 2c, but just for Biscoe island. What is different in the results?

penguins %>%
  filter(island == "Biscoe") %>% # filtered out Biscoe island using the filter()
  group_by(species, island) %>%  # Grouped species and island from dataset
  summarise(
    mean_body_mass = mean(body_mass_g, na.rm = TRUE),  # same as 2c above
    sd_body_mass = sd(body_mass_g, na.rm = TRUE),
    med_body_mass = median(body_mass_g, na.rm = TRUE)) %>%
kbl() %>%                        
  kable_styling()                  # Using KableExtra table formatting
## `summarise()` regrouping output by 'species' (override with `.groups` argument)
species island mean_body_mass sd_body_mass med_body_mass
Adelie Biscoe 3709.659 487.7337 3750
Gentoo Biscoe 5076.016 504.1162 5000

The ‘Chinstrap’ specie is not included in the summarized dataframe. It can be inferred that the Chinstrap species is not found in Biscoe island.

2e. Make a species-island column in penguins using paste(). This is an awesome function that takes multiple strings, and slams them together using the argument sep = to define how the string should be combined.

penguins %>%    # pipe penguin data
group_by(species, island) %>% # group the species and island column together
mutate('species-island' = paste(as.character(species), as.character(island), sep = "_")) %>%   # create a new dataframe with 'species' island column using mutate() and paste(sep = "_")
kbl() %>%                        
kable_styling() # Using KableExtra table formatting
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year species-island
Adelie Torgersen 39.1 18.7 181 3750 male 2007 Adelie_Torgersen
Adelie Torgersen 39.5 17.4 186 3800 female 2007 Adelie_Torgersen
Adelie Torgersen 40.3 18.0 195 3250 female 2007 Adelie_Torgersen
Adelie Torgersen NA NA NA NA NA 2007 Adelie_Torgersen
Adelie Torgersen 36.7 19.3 193 3450 female 2007 Adelie_Torgersen
Adelie Torgersen 39.3 20.6 190 3650 male 2007 Adelie_Torgersen
Adelie Torgersen 38.9 17.8 181 3625 female 2007 Adelie_Torgersen
Adelie Torgersen 39.2 19.6 195 4675 male 2007 Adelie_Torgersen
Adelie Torgersen 34.1 18.1 193 3475 NA 2007 Adelie_Torgersen
Adelie Torgersen 42.0 20.2 190 4250 NA 2007 Adelie_Torgersen
Adelie Torgersen 37.8 17.1 186 3300 NA 2007 Adelie_Torgersen
Adelie Torgersen 37.8 17.3 180 3700 NA 2007 Adelie_Torgersen
Adelie Torgersen 41.1 17.6 182 3200 female 2007 Adelie_Torgersen
Adelie Torgersen 38.6 21.2 191 3800 male 2007 Adelie_Torgersen
Adelie Torgersen 34.6 21.1 198 4400 male 2007 Adelie_Torgersen
Adelie Torgersen 36.6 17.8 185 3700 female 2007 Adelie_Torgersen
Adelie Torgersen 38.7 19.0 195 3450 female 2007 Adelie_Torgersen
Adelie Torgersen 42.5 20.7 197 4500 male 2007 Adelie_Torgersen
Adelie Torgersen 34.4 18.4 184 3325 female 2007 Adelie_Torgersen
Adelie Torgersen 46.0 21.5 194 4200 male 2007 Adelie_Torgersen
Adelie Biscoe 37.8 18.3 174 3400 female 2007 Adelie_Biscoe
Adelie Biscoe 37.7 18.7 180 3600 male 2007 Adelie_Biscoe
Adelie Biscoe 35.9 19.2 189 3800 female 2007 Adelie_Biscoe
Adelie Biscoe 38.2 18.1 185 3950 male 2007 Adelie_Biscoe
Adelie Biscoe 38.8 17.2 180 3800 male 2007 Adelie_Biscoe
Adelie Biscoe 35.3 18.9 187 3800 female 2007 Adelie_Biscoe
Adelie Biscoe 40.6 18.6 183 3550 male 2007 Adelie_Biscoe
Adelie Biscoe 40.5 17.9 187 3200 female 2007 Adelie_Biscoe
Adelie Biscoe 37.9 18.6 172 3150 female 2007 Adelie_Biscoe
Adelie Biscoe 40.5 18.9 180 3950 male 2007 Adelie_Biscoe
Adelie Dream 39.5 16.7 178 3250 female 2007 Adelie_Dream
Adelie Dream 37.2 18.1 178 3900 male 2007 Adelie_Dream
Adelie Dream 39.5 17.8 188 3300 female 2007 Adelie_Dream
Adelie Dream 40.9 18.9 184 3900 male 2007 Adelie_Dream
Adelie Dream 36.4 17.0 195 3325 female 2007 Adelie_Dream
Adelie Dream 39.2 21.1 196 4150 male 2007 Adelie_Dream
Adelie Dream 38.8 20.0 190 3950 male 2007 Adelie_Dream
Adelie Dream 42.2 18.5 180 3550 female 2007 Adelie_Dream
Adelie Dream 37.6 19.3 181 3300 female 2007 Adelie_Dream
Adelie Dream 39.8 19.1 184 4650 male 2007 Adelie_Dream
Adelie Dream 36.5 18.0 182 3150 female 2007 Adelie_Dream
Adelie Dream 40.8 18.4 195 3900 male 2007 Adelie_Dream
Adelie Dream 36.0 18.5 186 3100 female 2007 Adelie_Dream
Adelie Dream 44.1 19.7 196 4400 male 2007 Adelie_Dream
Adelie Dream 37.0 16.9 185 3000 female 2007 Adelie_Dream
Adelie Dream 39.6 18.8 190 4600 male 2007 Adelie_Dream
Adelie Dream 41.1 19.0 182 3425 male 2007 Adelie_Dream
Adelie Dream 37.5 18.9 179 2975 NA 2007 Adelie_Dream
Adelie Dream 36.0 17.9 190 3450 female 2007 Adelie_Dream
Adelie Dream 42.3 21.2 191 4150 male 2007 Adelie_Dream
Adelie Biscoe 39.6 17.7 186 3500 female 2008 Adelie_Biscoe
Adelie Biscoe 40.1 18.9 188 4300 male 2008 Adelie_Biscoe
Adelie Biscoe 35.0 17.9 190 3450 female 2008 Adelie_Biscoe
Adelie Biscoe 42.0 19.5 200 4050 male 2008 Adelie_Biscoe
Adelie Biscoe 34.5 18.1 187 2900 female 2008 Adelie_Biscoe
Adelie Biscoe 41.4 18.6 191 3700 male 2008 Adelie_Biscoe
Adelie Biscoe 39.0 17.5 186 3550 female 2008 Adelie_Biscoe
Adelie Biscoe 40.6 18.8 193 3800 male 2008 Adelie_Biscoe
Adelie Biscoe 36.5 16.6 181 2850 female 2008 Adelie_Biscoe
Adelie Biscoe 37.6 19.1 194 3750 male 2008 Adelie_Biscoe
Adelie Biscoe 35.7 16.9 185 3150 female 2008 Adelie_Biscoe
Adelie Biscoe 41.3 21.1 195 4400 male 2008 Adelie_Biscoe
Adelie Biscoe 37.6 17.0 185 3600 female 2008 Adelie_Biscoe
Adelie Biscoe 41.1 18.2 192 4050 male 2008 Adelie_Biscoe
Adelie Biscoe 36.4 17.1 184 2850 female 2008 Adelie_Biscoe
Adelie Biscoe 41.6 18.0 192 3950 male 2008 Adelie_Biscoe
Adelie Biscoe 35.5 16.2 195 3350 female 2008 Adelie_Biscoe
Adelie Biscoe 41.1 19.1 188 4100 male 2008 Adelie_Biscoe
Adelie Torgersen 35.9 16.6 190 3050 female 2008 Adelie_Torgersen
Adelie Torgersen 41.8 19.4 198 4450 male 2008 Adelie_Torgersen
Adelie Torgersen 33.5 19.0 190 3600 female 2008 Adelie_Torgersen
Adelie Torgersen 39.7 18.4 190 3900 male 2008 Adelie_Torgersen
Adelie Torgersen 39.6 17.2 196 3550 female 2008 Adelie_Torgersen
Adelie Torgersen 45.8 18.9 197 4150 male 2008 Adelie_Torgersen
Adelie Torgersen 35.5 17.5 190 3700 female 2008 Adelie_Torgersen
Adelie Torgersen 42.8 18.5 195 4250 male 2008 Adelie_Torgersen
Adelie Torgersen 40.9 16.8 191 3700 female 2008 Adelie_Torgersen
Adelie Torgersen 37.2 19.4 184 3900 male 2008 Adelie_Torgersen
Adelie Torgersen 36.2 16.1 187 3550 female 2008 Adelie_Torgersen
Adelie Torgersen 42.1 19.1 195 4000 male 2008 Adelie_Torgersen
Adelie Torgersen 34.6 17.2 189 3200 female 2008 Adelie_Torgersen
Adelie Torgersen 42.9 17.6 196 4700 male 2008 Adelie_Torgersen
Adelie Torgersen 36.7 18.8 187 3800 female 2008 Adelie_Torgersen
Adelie Torgersen 35.1 19.4 193 4200 male 2008 Adelie_Torgersen
Adelie Dream 37.3 17.8 191 3350 female 2008 Adelie_Dream
Adelie Dream 41.3 20.3 194 3550 male 2008 Adelie_Dream
Adelie Dream 36.3 19.5 190 3800 male 2008 Adelie_Dream
Adelie Dream 36.9 18.6 189 3500 female 2008 Adelie_Dream
Adelie Dream 38.3 19.2 189 3950 male 2008 Adelie_Dream
Adelie Dream 38.9 18.8 190 3600 female 2008 Adelie_Dream
Adelie Dream 35.7 18.0 202 3550 female 2008 Adelie_Dream
Adelie Dream 41.1 18.1 205 4300 male 2008 Adelie_Dream
Adelie Dream 34.0 17.1 185 3400 female 2008 Adelie_Dream
Adelie Dream 39.6 18.1 186 4450 male 2008 Adelie_Dream
Adelie Dream 36.2 17.3 187 3300 female 2008 Adelie_Dream
Adelie Dream 40.8 18.9 208 4300 male 2008 Adelie_Dream
Adelie Dream 38.1 18.6 190 3700 female 2008 Adelie_Dream
Adelie Dream 40.3 18.5 196 4350 male 2008 Adelie_Dream
Adelie Dream 33.1 16.1 178 2900 female 2008 Adelie_Dream
Adelie Dream 43.2 18.5 192 4100 male 2008 Adelie_Dream
Adelie Biscoe 35.0 17.9 192 3725 female 2009 Adelie_Biscoe
Adelie Biscoe 41.0 20.0 203 4725 male 2009 Adelie_Biscoe
Adelie Biscoe 37.7 16.0 183 3075 female 2009 Adelie_Biscoe
Adelie Biscoe 37.8 20.0 190 4250 male 2009 Adelie_Biscoe
Adelie Biscoe 37.9 18.6 193 2925 female 2009 Adelie_Biscoe
Adelie Biscoe 39.7 18.9 184 3550 male 2009 Adelie_Biscoe
Adelie Biscoe 38.6 17.2 199 3750 female 2009 Adelie_Biscoe
Adelie Biscoe 38.2 20.0 190 3900 male 2009 Adelie_Biscoe
Adelie Biscoe 38.1 17.0 181 3175 female 2009 Adelie_Biscoe
Adelie Biscoe 43.2 19.0 197 4775 male 2009 Adelie_Biscoe
Adelie Biscoe 38.1 16.5 198 3825 female 2009 Adelie_Biscoe
Adelie Biscoe 45.6 20.3 191 4600 male 2009 Adelie_Biscoe
Adelie Biscoe 39.7 17.7 193 3200 female 2009 Adelie_Biscoe
Adelie Biscoe 42.2 19.5 197 4275 male 2009 Adelie_Biscoe
Adelie Biscoe 39.6 20.7 191 3900 female 2009 Adelie_Biscoe
Adelie Biscoe 42.7 18.3 196 4075 male 2009 Adelie_Biscoe
Adelie Torgersen 38.6 17.0 188 2900 female 2009 Adelie_Torgersen
Adelie Torgersen 37.3 20.5 199 3775 male 2009 Adelie_Torgersen
Adelie Torgersen 35.7 17.0 189 3350 female 2009 Adelie_Torgersen
Adelie Torgersen 41.1 18.6 189 3325 male 2009 Adelie_Torgersen
Adelie Torgersen 36.2 17.2 187 3150 female 2009 Adelie_Torgersen
Adelie Torgersen 37.7 19.8 198 3500 male 2009 Adelie_Torgersen
Adelie Torgersen 40.2 17.0 176 3450 female 2009 Adelie_Torgersen
Adelie Torgersen 41.4 18.5 202 3875 male 2009 Adelie_Torgersen
Adelie Torgersen 35.2 15.9 186 3050 female 2009 Adelie_Torgersen
Adelie Torgersen 40.6 19.0 199 4000 male 2009 Adelie_Torgersen
Adelie Torgersen 38.8 17.6 191 3275 female 2009 Adelie_Torgersen
Adelie Torgersen 41.5 18.3 195 4300 male 2009 Adelie_Torgersen
Adelie Torgersen 39.0 17.1 191 3050 female 2009 Adelie_Torgersen
Adelie Torgersen 44.1 18.0 210 4000 male 2009 Adelie_Torgersen
Adelie Torgersen 38.5 17.9 190 3325 female 2009 Adelie_Torgersen
Adelie Torgersen 43.1 19.2 197 3500 male 2009 Adelie_Torgersen
Adelie Dream 36.8 18.5 193 3500 female 2009 Adelie_Dream
Adelie Dream 37.5 18.5 199 4475 male 2009 Adelie_Dream
Adelie Dream 38.1 17.6 187 3425 female 2009 Adelie_Dream
Adelie Dream 41.1 17.5 190 3900 male 2009 Adelie_Dream
Adelie Dream 35.6 17.5 191 3175 female 2009 Adelie_Dream
Adelie Dream 40.2 20.1 200 3975 male 2009 Adelie_Dream
Adelie Dream 37.0 16.5 185 3400 female 2009 Adelie_Dream
Adelie Dream 39.7 17.9 193 4250 male 2009 Adelie_Dream
Adelie Dream 40.2 17.1 193 3400 female 2009 Adelie_Dream
Adelie Dream 40.6 17.2 187 3475 male 2009 Adelie_Dream
Adelie Dream 32.1 15.5 188 3050 female 2009 Adelie_Dream
Adelie Dream 40.7 17.0 190 3725 male 2009 Adelie_Dream
Adelie Dream 37.3 16.8 192 3000 female 2009 Adelie_Dream
Adelie Dream 39.0 18.7 185 3650 male 2009 Adelie_Dream
Adelie Dream 39.2 18.6 190 4250 male 2009 Adelie_Dream
Adelie Dream 36.6 18.4 184 3475 female 2009 Adelie_Dream
Adelie Dream 36.0 17.8 195 3450 female 2009 Adelie_Dream
Adelie Dream 37.8 18.1 193 3750 male 2009 Adelie_Dream
Adelie Dream 36.0 17.1 187 3700 female 2009 Adelie_Dream
Adelie Dream 41.5 18.5 201 4000 male 2009 Adelie_Dream
Gentoo Biscoe 46.1 13.2 211 4500 female 2007 Gentoo_Biscoe
Gentoo Biscoe 50.0 16.3 230 5700 male 2007 Gentoo_Biscoe
Gentoo Biscoe 48.7 14.1 210 4450 female 2007 Gentoo_Biscoe
Gentoo Biscoe 50.0 15.2 218 5700 male 2007 Gentoo_Biscoe
Gentoo Biscoe 47.6 14.5 215 5400 male 2007 Gentoo_Biscoe
Gentoo Biscoe 46.5 13.5 210 4550 female 2007 Gentoo_Biscoe
Gentoo Biscoe 45.4 14.6 211 4800 female 2007 Gentoo_Biscoe
Gentoo Biscoe 46.7 15.3 219 5200 male 2007 Gentoo_Biscoe
Gentoo Biscoe 43.3 13.4 209 4400 female 2007 Gentoo_Biscoe
Gentoo Biscoe 46.8 15.4 215 5150 male 2007 Gentoo_Biscoe
Gentoo Biscoe 40.9 13.7 214 4650 female 2007 Gentoo_Biscoe
Gentoo Biscoe 49.0 16.1 216 5550 male 2007 Gentoo_Biscoe
Gentoo Biscoe 45.5 13.7 214 4650 female 2007 Gentoo_Biscoe
Gentoo Biscoe 48.4 14.6 213 5850 male 2007 Gentoo_Biscoe
Gentoo Biscoe 45.8 14.6 210 4200 female 2007 Gentoo_Biscoe
Gentoo Biscoe 49.3 15.7 217 5850 male 2007 Gentoo_Biscoe
Gentoo Biscoe 42.0 13.5 210 4150 female 2007 Gentoo_Biscoe
Gentoo Biscoe 49.2 15.2 221 6300 male 2007 Gentoo_Biscoe
Gentoo Biscoe 46.2 14.5 209 4800 female 2007 Gentoo_Biscoe
Gentoo Biscoe 48.7 15.1 222 5350 male 2007 Gentoo_Biscoe
Gentoo Biscoe 50.2 14.3 218 5700 male 2007 Gentoo_Biscoe
Gentoo Biscoe 45.1 14.5 215 5000 female 2007 Gentoo_Biscoe
Gentoo Biscoe 46.5 14.5 213 4400 female 2007 Gentoo_Biscoe
Gentoo Biscoe 46.3 15.8 215 5050 male 2007 Gentoo_Biscoe
Gentoo Biscoe 42.9 13.1 215 5000 female 2007 Gentoo_Biscoe
Gentoo Biscoe 46.1 15.1 215 5100 male 2007 Gentoo_Biscoe
Gentoo Biscoe 44.5 14.3 216 4100 NA 2007 Gentoo_Biscoe
Gentoo Biscoe 47.8 15.0 215 5650 male 2007 Gentoo_Biscoe
Gentoo Biscoe 48.2 14.3 210 4600 female 2007 Gentoo_Biscoe
Gentoo Biscoe 50.0 15.3 220 5550 male 2007 Gentoo_Biscoe
Gentoo Biscoe 47.3 15.3 222 5250 male 2007 Gentoo_Biscoe
Gentoo Biscoe 42.8 14.2 209 4700 female 2007 Gentoo_Biscoe
Gentoo Biscoe 45.1 14.5 207 5050 female 2007 Gentoo_Biscoe
Gentoo Biscoe 59.6 17.0 230 6050 male 2007 Gentoo_Biscoe
Gentoo Biscoe 49.1 14.8 220 5150 female 2008 Gentoo_Biscoe
Gentoo Biscoe 48.4 16.3 220 5400 male 2008 Gentoo_Biscoe
Gentoo Biscoe 42.6 13.7 213 4950 female 2008 Gentoo_Biscoe
Gentoo Biscoe 44.4 17.3 219 5250 male 2008 Gentoo_Biscoe
Gentoo Biscoe 44.0 13.6 208 4350 female 2008 Gentoo_Biscoe
Gentoo Biscoe 48.7 15.7 208 5350 male 2008 Gentoo_Biscoe
Gentoo Biscoe 42.7 13.7 208 3950 female 2008 Gentoo_Biscoe
Gentoo Biscoe 49.6 16.0 225 5700 male 2008 Gentoo_Biscoe
Gentoo Biscoe 45.3 13.7 210 4300 female 2008 Gentoo_Biscoe
Gentoo Biscoe 49.6 15.0 216 4750 male 2008 Gentoo_Biscoe
Gentoo Biscoe 50.5 15.9 222 5550 male 2008 Gentoo_Biscoe
Gentoo Biscoe 43.6 13.9 217 4900 female 2008 Gentoo_Biscoe
Gentoo Biscoe 45.5 13.9 210 4200 female 2008 Gentoo_Biscoe
Gentoo Biscoe 50.5 15.9 225 5400 male 2008 Gentoo_Biscoe
Gentoo Biscoe 44.9 13.3 213 5100 female 2008 Gentoo_Biscoe
Gentoo Biscoe 45.2 15.8 215 5300 male 2008 Gentoo_Biscoe
Gentoo Biscoe 46.6 14.2 210 4850 female 2008 Gentoo_Biscoe
Gentoo Biscoe 48.5 14.1 220 5300 male 2008 Gentoo_Biscoe
Gentoo Biscoe 45.1 14.4 210 4400 female 2008 Gentoo_Biscoe
Gentoo Biscoe 50.1 15.0 225 5000 male 2008 Gentoo_Biscoe
Gentoo Biscoe 46.5 14.4 217 4900 female 2008 Gentoo_Biscoe
Gentoo Biscoe 45.0 15.4 220 5050 male 2008 Gentoo_Biscoe
Gentoo Biscoe 43.8 13.9 208 4300 female 2008 Gentoo_Biscoe
Gentoo Biscoe 45.5 15.0 220 5000 male 2008 Gentoo_Biscoe
Gentoo Biscoe 43.2 14.5 208 4450 female 2008 Gentoo_Biscoe
Gentoo Biscoe 50.4 15.3 224 5550 male 2008 Gentoo_Biscoe
Gentoo Biscoe 45.3 13.8 208 4200 female 2008 Gentoo_Biscoe
Gentoo Biscoe 46.2 14.9 221 5300 male 2008 Gentoo_Biscoe
Gentoo Biscoe 45.7 13.9 214 4400 female 2008 Gentoo_Biscoe
Gentoo Biscoe 54.3 15.7 231 5650 male 2008 Gentoo_Biscoe
Gentoo Biscoe 45.8 14.2 219 4700 female 2008 Gentoo_Biscoe
Gentoo Biscoe 49.8 16.8 230 5700 male 2008 Gentoo_Biscoe
Gentoo Biscoe 46.2 14.4 214 4650 NA 2008 Gentoo_Biscoe
Gentoo Biscoe 49.5 16.2 229 5800 male 2008 Gentoo_Biscoe
Gentoo Biscoe 43.5 14.2 220 4700 female 2008 Gentoo_Biscoe
Gentoo Biscoe 50.7 15.0 223 5550 male 2008 Gentoo_Biscoe
Gentoo Biscoe 47.7 15.0 216 4750 female 2008 Gentoo_Biscoe
Gentoo Biscoe 46.4 15.6 221 5000 male 2008 Gentoo_Biscoe
Gentoo Biscoe 48.2 15.6 221 5100 male 2008 Gentoo_Biscoe
Gentoo Biscoe 46.5 14.8 217 5200 female 2008 Gentoo_Biscoe
Gentoo Biscoe 46.4 15.0 216 4700 female 2008 Gentoo_Biscoe
Gentoo Biscoe 48.6 16.0 230 5800 male 2008 Gentoo_Biscoe
Gentoo Biscoe 47.5 14.2 209 4600 female 2008 Gentoo_Biscoe
Gentoo Biscoe 51.1 16.3 220 6000 male 2008 Gentoo_Biscoe
Gentoo Biscoe 45.2 13.8 215 4750 female 2008 Gentoo_Biscoe
Gentoo Biscoe 45.2 16.4 223 5950 male 2008 Gentoo_Biscoe
Gentoo Biscoe 49.1 14.5 212 4625 female 2009 Gentoo_Biscoe
Gentoo Biscoe 52.5 15.6 221 5450 male 2009 Gentoo_Biscoe
Gentoo Biscoe 47.4 14.6 212 4725 female 2009 Gentoo_Biscoe
Gentoo Biscoe 50.0 15.9 224 5350 male 2009 Gentoo_Biscoe
Gentoo Biscoe 44.9 13.8 212 4750 female 2009 Gentoo_Biscoe
Gentoo Biscoe 50.8 17.3 228 5600 male 2009 Gentoo_Biscoe
Gentoo Biscoe 43.4 14.4 218 4600 female 2009 Gentoo_Biscoe
Gentoo Biscoe 51.3 14.2 218 5300 male 2009 Gentoo_Biscoe
Gentoo Biscoe 47.5 14.0 212 4875 female 2009 Gentoo_Biscoe
Gentoo Biscoe 52.1 17.0 230 5550 male 2009 Gentoo_Biscoe
Gentoo Biscoe 47.5 15.0 218 4950 female 2009 Gentoo_Biscoe
Gentoo Biscoe 52.2 17.1 228 5400 male 2009 Gentoo_Biscoe
Gentoo Biscoe 45.5 14.5 212 4750 female 2009 Gentoo_Biscoe
Gentoo Biscoe 49.5 16.1 224 5650 male 2009 Gentoo_Biscoe
Gentoo Biscoe 44.5 14.7 214 4850 female 2009 Gentoo_Biscoe
Gentoo Biscoe 50.8 15.7 226 5200 male 2009 Gentoo_Biscoe
Gentoo Biscoe 49.4 15.8 216 4925 male 2009 Gentoo_Biscoe
Gentoo Biscoe 46.9 14.6 222 4875 female 2009 Gentoo_Biscoe
Gentoo Biscoe 48.4 14.4 203 4625 female 2009 Gentoo_Biscoe
Gentoo Biscoe 51.1 16.5 225 5250 male 2009 Gentoo_Biscoe
Gentoo Biscoe 48.5 15.0 219 4850 female 2009 Gentoo_Biscoe
Gentoo Biscoe 55.9 17.0 228 5600 male 2009 Gentoo_Biscoe
Gentoo Biscoe 47.2 15.5 215 4975 female 2009 Gentoo_Biscoe
Gentoo Biscoe 49.1 15.0 228 5500 male 2009 Gentoo_Biscoe
Gentoo Biscoe 47.3 13.8 216 4725 NA 2009 Gentoo_Biscoe
Gentoo Biscoe 46.8 16.1 215 5500 male 2009 Gentoo_Biscoe
Gentoo Biscoe 41.7 14.7 210 4700 female 2009 Gentoo_Biscoe
Gentoo Biscoe 53.4 15.8 219 5500 male 2009 Gentoo_Biscoe
Gentoo Biscoe 43.3 14.0 208 4575 female 2009 Gentoo_Biscoe
Gentoo Biscoe 48.1 15.1 209 5500 male 2009 Gentoo_Biscoe
Gentoo Biscoe 50.5 15.2 216 5000 female 2009 Gentoo_Biscoe
Gentoo Biscoe 49.8 15.9 229 5950 male 2009 Gentoo_Biscoe
Gentoo Biscoe 43.5 15.2 213 4650 female 2009 Gentoo_Biscoe
Gentoo Biscoe 51.5 16.3 230 5500 male 2009 Gentoo_Biscoe
Gentoo Biscoe 46.2 14.1 217 4375 female 2009 Gentoo_Biscoe
Gentoo Biscoe 55.1 16.0 230 5850 male 2009 Gentoo_Biscoe
Gentoo Biscoe 44.5 15.7 217 4875 NA 2009 Gentoo_Biscoe
Gentoo Biscoe 48.8 16.2 222 6000 male 2009 Gentoo_Biscoe
Gentoo Biscoe 47.2 13.7 214 4925 female 2009 Gentoo_Biscoe
Gentoo Biscoe NA NA NA NA NA 2009 Gentoo_Biscoe
Gentoo Biscoe 46.8 14.3 215 4850 female 2009 Gentoo_Biscoe
Gentoo Biscoe 50.4 15.7 222 5750 male 2009 Gentoo_Biscoe
Gentoo Biscoe 45.2 14.8 212 5200 female 2009 Gentoo_Biscoe
Gentoo Biscoe 49.9 16.1 213 5400 male 2009 Gentoo_Biscoe
Chinstrap Dream 46.5 17.9 192 3500 female 2007 Chinstrap_Dream
Chinstrap Dream 50.0 19.5 196 3900 male 2007 Chinstrap_Dream
Chinstrap Dream 51.3 19.2 193 3650 male 2007 Chinstrap_Dream
Chinstrap Dream 45.4 18.7 188 3525 female 2007 Chinstrap_Dream
Chinstrap Dream 52.7 19.8 197 3725 male 2007 Chinstrap_Dream
Chinstrap Dream 45.2 17.8 198 3950 female 2007 Chinstrap_Dream
Chinstrap Dream 46.1 18.2 178 3250 female 2007 Chinstrap_Dream
Chinstrap Dream 51.3 18.2 197 3750 male 2007 Chinstrap_Dream
Chinstrap Dream 46.0 18.9 195 4150 female 2007 Chinstrap_Dream
Chinstrap Dream 51.3 19.9 198 3700 male 2007 Chinstrap_Dream
Chinstrap Dream 46.6 17.8 193 3800 female 2007 Chinstrap_Dream
Chinstrap Dream 51.7 20.3 194 3775 male 2007 Chinstrap_Dream
Chinstrap Dream 47.0 17.3 185 3700 female 2007 Chinstrap_Dream
Chinstrap Dream 52.0 18.1 201 4050 male 2007 Chinstrap_Dream
Chinstrap Dream 45.9 17.1 190 3575 female 2007 Chinstrap_Dream
Chinstrap Dream 50.5 19.6 201 4050 male 2007 Chinstrap_Dream
Chinstrap Dream 50.3 20.0 197 3300 male 2007 Chinstrap_Dream
Chinstrap Dream 58.0 17.8 181 3700 female 2007 Chinstrap_Dream
Chinstrap Dream 46.4 18.6 190 3450 female 2007 Chinstrap_Dream
Chinstrap Dream 49.2 18.2 195 4400 male 2007 Chinstrap_Dream
Chinstrap Dream 42.4 17.3 181 3600 female 2007 Chinstrap_Dream
Chinstrap Dream 48.5 17.5 191 3400 male 2007 Chinstrap_Dream
Chinstrap Dream 43.2 16.6 187 2900 female 2007 Chinstrap_Dream
Chinstrap Dream 50.6 19.4 193 3800 male 2007 Chinstrap_Dream
Chinstrap Dream 46.7 17.9 195 3300 female 2007 Chinstrap_Dream
Chinstrap Dream 52.0 19.0 197 4150 male 2007 Chinstrap_Dream
Chinstrap Dream 50.5 18.4 200 3400 female 2008 Chinstrap_Dream
Chinstrap Dream 49.5 19.0 200 3800 male 2008 Chinstrap_Dream
Chinstrap Dream 46.4 17.8 191 3700 female 2008 Chinstrap_Dream
Chinstrap Dream 52.8 20.0 205 4550 male 2008 Chinstrap_Dream
Chinstrap Dream 40.9 16.6 187 3200 female 2008 Chinstrap_Dream
Chinstrap Dream 54.2 20.8 201 4300 male 2008 Chinstrap_Dream
Chinstrap Dream 42.5 16.7 187 3350 female 2008 Chinstrap_Dream
Chinstrap Dream 51.0 18.8 203 4100 male 2008 Chinstrap_Dream
Chinstrap Dream 49.7 18.6 195 3600 male 2008 Chinstrap_Dream
Chinstrap Dream 47.5 16.8 199 3900 female 2008 Chinstrap_Dream
Chinstrap Dream 47.6 18.3 195 3850 female 2008 Chinstrap_Dream
Chinstrap Dream 52.0 20.7 210 4800 male 2008 Chinstrap_Dream
Chinstrap Dream 46.9 16.6 192 2700 female 2008 Chinstrap_Dream
Chinstrap Dream 53.5 19.9 205 4500 male 2008 Chinstrap_Dream
Chinstrap Dream 49.0 19.5 210 3950 male 2008 Chinstrap_Dream
Chinstrap Dream 46.2 17.5 187 3650 female 2008 Chinstrap_Dream
Chinstrap Dream 50.9 19.1 196 3550 male 2008 Chinstrap_Dream
Chinstrap Dream 45.5 17.0 196 3500 female 2008 Chinstrap_Dream
Chinstrap Dream 50.9 17.9 196 3675 female 2009 Chinstrap_Dream
Chinstrap Dream 50.8 18.5 201 4450 male 2009 Chinstrap_Dream
Chinstrap Dream 50.1 17.9 190 3400 female 2009 Chinstrap_Dream
Chinstrap Dream 49.0 19.6 212 4300 male 2009 Chinstrap_Dream
Chinstrap Dream 51.5 18.7 187 3250 male 2009 Chinstrap_Dream
Chinstrap Dream 49.8 17.3 198 3675 female 2009 Chinstrap_Dream
Chinstrap Dream 48.1 16.4 199 3325 female 2009 Chinstrap_Dream
Chinstrap Dream 51.4 19.0 201 3950 male 2009 Chinstrap_Dream
Chinstrap Dream 45.7 17.3 193 3600 female 2009 Chinstrap_Dream
Chinstrap Dream 50.7 19.7 203 4050 male 2009 Chinstrap_Dream
Chinstrap Dream 42.5 17.3 187 3350 female 2009 Chinstrap_Dream
Chinstrap Dream 52.2 18.8 197 3450 male 2009 Chinstrap_Dream
Chinstrap Dream 45.2 16.6 191 3250 female 2009 Chinstrap_Dream
Chinstrap Dream 49.3 19.9 203 4050 male 2009 Chinstrap_Dream
Chinstrap Dream 50.2 18.8 202 3800 male 2009 Chinstrap_Dream
Chinstrap Dream 45.6 19.4 194 3525 female 2009 Chinstrap_Dream
Chinstrap Dream 51.9 19.5 206 3950 male 2009 Chinstrap_Dream
Chinstrap Dream 46.8 16.5 189 3650 female 2009 Chinstrap_Dream
Chinstrap Dream 45.7 17.0 195 3650 female 2009 Chinstrap_Dream
Chinstrap Dream 55.8 19.8 207 4000 male 2009 Chinstrap_Dream
Chinstrap Dream 43.5 18.1 202 3400 female 2009 Chinstrap_Dream
Chinstrap Dream 49.6 18.2 193 3775 male 2009 Chinstrap_Dream
Chinstrap Dream 50.8 19.0 210 4100 male 2009 Chinstrap_Dream
Chinstrap Dream 50.2 18.7 198 3775 female 2009 Chinstrap_Dream

3. PLOTTING

**Show the distribution of flipper_length_mm by species and island using boxplots. For one point of extra credit, redo creating the species_island column with the sep as instead of _. What does do? You will find it very handy in the future.**

penguins %>%    # pipe penguin data
  group_by(species, island) %>%
  mutate('species-island' = paste(as.character(species), as.character(island), sep = "\n")) %>%  # create a new dataframe with 'species' island column using mutate() and paste(sep = "\n")
  summarise(flipper_length_mm, 'species-island') %>%



  boxplot(flipper_length_mm ~ species-island, data = .) # make a boxplot 
## `summarise()` regrouping output by 'species', 'island' (override with `.groups` argument)

The “” function separates pasted character with a new line.

3b. Show the relationship between average flipper length and average body mass by species and island. What do you see?

penguins %>%       # pipe penguin data 
  mutate(`species-island` = paste("species", "island", sep = "\n")) %>%                  # add a new species-island column using mutate() and paste(sep = "\n")
  summarise(avg_flipper_length_mm = mean(flipper_length_mm, na.rm = TRUE), avg_body_mass_g = mean(body_mass_g, na.rm = TRUE)) %>% # create a data frame that summarizes rouped elements
  
  plot(avg_flipper_length_mm ~ avg_body_mass_g, data = .) # plot average flipper length against average body mass

From the plot, it looks like there is a correlation between average flipper length and average body mass. That is, there is a linear relationship between average flipper length and average body mass.

3c. Interesting. What if you had made the same plot with the whole dataset? What do you see? Is there anything that could clarify the result any more? Think about it - lots of possible right answers here.

penguins %>%        # pipe penguin data
  group_by(flipper_length_mm, body_mass_g) %>%
 summarize(avg_flipper_length_mm = mean(flipper_length_mm, na.rm = TRUE), avg_body_mass_g = mean(body_mass_g, na.rm = TRUE)) %>% # create a new column of average flipper length and average body mass in the penguins data set using mutate()
  
  plot(avg_flipper_length_mm ~ avg_body_mass_g, data = .) # plot average flipper length against average body mass using the whole dataset
## `summarise()` regrouping output by 'flipper_length_mm' (override with `.groups` argument)

My thoughts about this plot;

  1. The average body mass across the dataset falls between 4000g to 4500g.

  2. The average flipper length is about 200mm

  3. Values above the ranges mentioned above are probably outliers

  4. This plot correlates with the previous plot of average flipper length against body mass across species and island.

4. Simulation

4a.Grab the values for bill_length_mm for Gentoo penguins in Biscoe Island and put it into an object. Note, the dplyr function pull() is kinda cool, as if you apply it to a data frame, it will pull out a vector from a column of interest.

bill_gent_bis <- penguins %>% 
  group_by(species, island) %>% # create a new dataframe with values for bill length for Gentoo species in Biscoe island
  filter(species == "Gentoo" | island == "Biscoe") %>%  # Filter Gentoo species and Biscoe island
  pull(bill_length_mm) # Use pull() to select bill length

bill_gent_bis
##   [1] 37.8 37.7 35.9 38.2 38.8 35.3 40.6 40.5 37.9 40.5 39.6 40.1 35.0 42.0 34.5
##  [16] 41.4 39.0 40.6 36.5 37.6 35.7 41.3 37.6 41.1 36.4 41.6 35.5 41.1 35.0 41.0
##  [31] 37.7 37.8 37.9 39.7 38.6 38.2 38.1 43.2 38.1 45.6 39.7 42.2 39.6 42.7 46.1
##  [46] 50.0 48.7 50.0 47.6 46.5 45.4 46.7 43.3 46.8 40.9 49.0 45.5 48.4 45.8 49.3
##  [61] 42.0 49.2 46.2 48.7 50.2 45.1 46.5 46.3 42.9 46.1 44.5 47.8 48.2 50.0 47.3
##  [76] 42.8 45.1 59.6 49.1 48.4 42.6 44.4 44.0 48.7 42.7 49.6 45.3 49.6 50.5 43.6
##  [91] 45.5 50.5 44.9 45.2 46.6 48.5 45.1 50.1 46.5 45.0 43.8 45.5 43.2 50.4 45.3
## [106] 46.2 45.7 54.3 45.8 49.8 46.2 49.5 43.5 50.7 47.7 46.4 48.2 46.5 46.4 48.6
## [121] 47.5 51.1 45.2 45.2 49.1 52.5 47.4 50.0 44.9 50.8 43.4 51.3 47.5 52.1 47.5
## [136] 52.2 45.5 49.5 44.5 50.8 49.4 46.9 48.4 51.1 48.5 55.9 47.2 49.1 47.3 46.8
## [151] 41.7 53.4 43.3 48.1 50.5 49.8 43.5 51.5 46.2 55.1 44.5 48.8 47.2   NA 46.8
## [166] 50.4 45.2 49.9

4b.Use replicate() to calculate the standard error of the mean 10 times. Use a formula! Don’t forget that NA values shouldn’t be included!

as.data.frame(bill_gent_bis) %>%  # covert bill_gent_bis containing bill length of Gentoo species in Biscoe island 
  summarise(sd = sd(bill_gent_bis, na.rm = TRUE), n = n(),  se = replicate(n =10, sd/sqrt(n))) %>%  # calculate the standard deviations, replicate standard error 10x and summarize
  na.omit() %>%  # omit missing values
kbl() %>%                        
  kable_styling()                  # Using KableExtra table formatting
sd n se
4.772731 168 0.3682242
4.772731 168 0.3682242
4.772731 168 0.3682242
4.772731 168 0.3682242
4.772731 168 0.3682242
4.772731 168 0.3682242
4.772731 168 0.3682242
4.772731 168 0.3682242
4.772731 168 0.3682242
4.772731 168 0.3682242

4c.Use map_df() to create a data frame with the mean and sd of different sample sizes using the first 5 through 100 values (so, n = 5:100 - smallest sample size will have the values 1-5). Make sure the sample size is included in the final data frame.

library(purrr)

my_vec <- 1:100      # create a vector containing number 1 through 100

my_vec_data <- map_df(5:100, ~data.frame(m = mean(my_vec[1:.x]),
                         sd = sd(my_vec[1:.x])),
       .id = "sample_size")    # use map_df to create a data frame from the created vector. 
  

my_vec_data      # view new data frame
##    sample_size    m        sd
## 1            1  3.0  1.581139
## 2            2  3.5  1.870829
## 3            3  4.0  2.160247
## 4            4  4.5  2.449490
## 5            5  5.0  2.738613
## 6            6  5.5  3.027650
## 7            7  6.0  3.316625
## 8            8  6.5  3.605551
## 9            9  7.0  3.894440
## 10          10  7.5  4.183300
## 11          11  8.0  4.472136
## 12          12  8.5  4.760952
## 13          13  9.0  5.049752
## 14          14  9.5  5.338539
## 15          15 10.0  5.627314
## 16          16 10.5  5.916080
## 17          17 11.0  6.204837
## 18          18 11.5  6.493587
## 19          19 12.0  6.782330
## 20          20 12.5  7.071068
## 21          21 13.0  7.359801
## 22          22 13.5  7.648529
## 23          23 14.0  7.937254
## 24          24 14.5  8.225975
## 25          25 15.0  8.514693
## 26          26 15.5  8.803408
## 27          27 16.0  9.092121
## 28          28 16.5  9.380832
## 29          29 17.0  9.669540
## 30          30 17.5  9.958246
## 31          31 18.0 10.246951
## 32          32 18.5 10.535654
## 33          33 19.0 10.824355
## 34          34 19.5 11.113055
## 35          35 20.0 11.401754
## 36          36 20.5 11.690452
## 37          37 21.0 11.979149
## 38          38 21.5 12.267844
## 39          39 22.0 12.556539
## 40          40 22.5 12.845233
## 41          41 23.0 13.133926
## 42          42 23.5 13.422618
## 43          43 24.0 13.711309
## 44          44 24.5 14.000000
## 45          45 25.0 14.288690
## 46          46 25.5 14.577380
## 47          47 26.0 14.866069
## 48          48 26.5 15.154757
## 49          49 27.0 15.443445
## 50          50 27.5 15.732133
## 51          51 28.0 16.020820
## 52          52 28.5 16.309506
## 53          53 29.0 16.598193
## 54          54 29.5 16.886879
## 55          55 30.0 17.175564
## 56          56 30.5 17.464249
## 57          57 31.0 17.752934
## 58          58 31.5 18.041619
## 59          59 32.0 18.330303
## 60          60 32.5 18.618987
## 61          61 33.0 18.907670
## 62          62 33.5 19.196354
## 63          63 34.0 19.485037
## 64          64 34.5 19.773720
## 65          65 35.0 20.062403
## 66          66 35.5 20.351085
## 67          67 36.0 20.639767
## 68          68 36.5 20.928450
## 69          69 37.0 21.217131
## 70          70 37.5 21.505813
## 71          71 38.0 21.794495
## 72          72 38.5 22.083176
## 73          73 39.0 22.371857
## 74          74 39.5 22.660538
## 75          75 40.0 22.949219
## 76          76 40.5 23.237900
## 77          77 41.0 23.526581
## 78          78 41.5 23.815261
## 79          79 42.0 24.103942
## 80          80 42.5 24.392622
## 81          81 43.0 24.681302
## 82          82 43.5 24.969982
## 83          83 44.0 25.258662
## 84          84 44.5 25.547342
## 85          85 45.0 25.836021
## 86          86 45.5 26.124701
## 87          87 46.0 26.413380
## 88          88 46.5 26.702060
## 89          89 47.0 26.990739
## 90          90 47.5 27.279418
## 91          91 48.0 27.568098
## 92          92 48.5 27.856777
## 93          93 49.0 28.145456
## 94          94 49.5 28.434134
## 95          95 50.0 28.722813
## 96          96 50.5 29.011492

4d. Plot the relationship between sample size and SD and sample size versus SE of the mean. What difference do you see and why? Note, you’ll need to create a column for SE here!

+2 EC for using par() to make a two-panel plot. Don’t forget to reset back to a single plot per panel after making a two-panel plot. Otherwise things get weird.

my_vec_data_02 <- my_vec_data %>%         # pipe data frame containing mean and SD of samples
  group_by(sample_size, sd) %>%    # group sample size and SD
  summarise(sample_size, sd, n = n(), se = sd/sqrt(n))    # calculate standard error and summarize sample_size, standard deviation and Standard error into one data frame
## `summarise()` regrouping output by 'sample_size' (override with `.groups` argument)
my_vec_data_02     # summarized data showing se column
## # A tibble: 96 x 4
## # Groups:   sample_size [96]
##    sample_size    sd     n    se
##    <chr>       <dbl> <int> <dbl>
##  1 1            1.58     1  1.58
##  2 10           4.18     1  4.18
##  3 11           4.47     1  4.47
##  4 12           4.76     1  4.76
##  5 13           5.05     1  5.05
##  6 14           5.34     1  5.34
##  7 15           5.63     1  5.63
##  8 16           5.92     1  5.92
##  9 17           6.20     1  6.20
## 10 18           6.49     1  6.49
## # ... with 86 more rows
  plot(sd ~ se, data = my_vec_data_02)     # plot standard deviation against standard error

There’s no difference between the SD and SE. That is; SD = SE

+2 using par() for multiple plots

I plotted the standard Errors using plot() and hist() side by side

par(mfrow = c(2, 2))     # setting the two panel plot
plot(my_vec_data_02$se)  # plot se from my_vec_data_02
hist(my_vec_data_02$se)  # make a histogram to show the distrubution of se from my_vec data
par(mfrow = c(1, 1))     # undo two-panel plot

GitHub Extra Credit