Markdown Author: Jessie Bell, 2023

Libraries Used: dplyr

Introduction to R Part 2

Markdown Code.

1. Peeing in Pools

A. Vector

poolpee <- c(640, 1070, 780, 70, 160, 130, 60, 50, 2110, 70, 350, 30, 210, 90, 470, 580, 250, 310, 460, 430, 140, 1070, 130)

B. Mean

mean(poolpee) #ng/L
## [1] 420

C. Iterate a List

poolpee_conversion <- poolpee/4000 # urine/L
print(poolpee_conversion)
##  [1] 0.1600 0.2675 0.1950 0.0175 0.0400 0.0325 0.0150 0.0125 0.5275 0.0175
## [11] 0.0875 0.0075 0.0525 0.0225 0.1175 0.1450 0.0625 0.0775 0.1150 0.1075
## [21] 0.0350 0.2675 0.0325

D. Converted Mean

mean(poolpee_conversion) # the value is much lower after converting
## [1] 0.105

E. Double Check

sum(poolpee_conversion)/length(poolpee_conversion)
## [1] 0.105
mean(poolpee_conversion) 
## [1] 0.105
# they are exactly the same!

F. Dimensionally Analyze

# our dimensions for poolpee_conversion are in urine/L, so to determine how much urine is in 500,000 liter pool on average we can just multiply the mean by 500,000 liters to cancel out liters. 

mean(poolpee_conversion)*500000 # Urine is our new unit
## [1] 52500

2. Weddell Seals

A. Vector

# metabolic costs for dives in ml of O2/kg
feedingDives <- c(71.0, 77.3, 82.6, 96.1, 106.6, 112.8, 121.2, 126.4, 127.5, 143.1)
nonfeedingDives <- c(42.2, 51.7, 59.8, 66.5, 81.9, 82.0, 81.3, 81.3, 96.0, 104.1)

B. Length

length(feedingDives)
## [1] 10
length(nonfeedingDives)
## [1] 10
# both lists have 10 seals

C. Metabolism Difference

# assuming the lists are organized:
MetabolismDifference <- feedingDives-nonfeedingDives

print(MetabolismDifference)
##  [1] 28.8 25.6 22.8 29.6 24.7 30.8 39.9 45.1 31.5 39.0

D. Average Difference

avg_feeding <- mean(feedingDives)
avg_nonfeeding <- mean(nonfeedingDives)

avg_feeding - avg_nonfeeding
## [1] 31.78

E. Ratio

oxygenconsumption_ratio <- feedingDives/nonfeedingDives

oxygenconsumption_ratio
##  [1] 1.682464 1.495164 1.381271 1.445113 1.301587 1.375610 1.490775 1.554736
##  [9] 1.328125 1.374640

F. Log of the Ratio

logratio <- log(oxygenconsumption_ratio)
logratio
##  [1] 0.5202597 0.4022362 0.3230040 0.3681874 0.2635845 0.3188971 0.3992961
##  [8] 0.4413055 0.2837682 0.3181917
mean(logratio)
## [1] 0.363873

3. Countries Data

A. Read in the Data

countriesData <- read.csv("countries.csv")

B. Look at Data

summary(countriesData)
##    country          total_population_in_thousands_2015
##  Length:196         Min.   :      1.6                 
##  Class :character   1st Qu.:   1875.8                 
##  Mode  :character   Median :   8069.6                 
##                     Mean   :  37721.9                 
##                     3rd Qu.:  26413.0                 
##                     Max.   :1400000.0                 
##                     NA's   :2                         
##  gross_national_income_per_capita_2013 life_expectancy_at_birth_female
##  Min.   :   600                        Min.   :48.80                  
##  1st Qu.:  3070                        1st Qu.:67.05                  
##  Median :  9800                        Median :75.90                  
##  Mean   : 14792                        Mean   :73.42                  
##  3rd Qu.: 20370                        3rd Qu.:79.25                  
##  Max.   :123860                        Max.   :86.70                  
##  NA's   :27                            NA's   :13                     
##  life_expectancy_at_birth_male life_expectancy_at_age_60_female
##  Min.   :47.40                 Min.   :12.70                   
##  1st Qu.:62.90                 1st Qu.:18.00                   
##  Median :69.80                 Median :20.40                   
##  Mean   :68.53                 Mean   :20.81                   
##  3rd Qu.:73.95                 3rd Qu.:23.40                   
##  Max.   :81.10                 Max.   :28.60                   
##  NA's   :13                    NA's   :13                      
##  life_expectancy_at_age_60_male physicians_density_per_1000
##  Min.   :12.50                  Min.   :0.029              
##  1st Qu.:15.80                  1st Qu.:1.681              
##  Median :17.50                  Median :2.765              
##  Mean   :18.07                  Mean   :2.725              
##  3rd Qu.:20.20                  3rd Qu.:3.510              
##  Max.   :23.90                  Max.   :7.519              
##  NA's   :13                     NA's   :125                
##  number_neonatal_deaths_in_thousands_2014 measles_immunization_oneyearolds
##  Min.   :  0.00                           Min.   :22.00                   
##  1st Qu.:  0.00                           1st Qu.:83.25                   
##  Median :  1.00                           Median :93.00                   
##  Mean   : 14.11                           Mean   :87.28                   
##  3rd Qu.:  9.50                           3rd Qu.:97.00                   
##  Max.   :722.00                           Max.   :99.00                   
##  NA's   :2                                NA's   :2                       
##  dpt2_vaccination_oneyearolds fines_for_tobacco_advertising_2014
##  Min.   :20.00                Length:196                        
##  1st Qu.:84.25                Class :character                  
##  Median :94.00                Mode  :character                  
##  Mean   :87.91                                                  
##  3rd Qu.:97.00                                                  
##  Max.   :99.00                                                  
##  NA's   :2                                                      
##  mortality_rate_cancer_2012 cigarette_price_2014  continent        
##  Min.   : 54.00             Min.   : 0.360       Length:196        
##  1st Qu.: 88.62             1st Qu.: 1.320       Class :character  
##  Median :108.00             Median : 2.620       Mode  :character  
##  Mean   :109.64             Mean   : 3.798                         
##  3rd Qu.:124.53             3rd Qu.: 4.965                         
##  Max.   :223.00             Max.   :16.140                         
##  NA's   :24                 NA's   :89                             
##  ecological_footprint_2000 ecological_footprint_2012
##  Min.   : 0.600            Min.   :0.700            
##  1st Qu.: 1.097            1st Qu.:1.400            
##  Median : 2.140            Median :2.000            
##  Mean   : 3.147            Mean   :2.353            
##  3rd Qu.: 4.872            3rd Qu.:3.000            
##  Max.   :15.990            Max.   :5.300            
##  NA's   :58                NA's   :147              
##  cell_phone_subscriptions_per_100_people_2012
##  Min.   :  5.47                              
##  1st Qu.: 69.83                              
##  Median :103.25                              
##  Mean   : 99.90                              
##  3rd Qu.:126.10                              
##  Max.   :198.62                              
##  NA's   :10
# country
# total_population_in_thousands_2015
# gross_national_income_per_capita_2013

C. African Countries

AfricanCountries <- subset(countriesData, continent == "Africa")
length(AfricanCountries)
## [1] 18
# there are 18 countries located in Africa

D. Variable Type

summary(countriesData)
##    country          total_population_in_thousands_2015
##  Length:196         Min.   :      1.6                 
##  Class :character   1st Qu.:   1875.8                 
##  Mode  :character   Median :   8069.6                 
##                     Mean   :  37721.9                 
##                     3rd Qu.:  26413.0                 
##                     Max.   :1400000.0                 
##                     NA's   :2                         
##  gross_national_income_per_capita_2013 life_expectancy_at_birth_female
##  Min.   :   600                        Min.   :48.80                  
##  1st Qu.:  3070                        1st Qu.:67.05                  
##  Median :  9800                        Median :75.90                  
##  Mean   : 14792                        Mean   :73.42                  
##  3rd Qu.: 20370                        3rd Qu.:79.25                  
##  Max.   :123860                        Max.   :86.70                  
##  NA's   :27                            NA's   :13                     
##  life_expectancy_at_birth_male life_expectancy_at_age_60_female
##  Min.   :47.40                 Min.   :12.70                   
##  1st Qu.:62.90                 1st Qu.:18.00                   
##  Median :69.80                 Median :20.40                   
##  Mean   :68.53                 Mean   :20.81                   
##  3rd Qu.:73.95                 3rd Qu.:23.40                   
##  Max.   :81.10                 Max.   :28.60                   
##  NA's   :13                    NA's   :13                      
##  life_expectancy_at_age_60_male physicians_density_per_1000
##  Min.   :12.50                  Min.   :0.029              
##  1st Qu.:15.80                  1st Qu.:1.681              
##  Median :17.50                  Median :2.765              
##  Mean   :18.07                  Mean   :2.725              
##  3rd Qu.:20.20                  3rd Qu.:3.510              
##  Max.   :23.90                  Max.   :7.519              
##  NA's   :13                     NA's   :125                
##  number_neonatal_deaths_in_thousands_2014 measles_immunization_oneyearolds
##  Min.   :  0.00                           Min.   :22.00                   
##  1st Qu.:  0.00                           1st Qu.:83.25                   
##  Median :  1.00                           Median :93.00                   
##  Mean   : 14.11                           Mean   :87.28                   
##  3rd Qu.:  9.50                           3rd Qu.:97.00                   
##  Max.   :722.00                           Max.   :99.00                   
##  NA's   :2                                NA's   :2                       
##  dpt2_vaccination_oneyearolds fines_for_tobacco_advertising_2014
##  Min.   :20.00                Length:196                        
##  1st Qu.:84.25                Class :character                  
##  Median :94.00                Mode  :character                  
##  Mean   :87.91                                                  
##  3rd Qu.:97.00                                                  
##  Max.   :99.00                                                  
##  NA's   :2                                                      
##  mortality_rate_cancer_2012 cigarette_price_2014  continent        
##  Min.   : 54.00             Min.   : 0.360       Length:196        
##  1st Qu.: 88.62             1st Qu.: 1.320       Class :character  
##  Median :108.00             Median : 2.620       Mode  :character  
##  Mean   :109.64             Mean   : 3.798                         
##  3rd Qu.:124.53             3rd Qu.: 4.965                         
##  Max.   :223.00             Max.   :16.140                         
##  NA's   :24                 NA's   :89                             
##  ecological_footprint_2000 ecological_footprint_2012
##  Min.   : 0.600            Min.   :0.700            
##  1st Qu.: 1.097            1st Qu.:1.400            
##  Median : 2.140            Median :2.000            
##  Mean   : 3.147            Mean   :2.353            
##  3rd Qu.: 4.872            3rd Qu.:3.000            
##  Max.   :15.990            Max.   :5.300            
##  NA's   :58                NA's   :147              
##  cell_phone_subscriptions_per_100_people_2012
##  Min.   :  5.47                              
##  1st Qu.: 69.83                              
##  Median :103.25                              
##  Mean   : 99.90                              
##  3rd Qu.:126.10                              
##  Max.   :198.62                              
##  NA's   :10
#continents: categorical
#cell_phone_subscriptions_per_100_people_2012: numerical
#total_population_in_thousands_2015: numerical
#fines_for_tobacco_advertising_2014: categorical

# notice that some of the columns print right next to one another so it can be difficult to tell where one column ends and another begins. 

E. Add New Column

countriesData$mean_difference = (countriesData$ecological_footprint_2012-countriesData$ecological_footprint_2000)
mean(countriesData$mean_difference)
## [1] NA

4. New Data Frame

# I subset AfricanCountries already in problem 3 C. 

sum(AfricanCountries$total_population_in_thousands_2015) #total population
## [1] 1184501

5. Extension Activity, add tabs to your markdown

Add interactive tabs like the ones above to your markdown by adding {.tabset} to one of your headers like this three hashtag header:

Now all of your headings with four hashtags should be interactive. Learn more about how to do this here.

This lab was completed using the following textbook: The Analysis of Biological Data by Whitlock and Schluter