Clean and Format Table 1: Maternal Mortality Ratio

Import data

library(readr) # code wants to use the package 'readr'
dirty_maternal_mortality <- read_csv("Maternal mortality ratio.csv") # code uses the package 'readr' to read the csv file imported into R and name it dirty
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 195 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): name, slug, ranking
## dbl (3): deaths/100, 000 live births, date_of_information
## lgl (1): region
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(dirty_maternal_mortality) # code views the data set

Checking the values

str(dirty_maternal_mortality) # code requests the values in the data as well as confirming the column names
## spc_tbl_ [195 × 7] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ name               : chr [1:195] "Nigeria" "Chad" "South Sudan" "Central African Republic" ...
##  $ slug               : chr [1:195] "nigeria" "chad" "south-sudan" "central-african-republic" ...
##  $ deaths/100         : num [1:195] 993 748 692 692 628 563 521 518 505 494 ...
##  $ 000 live births    : num [1:195] 2023 2023 2023 2023 2023 ...
##  $ date_of_information: num [1:195] 1 2 3 4 5 6 7 8 9 10 ...
##  $ ranking            : chr [1:195] "Africa" "Africa" "Africa" "Africa" ...
##  $ region             : logi [1:195] NA NA NA NA NA NA ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   name = col_character(),
##   ..   slug = col_character(),
##   ..   `deaths/100` = col_double(),
##   ..   `000 live births` = col_double(),
##   ..   date_of_information = col_double(),
##   ..   ranking = col_character(),
##   ..   region = col_logical()
##   .. )
##  - attr(*, "problems")=<externalptr>

Keeping certain columns and renaming the columns

names(dirty_maternal_mortality) # code checks the column names of dirty
## [1] "name"                "slug"                "deaths/100"         
## [4] "000 live births"     "date_of_information" "ranking"            
## [7] "region"
dirty_maternal_mortality <- dirty_maternal_mortality[,c(1,3,6)] # code keeps the 3 columns needs even though their column names may not be correct yet: we want to keep the country, the variable (deaths) and the region.
as.list(dirty_maternal_mortality[1,]) # code helps to notice that when the data was imported, the third column name was shifted between the third and fourth columns and created a shift in data. 
## $name
## [1] "Nigeria"
## 
## $`deaths/100`
## [1] 993
## 
## $ranking
## [1] "Africa"
names(dirty_maternal_mortality)[1:3]=c("Country","Mortality Rate","Region") # code renames the columns appropriately
str(dirty_maternal_mortality) # code checks the data types again to make sure we have what we want
## tibble [195 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country       : chr [1:195] "Nigeria" "Chad" "South Sudan" "Central African Republic" ...
##  $ Mortality Rate: num [1:195] 993 748 692 692 628 563 521 518 505 494 ...
##  $ Region        : chr [1:195] "Africa" "Africa" "Africa" "Africa" ...

Checking and Cleaning characters within the columns

dirty_maternal_mortality$Country[grepl("[^a-zA-Z]",dirty_maternal_mortality$Country)] # Code requests any cells in the Country column that aren't only upper and lower case letters. Helps to notice that we have spaces, commas, apostrophes, dashes, and parenthases. However, countries with spaces are fine so let's try something else. 
##  [1] "South Sudan"                       "Central African Republic"         
##  [3] "Guinea-Bissau"                     "Congo, Democratic Republic of the"
##  [5] "Cote d'Ivoire"                     "Gambia, The"                      
##  [7] "Sierra Leone"                      "Burkina Faso"                     
##  [9] "Congo, Republic of the"            "Timor-Leste"                      
## [11] "Papua New Guinea"                  "Equatorial Guinea"                
## [13] "Marshall Islands"                  "Micronesia, Federated States of"  
## [15] "Dominican Republic"                "Solomon Islands"                  
## [17] "South Africa"                      "Bahamas, The"                     
## [19] "Sao Tome and Principe"             "Saint Kitts and Nevis"            
## [21] "Korea, North"                      "Saint Vincent and the Grenadines" 
## [23] "Trinidad and Tobago"               "Saint Lucia"                      
## [25] "Cabo Verde"                        "El Salvador"                      
## [27] "Antigua and Barbuda"               "Costa Rica"                       
## [29] "Sri Lanka"                         "United States"                    
## [31] "West Bank"                         "Gaza Strip"                       
## [33] "Turkey (Turkiye)"                  "Puerto Rico"                      
## [35] "United Kingdom"                    "San Marino"                       
## [37] "New Zealand"                       "Saudi Arabia"                     
## [39] "Bosnia and Herzegovina"            "Korea, South"                     
## [41] "North Macedonia"                   "United Arab Emirates"
dirty_maternal_mortality$Country[grepl("[^a-zA-Z\\s]", dirty_maternal_mortality$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces
##  [1] "Guinea-Bissau"                     "Congo, Democratic Republic of the"
##  [3] "Cote d'Ivoire"                     "Gambia, The"                      
##  [5] "Congo, Republic of the"            "Timor-Leste"                      
##  [7] "Micronesia, Federated States of"   "Bahamas, The"                     
##  [9] "Korea, North"                      "Turkey (Turkiye)"                 
## [11] "Korea, South"
gsub('-',' ',dirty_maternal_mortality$Country,fixed = T) # code requests all dashes be replaced with spaces
##   [1] "Nigeria"                           "Chad"                             
##   [3] "South Sudan"                       "Central African Republic"         
##   [5] "Liberia"                           "Somalia"                          
##   [7] "Afghanistan"                       "Benin"                            
##   [9] "Guinea Bissau"                     "Guinea"                           
##  [11] "Lesotho"                           "Madagascar"                       
##  [13] "Congo, Democratic Republic of the" "Burundi"                          
##  [15] "Mauritania"                        "Kenya"                            
##  [17] "Mali"                              "Cote d'Ivoire"                    
##  [19] "Zimbabwe"                          "Gambia, The"                      
##  [21] "Sierra Leone"                      "Niger"                            
##  [23] "Togo"                              "Haiti"                            
##  [25] "Eritrea"                           "Tanzania"                         
##  [27] "Nauru"                             "Cameroon"                         
##  [29] "Sudan"                             "Burkina Faso"                     
##  [31] "Congo, Republic of the"            "Senegal"                          
##  [33] "Ghana"                             "Gabon"                            
##  [35] "Rwanda"                            "Venezuela"                        
##  [37] "Malawi"                            "Ethiopia"                         
##  [39] "Timor Leste"                       "Papua New Guinea"                 
##  [41] "Burma"                             "Angola"                           
##  [43] "Comoros"                           "Equatorial Guinea"                
##  [45] "Uganda"                            "Tuvalu"                           
##  [47] "Djibouti"                          "Pakistan"                         
##  [49] "Marshall Islands"                  "Botswana"                         
##  [51] "Bolivia"                           "Nepal"                            
##  [53] "Indonesia"                         "Namibia"                          
##  [55] "Cambodia"                          "Jamaica"                          
##  [57] "Micronesia, Federated States of"   "Dominican Republic"               
##  [59] "Solomon Islands"                   "South Africa"                     
##  [61] "Eswatini"                          "Yemen"                            
##  [63] "Bangladesh"                        "Laos"                             
##  [65] "Samoa"                             "Vanuatu"                          
##  [67] "Guatemala"                         "Palau"                            
##  [69] "Zambia"                            "Philippines"                      
##  [71] "Suriname"                          "Mozambique"                       
##  [73] "Kiribati"                          "India"                            
##  [75] "Bahamas, The"                      "Guyana"                           
##  [77] "Sao Tome and Principe"             "Saint Kitts and Nevis"            
##  [79] "Morocco"                           "Belize"                           
##  [81] "Korea, North"                      "Tonga"                            
##  [83] "Brazil"                            "Iraq"                             
##  [85] "Mauritius"                         "Algeria"                          
##  [87] "Nicaragua"                         "Colombia"                         
##  [89] "Libya"                             "Paraguay"                         
##  [91] "Saint Vincent and the Grenadines"  "Ecuador"                          
##  [93] "Trinidad and Tobago"               "Peru"                             
##  [95] "Grenada"                           "Vietnam"                          
##  [97] "Bhutan"                            "Honduras"                         
##  [99] "Saint Lucia"                       "Kyrgyzstan"                       
## [101] "Mexico"                            "Seychelles"                       
## [103] "Mongolia"                          "Cabo Verde"                       
## [105] "El Salvador"                       "Panama"                           
## [107] "Dominica"                          "Tunisia"                          
## [109] "Brunei"                            "Antigua and Barbuda"              
## [111] "Cuba"                              "Barbados"                         
## [113] "Thailand"                          "Argentina"                        
## [115] "Maldives"                          "Jordan"                           
## [117] "Fiji"                              "Uzbekistan"                       
## [119] "Malaysia"                          "Costa Rica"                       
## [121] "Georgia"                           "Syria"                            
## [123] "Latvia"                            "Moldova"                          
## [125] "Armenia"                           "Sri Lanka"                        
## [127] "Azerbaijan"                        "Egypt"                            
## [129] "Bahrain"                           "United States"                    
## [131] "China"                             "West Bank"                        
## [133] "Gaza Strip"                        "Iran"                             
## [135] "Turkey (Turkiye)"                  "Uruguay"                          
## [137] "Portugal"                          "Ukraine"                          
## [139] "Lebanon"                           "Cyprus"                           
## [141] "Tajikistan"                        "Oman"                             
## [143] "Canada"                            "Romania"                          
## [145] "Luxembourg"                        "Hungary"                          
## [147] "Andorra"                           "Serbia"                           
## [149] "Puerto Rico"                       "Chile"                            
## [151] "Kazakhstan"                        "Russia"                           
## [153] "Lithuania"                         "Kuwait"                           
## [155] "United Kingdom"                    "Finland"                          
## [157] "Malta"                             "San Marino"                       
## [159] "France"                            "Albania"                          
## [161] "New Zealand"                       "Saudi Arabia"                     
## [163] "Italy"                             "Bosnia and Herzegovina"           
## [165] "Montenegro"                        "Singapore"                        
## [167] "Austria"                           "Bulgaria"                         
## [169] "Turkmenistan"                      "Estonia"                          
## [171] "Greece"                            "Switzerland"                      
## [173] "Monaco"                            "Ireland"                          
## [175] "Qatar"                             "Netherlands"                      
## [177] "Belgium"                           "Denmark"                          
## [179] "Korea, South"                      "Sweden"                           
## [181] "Slovakia"                          "Germany"                          
## [183] "Japan"                             "Spain"                            
## [185] "North Macedonia"                   "Iceland"                          
## [187] "Czechia"                           "Croatia"                          
## [189] "United Arab Emirates"              "Slovenia"                         
## [191] "Australia"                         "Israel"                           
## [193] "Poland"                            "Belarus"                          
## [195] "Norway"
dirty_maternal_mortality$Country=gsub('-',' ',dirty_maternal_mortality$Country,fixed = T) # code asks the data frame to update
dirty_maternal_mortality$Country[grepl("[^a-zA-Z\\s]", dirty_maternal_mortality$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces
## [1] "Congo, Democratic Republic of the" "Cote d'Ivoire"                    
## [3] "Gambia, The"                       "Congo, Republic of the"           
## [5] "Micronesia, Federated States of"   "Bahamas, The"                     
## [7] "Korea, North"                      "Turkey (Turkiye)"                 
## [9] "Korea, South"
gsub(',','',dirty_maternal_mortality$Country,fixed = T) # code requests any commas be removed
##   [1] "Nigeria"                          "Chad"                            
##   [3] "South Sudan"                      "Central African Republic"        
##   [5] "Liberia"                          "Somalia"                         
##   [7] "Afghanistan"                      "Benin"                           
##   [9] "Guinea Bissau"                    "Guinea"                          
##  [11] "Lesotho"                          "Madagascar"                      
##  [13] "Congo Democratic Republic of the" "Burundi"                         
##  [15] "Mauritania"                       "Kenya"                           
##  [17] "Mali"                             "Cote d'Ivoire"                   
##  [19] "Zimbabwe"                         "Gambia The"                      
##  [21] "Sierra Leone"                     "Niger"                           
##  [23] "Togo"                             "Haiti"                           
##  [25] "Eritrea"                          "Tanzania"                        
##  [27] "Nauru"                            "Cameroon"                        
##  [29] "Sudan"                            "Burkina Faso"                    
##  [31] "Congo Republic of the"            "Senegal"                         
##  [33] "Ghana"                            "Gabon"                           
##  [35] "Rwanda"                           "Venezuela"                       
##  [37] "Malawi"                           "Ethiopia"                        
##  [39] "Timor Leste"                      "Papua New Guinea"                
##  [41] "Burma"                            "Angola"                          
##  [43] "Comoros"                          "Equatorial Guinea"               
##  [45] "Uganda"                           "Tuvalu"                          
##  [47] "Djibouti"                         "Pakistan"                        
##  [49] "Marshall Islands"                 "Botswana"                        
##  [51] "Bolivia"                          "Nepal"                           
##  [53] "Indonesia"                        "Namibia"                         
##  [55] "Cambodia"                         "Jamaica"                         
##  [57] "Micronesia Federated States of"   "Dominican Republic"              
##  [59] "Solomon Islands"                  "South Africa"                    
##  [61] "Eswatini"                         "Yemen"                           
##  [63] "Bangladesh"                       "Laos"                            
##  [65] "Samoa"                            "Vanuatu"                         
##  [67] "Guatemala"                        "Palau"                           
##  [69] "Zambia"                           "Philippines"                     
##  [71] "Suriname"                         "Mozambique"                      
##  [73] "Kiribati"                         "India"                           
##  [75] "Bahamas The"                      "Guyana"                          
##  [77] "Sao Tome and Principe"            "Saint Kitts and Nevis"           
##  [79] "Morocco"                          "Belize"                          
##  [81] "Korea North"                      "Tonga"                           
##  [83] "Brazil"                           "Iraq"                            
##  [85] "Mauritius"                        "Algeria"                         
##  [87] "Nicaragua"                        "Colombia"                        
##  [89] "Libya"                            "Paraguay"                        
##  [91] "Saint Vincent and the Grenadines" "Ecuador"                         
##  [93] "Trinidad and Tobago"              "Peru"                            
##  [95] "Grenada"                          "Vietnam"                         
##  [97] "Bhutan"                           "Honduras"                        
##  [99] "Saint Lucia"                      "Kyrgyzstan"                      
## [101] "Mexico"                           "Seychelles"                      
## [103] "Mongolia"                         "Cabo Verde"                      
## [105] "El Salvador"                      "Panama"                          
## [107] "Dominica"                         "Tunisia"                         
## [109] "Brunei"                           "Antigua and Barbuda"             
## [111] "Cuba"                             "Barbados"                        
## [113] "Thailand"                         "Argentina"                       
## [115] "Maldives"                         "Jordan"                          
## [117] "Fiji"                             "Uzbekistan"                      
## [119] "Malaysia"                         "Costa Rica"                      
## [121] "Georgia"                          "Syria"                           
## [123] "Latvia"                           "Moldova"                         
## [125] "Armenia"                          "Sri Lanka"                       
## [127] "Azerbaijan"                       "Egypt"                           
## [129] "Bahrain"                          "United States"                   
## [131] "China"                            "West Bank"                       
## [133] "Gaza Strip"                       "Iran"                            
## [135] "Turkey (Turkiye)"                 "Uruguay"                         
## [137] "Portugal"                         "Ukraine"                         
## [139] "Lebanon"                          "Cyprus"                          
## [141] "Tajikistan"                       "Oman"                            
## [143] "Canada"                           "Romania"                         
## [145] "Luxembourg"                       "Hungary"                         
## [147] "Andorra"                          "Serbia"                          
## [149] "Puerto Rico"                      "Chile"                           
## [151] "Kazakhstan"                       "Russia"                          
## [153] "Lithuania"                        "Kuwait"                          
## [155] "United Kingdom"                   "Finland"                         
## [157] "Malta"                            "San Marino"                      
## [159] "France"                           "Albania"                         
## [161] "New Zealand"                      "Saudi Arabia"                    
## [163] "Italy"                            "Bosnia and Herzegovina"          
## [165] "Montenegro"                       "Singapore"                       
## [167] "Austria"                          "Bulgaria"                        
## [169] "Turkmenistan"                     "Estonia"                         
## [171] "Greece"                           "Switzerland"                     
## [173] "Monaco"                           "Ireland"                         
## [175] "Qatar"                            "Netherlands"                     
## [177] "Belgium"                          "Denmark"                         
## [179] "Korea South"                      "Sweden"                          
## [181] "Slovakia"                         "Germany"                         
## [183] "Japan"                            "Spain"                           
## [185] "North Macedonia"                  "Iceland"                         
## [187] "Czechia"                          "Croatia"                         
## [189] "United Arab Emirates"             "Slovenia"                        
## [191] "Australia"                        "Israel"                          
## [193] "Poland"                           "Belarus"                         
## [195] "Norway"
dirty_maternal_mortality$Country=gsub(',','',dirty_maternal_mortality$Country,fixed = T) # code asks the data frame to update
dirty_maternal_mortality$Country[grepl("[^a-zA-Z\\s]", dirty_maternal_mortality$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces. Only two names left that are an issue
## [1] "Cote d'Ivoire"    "Turkey (Turkiye)"
gsub("\\(.*\\)",'',dirty_maternal_mortality$Country,perl = T) # code asks anything inside of parenthases to be removed to get rid of (Turkiye)
##   [1] "Nigeria"                          "Chad"                            
##   [3] "South Sudan"                      "Central African Republic"        
##   [5] "Liberia"                          "Somalia"                         
##   [7] "Afghanistan"                      "Benin"                           
##   [9] "Guinea Bissau"                    "Guinea"                          
##  [11] "Lesotho"                          "Madagascar"                      
##  [13] "Congo Democratic Republic of the" "Burundi"                         
##  [15] "Mauritania"                       "Kenya"                           
##  [17] "Mali"                             "Cote d'Ivoire"                   
##  [19] "Zimbabwe"                         "Gambia The"                      
##  [21] "Sierra Leone"                     "Niger"                           
##  [23] "Togo"                             "Haiti"                           
##  [25] "Eritrea"                          "Tanzania"                        
##  [27] "Nauru"                            "Cameroon"                        
##  [29] "Sudan"                            "Burkina Faso"                    
##  [31] "Congo Republic of the"            "Senegal"                         
##  [33] "Ghana"                            "Gabon"                           
##  [35] "Rwanda"                           "Venezuela"                       
##  [37] "Malawi"                           "Ethiopia"                        
##  [39] "Timor Leste"                      "Papua New Guinea"                
##  [41] "Burma"                            "Angola"                          
##  [43] "Comoros"                          "Equatorial Guinea"               
##  [45] "Uganda"                           "Tuvalu"                          
##  [47] "Djibouti"                         "Pakistan"                        
##  [49] "Marshall Islands"                 "Botswana"                        
##  [51] "Bolivia"                          "Nepal"                           
##  [53] "Indonesia"                        "Namibia"                         
##  [55] "Cambodia"                         "Jamaica"                         
##  [57] "Micronesia Federated States of"   "Dominican Republic"              
##  [59] "Solomon Islands"                  "South Africa"                    
##  [61] "Eswatini"                         "Yemen"                           
##  [63] "Bangladesh"                       "Laos"                            
##  [65] "Samoa"                            "Vanuatu"                         
##  [67] "Guatemala"                        "Palau"                           
##  [69] "Zambia"                           "Philippines"                     
##  [71] "Suriname"                         "Mozambique"                      
##  [73] "Kiribati"                         "India"                           
##  [75] "Bahamas The"                      "Guyana"                          
##  [77] "Sao Tome and Principe"            "Saint Kitts and Nevis"           
##  [79] "Morocco"                          "Belize"                          
##  [81] "Korea North"                      "Tonga"                           
##  [83] "Brazil"                           "Iraq"                            
##  [85] "Mauritius"                        "Algeria"                         
##  [87] "Nicaragua"                        "Colombia"                        
##  [89] "Libya"                            "Paraguay"                        
##  [91] "Saint Vincent and the Grenadines" "Ecuador"                         
##  [93] "Trinidad and Tobago"              "Peru"                            
##  [95] "Grenada"                          "Vietnam"                         
##  [97] "Bhutan"                           "Honduras"                        
##  [99] "Saint Lucia"                      "Kyrgyzstan"                      
## [101] "Mexico"                           "Seychelles"                      
## [103] "Mongolia"                         "Cabo Verde"                      
## [105] "El Salvador"                      "Panama"                          
## [107] "Dominica"                         "Tunisia"                         
## [109] "Brunei"                           "Antigua and Barbuda"             
## [111] "Cuba"                             "Barbados"                        
## [113] "Thailand"                         "Argentina"                       
## [115] "Maldives"                         "Jordan"                          
## [117] "Fiji"                             "Uzbekistan"                      
## [119] "Malaysia"                         "Costa Rica"                      
## [121] "Georgia"                          "Syria"                           
## [123] "Latvia"                           "Moldova"                         
## [125] "Armenia"                          "Sri Lanka"                       
## [127] "Azerbaijan"                       "Egypt"                           
## [129] "Bahrain"                          "United States"                   
## [131] "China"                            "West Bank"                       
## [133] "Gaza Strip"                       "Iran"                            
## [135] "Turkey "                          "Uruguay"                         
## [137] "Portugal"                         "Ukraine"                         
## [139] "Lebanon"                          "Cyprus"                          
## [141] "Tajikistan"                       "Oman"                            
## [143] "Canada"                           "Romania"                         
## [145] "Luxembourg"                       "Hungary"                         
## [147] "Andorra"                          "Serbia"                          
## [149] "Puerto Rico"                      "Chile"                           
## [151] "Kazakhstan"                       "Russia"                          
## [153] "Lithuania"                        "Kuwait"                          
## [155] "United Kingdom"                   "Finland"                         
## [157] "Malta"                            "San Marino"                      
## [159] "France"                           "Albania"                         
## [161] "New Zealand"                      "Saudi Arabia"                    
## [163] "Italy"                            "Bosnia and Herzegovina"          
## [165] "Montenegro"                       "Singapore"                       
## [167] "Austria"                          "Bulgaria"                        
## [169] "Turkmenistan"                     "Estonia"                         
## [171] "Greece"                           "Switzerland"                     
## [173] "Monaco"                           "Ireland"                         
## [175] "Qatar"                            "Netherlands"                     
## [177] "Belgium"                          "Denmark"                         
## [179] "Korea South"                      "Sweden"                          
## [181] "Slovakia"                         "Germany"                         
## [183] "Japan"                            "Spain"                           
## [185] "North Macedonia"                  "Iceland"                         
## [187] "Czechia"                          "Croatia"                         
## [189] "United Arab Emirates"             "Slovenia"                        
## [191] "Australia"                        "Israel"                          
## [193] "Poland"                           "Belarus"                         
## [195] "Norway"
dirty_maternal_mortality$Country=gsub("\\(.*\\)",'',dirty_maternal_mortality$Country,perl = T) # code asks the data frame to update
dirty_maternal_mortality$Country[grepl("[^a-zA-Z\\s]", dirty_maternal_mortality$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces. Only one names left that are an issue but we are going to leave it be
## [1] "Cote d'Ivoire"
dirty_maternal_mortality$Country == trimws(dirty_maternal_mortality$Country) # code checks to see if there are any leading or trailing spaces
##   [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [25]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [37]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [49]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [61]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [73]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [85]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [97]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [109]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [121]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [133]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [145]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [157]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [169]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [181]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [193]  TRUE  TRUE  TRUE
dirty_maternal_mortality$Country <- trimws(dirty_maternal_mortality$Country) # code requests to remove any leading or trailing spaces
dirty_maternal_mortality$Country == trimws(dirty_maternal_mortality$Country) # code checks to see if there are any leading or trailing spaces and everything is good
##   [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [76] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [91] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [106] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [121] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [136] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [151] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [166] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [181] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
str(dirty_maternal_mortality) # Code is checking the columns, the mortality rate column is recognized as numerical so that is okay. 
## tibble [195 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country       : chr [1:195] "Nigeria" "Chad" "South Sudan" "Central African Republic" ...
##  $ Mortality Rate: num [1:195] 993 748 692 692 628 563 521 518 505 494 ...
##  $ Region        : chr [1:195] "Africa" "Africa" "Africa" "Africa" ...
dirty_maternal_mortality$Region == trimws(dirty_maternal_mortality$Region) # code checks to see if there are any leading or trailing spaces in the region column and there are not
##   [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [76] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [91] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [106] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [121] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [136] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [151] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [166] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [181] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

The data has been cleaned. There are no missing values or characters that may be an issue. Let’s move on to formatting.

Check numeric data

summary(dirty_maternal_mortality$`Mortality Rate`) # code is checking the statistical summary of the mortality rate column - we know that it is numerical since it gives us an analysis
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     1.0    11.5    47.0   116.7   155.0   993.0
str(dirty_maternal_mortality) # code is confirming that it is numerical again
## tibble [195 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country       : chr [1:195] "Nigeria" "Chad" "South Sudan" "Central African Republic" ...
##  $ Mortality Rate: num [1:195] 993 748 692 692 628 563 521 518 505 494 ...
##  $ Region        : chr [1:195] "Africa" "Africa" "Africa" "Africa" ...

Check text format

dirty_maternal_mortality$Country=toupper(dirty_maternal_mortality$Country) # code changes all the values in the country column to uppercase
dirty_maternal_mortality$Region=toupper(dirty_maternal_mortality$Region) # code changes all the values in the region column to upper case

The dirty_mortality file has been cleaned and formatted.

Saving Files

saveRDS(dirty_maternal_mortality,"Maternal_Mortality_Formatted.RDS") # code is saving the data frame as an RDS file
Maternal_Mortality_FormattedRDS=readRDS("Maternal_Mortality_Formatted.RDS") # code is reading the RDS file
str(Maternal_Mortality_FormattedRDS) # code is checking the data types in the RDS file
## tibble [195 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country       : chr [1:195] "NIGERIA" "CHAD" "SOUTH SUDAN" "CENTRAL AFRICAN REPUBLIC" ...
##  $ Mortality Rate: num [1:195] 993 748 692 692 628 563 521 518 505 494 ...
##  $ Region        : chr [1:195] "AFRICA" "AFRICA" "AFRICA" "AFRICA" ...
write.csv(dirty_maternal_mortality,"Maternal_Mortality_Formatted.csv", row.names = FALSE) # code is saving the data frame as a csv file
Maternal_Mortality_FormattedCSV=read.csv("Maternal_Mortality_Formatted.csv") # code is reading the csv file
str(Maternal_Mortality_FormattedCSV) # code is checking the data types in the csv file
## 'data.frame':    195 obs. of  3 variables:
##  $ Country       : chr  "NIGERIA" "CHAD" "SOUTH SUDAN" "CENTRAL AFRICAN REPUBLIC" ...
##  $ Mortality.Rate: int  993 748 692 692 628 563 521 518 505 494 ...
##  $ Region        : chr  "AFRICA" "AFRICA" "AFRICA" "AFRICA" ...

Clean and Format Table 2: Infant Mortality Ratio

Import data

library(readr) # code wants to use the package 'readr'
dirty_mortality <- read_csv("Infant mortality rate.csv") # code uses the package 'readr' to read the csv file imported into R and name it dirty_mortality
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 226 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): name, slug, ranking
## dbl (3): deaths/1, 000 live births, date_of_information
## lgl (1): region
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(dirty_mortality) # code views the data set

Checking the values

str(dirty_mortality) # code requests the values in the data as well as confirming the column names
## spc_tbl_ [226 × 7] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ name               : chr [1:226] "Somalia" "Central African Republic" "Equatorial Guinea" "Sierra Leone" ...
##  $ slug               : chr [1:226] "somalia" "central-african-republic" "equatorial-guinea" "sierra-leone" ...
##  $ deaths/1           : num [1:226] 81.5 79.3 76.9 70.1 65.6 63 61.1 58.6 56.7 55.8 ...
##  $ 000 live births    : num [1:226] 2025 2025 2025 2025 2025 ...
##  $ date_of_information: num [1:226] 1 2 3 4 5 6 7 8 9 10 ...
##  $ ranking            : chr [1:226] "Africa" "Africa" "Africa" "Africa" ...
##  $ region             : logi [1:226] NA NA NA NA NA NA ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   name = col_character(),
##   ..   slug = col_character(),
##   ..   `deaths/1` = col_double(),
##   ..   `000 live births` = col_double(),
##   ..   date_of_information = col_double(),
##   ..   ranking = col_character(),
##   ..   region = col_logical()
##   .. )
##  - attr(*, "problems")=<externalptr>

Keeping certain columns and renaming the columns

names(dirty_mortality) # code checks the column names of dirty
## [1] "name"                "slug"                "deaths/1"           
## [4] "000 live births"     "date_of_information" "ranking"            
## [7] "region"
dirty_mortality <- dirty_mortality[,c(1,3,6)] # code keeps the 3 columns needs even though their column names may not be correct yet: we want to keep the country, the variable (deaths) and the region.
as.list(dirty_mortality[1,]) # code helps to notice that when the data was imported, the third column name was shifted between the third and fourth columns and created a shift in data. 
## $name
## [1] "Somalia"
## 
## $`deaths/1`
## [1] 81.5
## 
## $ranking
## [1] "Africa"
names(dirty_mortality)[1:3]=c("Country","Mortality Rate","Region") # code renames the columns appropriately
str(dirty_mortality) # code checks the data types again to make sure we have what we want
## tibble [226 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country       : chr [1:226] "Somalia" "Central African Republic" "Equatorial Guinea" "Sierra Leone" ...
##  $ Mortality Rate: num [1:226] 81.5 79.3 76.9 70.1 65.6 63 61.1 58.6 56.7 55.8 ...
##  $ Region        : chr [1:226] "Africa" "Africa" "Africa" "Africa" ...

Checking and Cleaning characters within the columns

dirty_mortality$Country[grepl("[^a-zA-Z]", dirty_mortality$Country)] # Code requests any cells in the Country column that aren't only upper and lower case letters. Helps to notice that we have spaces, commas, apostrophes, dashes, and parenthases. However, countries with spaces are fine so let's try something else. 
##  [1] "Central African Republic"                     
##  [2] "Equatorial Guinea"                            
##  [3] "Sierra Leone"                                 
##  [4] "South Sudan"                                  
##  [5] "Congo, Democratic Republic of the"            
##  [6] "Cote d'Ivoire"                                
##  [7] "Burkina Faso"                                 
##  [8] "Guinea-Bissau"                                
##  [9] "Timor-Leste"                                  
## [10] "Sao Tome and Principe"                        
## [11] "Gambia, The"                                  
## [12] "Papua New Guinea"                             
## [13] "Congo, Republic of the"                       
## [14] "Cabo Verde"                                   
## [15] "Dominican Republic"                           
## [16] "Micronesia, Federated States of"              
## [17] "Marshall Islands"                             
## [18] "South Africa"                                 
## [19] "Solomon Islands"                              
## [20] "Turkey (Turkiye)"                             
## [21] "Saint Helena, Ascension, and Tristan da Cunha"
## [22] "Cook Islands"                                 
## [23] "Korea, North"                                 
## [24] "Trinidad and Tobago"                          
## [25] "Gaza Strip"                                   
## [26] "West Bank"                                    
## [27] "Antigua and Barbuda"                          
## [28] "British Virgin Islands"                       
## [29] "Saint Vincent and the Grenadines"             
## [30] "Northern Mariana Islands"                     
## [31] "Saudi Arabia"                                 
## [32] "Saint Lucia"                                  
## [33] "Turks and Caicos Islands"                     
## [34] "El Salvador"                                  
## [35] "American Samoa"                               
## [36] "Bahamas, The"                                 
## [37] "Saint Kitts and Nevis"                        
## [38] "Saint Pierre and Miquelon"                    
## [39] "Sint Maarten"                                 
## [40] "Virgin Islands"                               
## [41] "Cayman Islands"                               
## [42] "North Macedonia"                              
## [43] "Sri Lanka"                                    
## [44] "Costa Rica"                                   
## [45] "Saint Martin"                                 
## [46] "Saint Barthelemy"                             
## [47] "San Marino"                                   
## [48] "Puerto Rico"                                  
## [49] "Faroe Islands"                                
## [50] "United States"                                
## [51] "United Arab Emirates"                         
## [52] "New Caledonia"                                
## [53] "French Polynesia"                             
## [54] "Isle of Man"                                  
## [55] "Wallis and Futuna"                            
## [56] "Bosnia and Herzegovina"                       
## [57] "United Kingdom"                               
## [58] "New Zealand"                                  
## [59] "Hong Kong"                                    
## [60] "Korea, South"
dirty_mortality$Country[grepl("[^a-zA-Z\\s]", dirty_mortality$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces
##  [1] "Congo, Democratic Republic of the"            
##  [2] "Cote d'Ivoire"                                
##  [3] "Guinea-Bissau"                                
##  [4] "Timor-Leste"                                  
##  [5] "Gambia, The"                                  
##  [6] "Congo, Republic of the"                       
##  [7] "Micronesia, Federated States of"              
##  [8] "Turkey (Turkiye)"                             
##  [9] "Saint Helena, Ascension, and Tristan da Cunha"
## [10] "Korea, North"                                 
## [11] "Bahamas, The"                                 
## [12] "Korea, South"
gsub('-',' ',dirty_mortality$Country,fixed = T) # code requests all dashes be replaced with spaces
##   [1] "Somalia"                                      
##   [2] "Central African Republic"                     
##   [3] "Equatorial Guinea"                            
##   [4] "Sierra Leone"                                 
##   [5] "Nigeria"                                      
##   [6] "Niger"                                        
##   [7] "Chad"                                         
##   [8] "South Sudan"                                  
##   [9] "Mozambique"                                   
##  [10] "Mali"                                         
##  [11] "Congo, Democratic Republic of the"            
##  [12] "Liberia"                                      
##  [13] "Comoros"                                      
##  [14] "Cote d'Ivoire"                                
##  [15] "Lesotho"                                      
##  [16] "Benin"                                        
##  [17] "Pakistan"                                     
##  [18] "Burkina Faso"                                 
##  [19] "Angola"                                       
##  [20] "Guinea"                                       
##  [21] "Guinea Bissau"                                
##  [22] "Cameroon"                                     
##  [23] "Djibouti"                                     
##  [24] "Yemen"                                        
##  [25] "Afghanistan"                                  
##  [26] "Timor Leste"                                  
##  [27] "Sao Tome and Principe"                        
##  [28] "Sudan"                                        
##  [29] "Eritrea"                                      
##  [30] "Madagascar"                                   
##  [31] "Eswatini"                                     
##  [32] "Burundi"                                      
##  [33] "Haiti"                                        
##  [34] "Gambia, The"                                  
##  [35] "Turkmenistan"                                 
##  [36] "Togo"                                         
##  [37] "Laos"                                         
##  [38] "Zimbabwe"                                     
##  [39] "Papua New Guinea"                             
##  [40] "Ethiopia"                                     
##  [41] "Kiribati"                                     
##  [42] "Burma"                                        
##  [43] "Ghana"                                        
##  [44] "India"                                        
##  [45] "Senegal"                                      
##  [46] "Zambia"                                       
##  [47] "Mauritania"                                   
##  [48] "Congo, Republic of the"                       
##  [49] "Suriname"                                     
##  [50] "Malawi"                                       
##  [51] "Tanzania"                                     
##  [52] "Botswana"                                     
##  [53] "Uganda"                                       
##  [54] "Rwanda"                                       
##  [55] "Cambodia"                                     
##  [56] "Tuvalu"                                       
##  [57] "Namibia"                                      
##  [58] "Kenya"                                        
##  [59] "Gabon"                                        
##  [60] "Kyrgyzstan"                                   
##  [61] "Maldives"                                     
##  [62] "Guatemala"                                    
##  [63] "Nepal"                                        
##  [64] "Bhutan"                                       
##  [65] "Bolivia"                                      
##  [66] "Bangladesh"                                   
##  [67] "Cabo Verde"                                   
##  [68] "Paraguay"                                     
##  [69] "Kosovo"                                       
##  [70] "Georgia"                                      
##  [71] "Dominican Republic"                           
##  [72] "Guyana"                                       
##  [73] "Micronesia, Federated States of"              
##  [74] "Tajikistan"                                   
##  [75] "Marshall Islands"                             
##  [76] "South Africa"                                 
##  [77] "Solomon Islands"                              
##  [78] "Algeria"                                      
##  [79] "Indonesia"                                    
##  [80] "Philippines"                                  
##  [81] "Iraq"                                         
##  [82] "Turkey (Turkiye)"                             
##  [83] "Morocco"                                      
##  [84] "Uzbekistan"                                   
##  [85] "Saint Helena, Ascension, and Tristan da Cunha"
##  [86] "Samoa"                                        
##  [87] "Egypt"                                        
##  [88] "Honduras"                                     
##  [89] "Barbados"                                     
##  [90] "Cook Islands"                                 
##  [91] "Korea, North"                                 
##  [92] "Syria"                                        
##  [93] "Trinidad and Tobago"                          
##  [94] "Jamaica"                                      
##  [95] "Gaza Strip"                                   
##  [96] "West Bank"                                    
##  [97] "Nicaragua"                                    
##  [98] "Vietnam"                                      
##  [99] "Oman"                                         
## [100] "Venezuela"                                    
## [101] "Moldova"                                      
## [102] "Vanuatu"                                      
## [103] "Antigua and Barbuda"                          
## [104] "Panama"                                       
## [105] "British Virgin Islands"                       
## [106] "Jordan"                                       
## [107] "Brazil"                                       
## [108] "Mexico"                                       
## [109] "Saint Vincent and the Grenadines"             
## [110] "Northern Mariana Islands"                     
## [111] "Colombia"                                     
## [112] "Tonga"                                        
## [113] "Saudi Arabia"                                 
## [114] "Mauritius"                                    
## [115] "Saint Lucia"                                  
## [116] "Armenia"                                      
## [117] "Belize"                                       
## [118] "Aruba"                                        
## [119] "Ecuador"                                      
## [120] "Turks and Caicos Islands"                     
## [121] "Albania"                                      
## [122] "Azerbaijan"                                   
## [123] "Guam"                                         
## [124] "Tunisia"                                      
## [125] "Peru"                                         
## [126] "Palau"                                        
## [127] "Dominica"                                     
## [128] "Libya"                                        
## [129] "Seychelles"                                   
## [130] "El Salvador"                                  
## [131] "Brunei"                                       
## [132] "Iran"                                         
## [133] "Bahrain"                                      
## [134] "American Samoa"                               
## [135] "Fiji"                                         
## [136] "Montserrat"                                   
## [137] "Bahamas, The"                                 
## [138] "Grenada"                                      
## [139] "Mongolia"                                     
## [140] "Ukraine"                                      
## [141] "Greenland"                                    
## [142] "Cyprus"                                       
## [143] "Kazakhstan"                                   
## [144] "Saint Kitts and Nevis"                        
## [145] "Saint Pierre and Miquelon"                    
## [146] "Sint Maarten"                                 
## [147] "Bulgaria"                                     
## [148] "Nauru"                                        
## [149] "Curacao"                                      
## [150] "Argentina"                                    
## [151] "Lebanon"                                      
## [152] "Virgin Islands"                               
## [153] "Cayman Islands"                               
## [154] "Kuwait"                                       
## [155] "Uruguay"                                      
## [156] "North Macedonia"                              
## [157] "Sri Lanka"                                    
## [158] "Russia"                                       
## [159] "Costa Rica"                                   
## [160] "Saint Martin"                                 
## [161] "Malaysia"                                     
## [162] "Saint Barthelemy"                             
## [163] "Thailand"                                     
## [164] "Qatar"                                        
## [165] "San Marino"                                   
## [166] "China"                                        
## [167] "Gibraltar"                                    
## [168] "Puerto Rico"                                  
## [169] "Faroe Islands"                                
## [170] "Romania"                                      
## [171] "Cuba"                                         
## [172] "United States"                                
## [173] "Slovakia"                                     
## [174] "United Arab Emirates"                         
## [175] "New Caledonia"                                
## [176] "Poland"                                       
## [177] "Malta"                                        
## [178] "Hungary"                                      
## [179] "Chile"                                        
## [180] "Serbia"                                       
## [181] "Macau"                                        
## [182] "French Polynesia"                             
## [183] "Isle of Man"                                  
## [184] "Wallis and Futuna"                            
## [185] "Canada"                                       
## [186] "Bosnia and Herzegovina"                       
## [187] "Taiwan"                                       
## [188] "Liechtenstein"                                
## [189] "Jersey"                                       
## [190] "United Kingdom"                               
## [191] "Netherlands"                                  
## [192] "Croatia"                                      
## [193] "Greece"                                       
## [194] "New Zealand"                                  
## [195] "Andorra"                                      
## [196] "Guernsey"                                     
## [197] "Austria"                                      
## [198] "Ireland"                                      
## [199] "France"                                       
## [200] "Montenegro"                                   
## [201] "Luxembourg"                                   
## [202] "Denmark"                                      
## [203] "Australia"                                    
## [204] "Switzerland"                                  
## [205] "Belgium"                                      
## [206] "Germany"                                      
## [207] "Italy"                                        
## [208] "Anguilla"                                     
## [209] "Israel"                                       
## [210] "Lithuania"                                    
## [211] "Czechia"                                      
## [212] "Hong Kong"                                    
## [213] "Portugal"                                     
## [214] "Spain"                                        
## [215] "Latvia"                                       
## [216] "Sweden"                                       
## [217] "Korea, South"                                 
## [218] "Belarus"                                      
## [219] "Bermuda"                                      
## [220] "Estonia"                                      
## [221] "Finland"                                      
## [222] "Norway"                                       
## [223] "Japan"                                        
## [224] "Slovenia"                                     
## [225] "Monaco"                                       
## [226] "Iceland"
dirty_mortality$Country=gsub('-',' ',dirty_mortality$Country,fixed = T) # code asks the data frame to update
dirty_mortality$Country[grepl("[^a-zA-Z\\s]", dirty_mortality$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces
##  [1] "Congo, Democratic Republic of the"            
##  [2] "Cote d'Ivoire"                                
##  [3] "Gambia, The"                                  
##  [4] "Congo, Republic of the"                       
##  [5] "Micronesia, Federated States of"              
##  [6] "Turkey (Turkiye)"                             
##  [7] "Saint Helena, Ascension, and Tristan da Cunha"
##  [8] "Korea, North"                                 
##  [9] "Bahamas, The"                                 
## [10] "Korea, South"
gsub(',','',dirty_mortality$Country,fixed = T) # code requests any commas be removed
##   [1] "Somalia"                                    
##   [2] "Central African Republic"                   
##   [3] "Equatorial Guinea"                          
##   [4] "Sierra Leone"                               
##   [5] "Nigeria"                                    
##   [6] "Niger"                                      
##   [7] "Chad"                                       
##   [8] "South Sudan"                                
##   [9] "Mozambique"                                 
##  [10] "Mali"                                       
##  [11] "Congo Democratic Republic of the"           
##  [12] "Liberia"                                    
##  [13] "Comoros"                                    
##  [14] "Cote d'Ivoire"                              
##  [15] "Lesotho"                                    
##  [16] "Benin"                                      
##  [17] "Pakistan"                                   
##  [18] "Burkina Faso"                               
##  [19] "Angola"                                     
##  [20] "Guinea"                                     
##  [21] "Guinea Bissau"                              
##  [22] "Cameroon"                                   
##  [23] "Djibouti"                                   
##  [24] "Yemen"                                      
##  [25] "Afghanistan"                                
##  [26] "Timor Leste"                                
##  [27] "Sao Tome and Principe"                      
##  [28] "Sudan"                                      
##  [29] "Eritrea"                                    
##  [30] "Madagascar"                                 
##  [31] "Eswatini"                                   
##  [32] "Burundi"                                    
##  [33] "Haiti"                                      
##  [34] "Gambia The"                                 
##  [35] "Turkmenistan"                               
##  [36] "Togo"                                       
##  [37] "Laos"                                       
##  [38] "Zimbabwe"                                   
##  [39] "Papua New Guinea"                           
##  [40] "Ethiopia"                                   
##  [41] "Kiribati"                                   
##  [42] "Burma"                                      
##  [43] "Ghana"                                      
##  [44] "India"                                      
##  [45] "Senegal"                                    
##  [46] "Zambia"                                     
##  [47] "Mauritania"                                 
##  [48] "Congo Republic of the"                      
##  [49] "Suriname"                                   
##  [50] "Malawi"                                     
##  [51] "Tanzania"                                   
##  [52] "Botswana"                                   
##  [53] "Uganda"                                     
##  [54] "Rwanda"                                     
##  [55] "Cambodia"                                   
##  [56] "Tuvalu"                                     
##  [57] "Namibia"                                    
##  [58] "Kenya"                                      
##  [59] "Gabon"                                      
##  [60] "Kyrgyzstan"                                 
##  [61] "Maldives"                                   
##  [62] "Guatemala"                                  
##  [63] "Nepal"                                      
##  [64] "Bhutan"                                     
##  [65] "Bolivia"                                    
##  [66] "Bangladesh"                                 
##  [67] "Cabo Verde"                                 
##  [68] "Paraguay"                                   
##  [69] "Kosovo"                                     
##  [70] "Georgia"                                    
##  [71] "Dominican Republic"                         
##  [72] "Guyana"                                     
##  [73] "Micronesia Federated States of"             
##  [74] "Tajikistan"                                 
##  [75] "Marshall Islands"                           
##  [76] "South Africa"                               
##  [77] "Solomon Islands"                            
##  [78] "Algeria"                                    
##  [79] "Indonesia"                                  
##  [80] "Philippines"                                
##  [81] "Iraq"                                       
##  [82] "Turkey (Turkiye)"                           
##  [83] "Morocco"                                    
##  [84] "Uzbekistan"                                 
##  [85] "Saint Helena Ascension and Tristan da Cunha"
##  [86] "Samoa"                                      
##  [87] "Egypt"                                      
##  [88] "Honduras"                                   
##  [89] "Barbados"                                   
##  [90] "Cook Islands"                               
##  [91] "Korea North"                                
##  [92] "Syria"                                      
##  [93] "Trinidad and Tobago"                        
##  [94] "Jamaica"                                    
##  [95] "Gaza Strip"                                 
##  [96] "West Bank"                                  
##  [97] "Nicaragua"                                  
##  [98] "Vietnam"                                    
##  [99] "Oman"                                       
## [100] "Venezuela"                                  
## [101] "Moldova"                                    
## [102] "Vanuatu"                                    
## [103] "Antigua and Barbuda"                        
## [104] "Panama"                                     
## [105] "British Virgin Islands"                     
## [106] "Jordan"                                     
## [107] "Brazil"                                     
## [108] "Mexico"                                     
## [109] "Saint Vincent and the Grenadines"           
## [110] "Northern Mariana Islands"                   
## [111] "Colombia"                                   
## [112] "Tonga"                                      
## [113] "Saudi Arabia"                               
## [114] "Mauritius"                                  
## [115] "Saint Lucia"                                
## [116] "Armenia"                                    
## [117] "Belize"                                     
## [118] "Aruba"                                      
## [119] "Ecuador"                                    
## [120] "Turks and Caicos Islands"                   
## [121] "Albania"                                    
## [122] "Azerbaijan"                                 
## [123] "Guam"                                       
## [124] "Tunisia"                                    
## [125] "Peru"                                       
## [126] "Palau"                                      
## [127] "Dominica"                                   
## [128] "Libya"                                      
## [129] "Seychelles"                                 
## [130] "El Salvador"                                
## [131] "Brunei"                                     
## [132] "Iran"                                       
## [133] "Bahrain"                                    
## [134] "American Samoa"                             
## [135] "Fiji"                                       
## [136] "Montserrat"                                 
## [137] "Bahamas The"                                
## [138] "Grenada"                                    
## [139] "Mongolia"                                   
## [140] "Ukraine"                                    
## [141] "Greenland"                                  
## [142] "Cyprus"                                     
## [143] "Kazakhstan"                                 
## [144] "Saint Kitts and Nevis"                      
## [145] "Saint Pierre and Miquelon"                  
## [146] "Sint Maarten"                               
## [147] "Bulgaria"                                   
## [148] "Nauru"                                      
## [149] "Curacao"                                    
## [150] "Argentina"                                  
## [151] "Lebanon"                                    
## [152] "Virgin Islands"                             
## [153] "Cayman Islands"                             
## [154] "Kuwait"                                     
## [155] "Uruguay"                                    
## [156] "North Macedonia"                            
## [157] "Sri Lanka"                                  
## [158] "Russia"                                     
## [159] "Costa Rica"                                 
## [160] "Saint Martin"                               
## [161] "Malaysia"                                   
## [162] "Saint Barthelemy"                           
## [163] "Thailand"                                   
## [164] "Qatar"                                      
## [165] "San Marino"                                 
## [166] "China"                                      
## [167] "Gibraltar"                                  
## [168] "Puerto Rico"                                
## [169] "Faroe Islands"                              
## [170] "Romania"                                    
## [171] "Cuba"                                       
## [172] "United States"                              
## [173] "Slovakia"                                   
## [174] "United Arab Emirates"                       
## [175] "New Caledonia"                              
## [176] "Poland"                                     
## [177] "Malta"                                      
## [178] "Hungary"                                    
## [179] "Chile"                                      
## [180] "Serbia"                                     
## [181] "Macau"                                      
## [182] "French Polynesia"                           
## [183] "Isle of Man"                                
## [184] "Wallis and Futuna"                          
## [185] "Canada"                                     
## [186] "Bosnia and Herzegovina"                     
## [187] "Taiwan"                                     
## [188] "Liechtenstein"                              
## [189] "Jersey"                                     
## [190] "United Kingdom"                             
## [191] "Netherlands"                                
## [192] "Croatia"                                    
## [193] "Greece"                                     
## [194] "New Zealand"                                
## [195] "Andorra"                                    
## [196] "Guernsey"                                   
## [197] "Austria"                                    
## [198] "Ireland"                                    
## [199] "France"                                     
## [200] "Montenegro"                                 
## [201] "Luxembourg"                                 
## [202] "Denmark"                                    
## [203] "Australia"                                  
## [204] "Switzerland"                                
## [205] "Belgium"                                    
## [206] "Germany"                                    
## [207] "Italy"                                      
## [208] "Anguilla"                                   
## [209] "Israel"                                     
## [210] "Lithuania"                                  
## [211] "Czechia"                                    
## [212] "Hong Kong"                                  
## [213] "Portugal"                                   
## [214] "Spain"                                      
## [215] "Latvia"                                     
## [216] "Sweden"                                     
## [217] "Korea South"                                
## [218] "Belarus"                                    
## [219] "Bermuda"                                    
## [220] "Estonia"                                    
## [221] "Finland"                                    
## [222] "Norway"                                     
## [223] "Japan"                                      
## [224] "Slovenia"                                   
## [225] "Monaco"                                     
## [226] "Iceland"
dirty_mortality$Country=gsub(',','',dirty_mortality$Country,fixed = T) # code asks the data frame to update
dirty_mortality$Country[grepl("[^a-zA-Z\\s]", dirty_mortality$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces. Only two names left that are an issue
## [1] "Cote d'Ivoire"    "Turkey (Turkiye)"
gsub("\\(.*\\)",'',dirty_mortality$Country,perl = T) # code asks anything inside of parentheses to be removed to get rid of (Turkiye)
##   [1] "Somalia"                                    
##   [2] "Central African Republic"                   
##   [3] "Equatorial Guinea"                          
##   [4] "Sierra Leone"                               
##   [5] "Nigeria"                                    
##   [6] "Niger"                                      
##   [7] "Chad"                                       
##   [8] "South Sudan"                                
##   [9] "Mozambique"                                 
##  [10] "Mali"                                       
##  [11] "Congo Democratic Republic of the"           
##  [12] "Liberia"                                    
##  [13] "Comoros"                                    
##  [14] "Cote d'Ivoire"                              
##  [15] "Lesotho"                                    
##  [16] "Benin"                                      
##  [17] "Pakistan"                                   
##  [18] "Burkina Faso"                               
##  [19] "Angola"                                     
##  [20] "Guinea"                                     
##  [21] "Guinea Bissau"                              
##  [22] "Cameroon"                                   
##  [23] "Djibouti"                                   
##  [24] "Yemen"                                      
##  [25] "Afghanistan"                                
##  [26] "Timor Leste"                                
##  [27] "Sao Tome and Principe"                      
##  [28] "Sudan"                                      
##  [29] "Eritrea"                                    
##  [30] "Madagascar"                                 
##  [31] "Eswatini"                                   
##  [32] "Burundi"                                    
##  [33] "Haiti"                                      
##  [34] "Gambia The"                                 
##  [35] "Turkmenistan"                               
##  [36] "Togo"                                       
##  [37] "Laos"                                       
##  [38] "Zimbabwe"                                   
##  [39] "Papua New Guinea"                           
##  [40] "Ethiopia"                                   
##  [41] "Kiribati"                                   
##  [42] "Burma"                                      
##  [43] "Ghana"                                      
##  [44] "India"                                      
##  [45] "Senegal"                                    
##  [46] "Zambia"                                     
##  [47] "Mauritania"                                 
##  [48] "Congo Republic of the"                      
##  [49] "Suriname"                                   
##  [50] "Malawi"                                     
##  [51] "Tanzania"                                   
##  [52] "Botswana"                                   
##  [53] "Uganda"                                     
##  [54] "Rwanda"                                     
##  [55] "Cambodia"                                   
##  [56] "Tuvalu"                                     
##  [57] "Namibia"                                    
##  [58] "Kenya"                                      
##  [59] "Gabon"                                      
##  [60] "Kyrgyzstan"                                 
##  [61] "Maldives"                                   
##  [62] "Guatemala"                                  
##  [63] "Nepal"                                      
##  [64] "Bhutan"                                     
##  [65] "Bolivia"                                    
##  [66] "Bangladesh"                                 
##  [67] "Cabo Verde"                                 
##  [68] "Paraguay"                                   
##  [69] "Kosovo"                                     
##  [70] "Georgia"                                    
##  [71] "Dominican Republic"                         
##  [72] "Guyana"                                     
##  [73] "Micronesia Federated States of"             
##  [74] "Tajikistan"                                 
##  [75] "Marshall Islands"                           
##  [76] "South Africa"                               
##  [77] "Solomon Islands"                            
##  [78] "Algeria"                                    
##  [79] "Indonesia"                                  
##  [80] "Philippines"                                
##  [81] "Iraq"                                       
##  [82] "Turkey "                                    
##  [83] "Morocco"                                    
##  [84] "Uzbekistan"                                 
##  [85] "Saint Helena Ascension and Tristan da Cunha"
##  [86] "Samoa"                                      
##  [87] "Egypt"                                      
##  [88] "Honduras"                                   
##  [89] "Barbados"                                   
##  [90] "Cook Islands"                               
##  [91] "Korea North"                                
##  [92] "Syria"                                      
##  [93] "Trinidad and Tobago"                        
##  [94] "Jamaica"                                    
##  [95] "Gaza Strip"                                 
##  [96] "West Bank"                                  
##  [97] "Nicaragua"                                  
##  [98] "Vietnam"                                    
##  [99] "Oman"                                       
## [100] "Venezuela"                                  
## [101] "Moldova"                                    
## [102] "Vanuatu"                                    
## [103] "Antigua and Barbuda"                        
## [104] "Panama"                                     
## [105] "British Virgin Islands"                     
## [106] "Jordan"                                     
## [107] "Brazil"                                     
## [108] "Mexico"                                     
## [109] "Saint Vincent and the Grenadines"           
## [110] "Northern Mariana Islands"                   
## [111] "Colombia"                                   
## [112] "Tonga"                                      
## [113] "Saudi Arabia"                               
## [114] "Mauritius"                                  
## [115] "Saint Lucia"                                
## [116] "Armenia"                                    
## [117] "Belize"                                     
## [118] "Aruba"                                      
## [119] "Ecuador"                                    
## [120] "Turks and Caicos Islands"                   
## [121] "Albania"                                    
## [122] "Azerbaijan"                                 
## [123] "Guam"                                       
## [124] "Tunisia"                                    
## [125] "Peru"                                       
## [126] "Palau"                                      
## [127] "Dominica"                                   
## [128] "Libya"                                      
## [129] "Seychelles"                                 
## [130] "El Salvador"                                
## [131] "Brunei"                                     
## [132] "Iran"                                       
## [133] "Bahrain"                                    
## [134] "American Samoa"                             
## [135] "Fiji"                                       
## [136] "Montserrat"                                 
## [137] "Bahamas The"                                
## [138] "Grenada"                                    
## [139] "Mongolia"                                   
## [140] "Ukraine"                                    
## [141] "Greenland"                                  
## [142] "Cyprus"                                     
## [143] "Kazakhstan"                                 
## [144] "Saint Kitts and Nevis"                      
## [145] "Saint Pierre and Miquelon"                  
## [146] "Sint Maarten"                               
## [147] "Bulgaria"                                   
## [148] "Nauru"                                      
## [149] "Curacao"                                    
## [150] "Argentina"                                  
## [151] "Lebanon"                                    
## [152] "Virgin Islands"                             
## [153] "Cayman Islands"                             
## [154] "Kuwait"                                     
## [155] "Uruguay"                                    
## [156] "North Macedonia"                            
## [157] "Sri Lanka"                                  
## [158] "Russia"                                     
## [159] "Costa Rica"                                 
## [160] "Saint Martin"                               
## [161] "Malaysia"                                   
## [162] "Saint Barthelemy"                           
## [163] "Thailand"                                   
## [164] "Qatar"                                      
## [165] "San Marino"                                 
## [166] "China"                                      
## [167] "Gibraltar"                                  
## [168] "Puerto Rico"                                
## [169] "Faroe Islands"                              
## [170] "Romania"                                    
## [171] "Cuba"                                       
## [172] "United States"                              
## [173] "Slovakia"                                   
## [174] "United Arab Emirates"                       
## [175] "New Caledonia"                              
## [176] "Poland"                                     
## [177] "Malta"                                      
## [178] "Hungary"                                    
## [179] "Chile"                                      
## [180] "Serbia"                                     
## [181] "Macau"                                      
## [182] "French Polynesia"                           
## [183] "Isle of Man"                                
## [184] "Wallis and Futuna"                          
## [185] "Canada"                                     
## [186] "Bosnia and Herzegovina"                     
## [187] "Taiwan"                                     
## [188] "Liechtenstein"                              
## [189] "Jersey"                                     
## [190] "United Kingdom"                             
## [191] "Netherlands"                                
## [192] "Croatia"                                    
## [193] "Greece"                                     
## [194] "New Zealand"                                
## [195] "Andorra"                                    
## [196] "Guernsey"                                   
## [197] "Austria"                                    
## [198] "Ireland"                                    
## [199] "France"                                     
## [200] "Montenegro"                                 
## [201] "Luxembourg"                                 
## [202] "Denmark"                                    
## [203] "Australia"                                  
## [204] "Switzerland"                                
## [205] "Belgium"                                    
## [206] "Germany"                                    
## [207] "Italy"                                      
## [208] "Anguilla"                                   
## [209] "Israel"                                     
## [210] "Lithuania"                                  
## [211] "Czechia"                                    
## [212] "Hong Kong"                                  
## [213] "Portugal"                                   
## [214] "Spain"                                      
## [215] "Latvia"                                     
## [216] "Sweden"                                     
## [217] "Korea South"                                
## [218] "Belarus"                                    
## [219] "Bermuda"                                    
## [220] "Estonia"                                    
## [221] "Finland"                                    
## [222] "Norway"                                     
## [223] "Japan"                                      
## [224] "Slovenia"                                   
## [225] "Monaco"                                     
## [226] "Iceland"
dirty_mortality$Country=gsub("\\(.*\\)",'',dirty_mortality$Country,perl = T) # code asks the data frame to update
dirty_mortality$Country[grepl("[^a-zA-Z\\s]", dirty_mortality$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces. Only one names left that are an issue but we are going to leave it be
## [1] "Cote d'Ivoire"
dirty_mortality$Country == trimws(dirty_mortality$Country) # code checks to see if there are any leading or trailing spaces
##   [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [25]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [37]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [49]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [61]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [73]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
##  [85]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [97]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [109]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [121]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [133]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [145]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [157]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [169]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [181]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [193]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [205]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [217]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
dirty_mortality$Country <- trimws(dirty_mortality$Country) # code requests to remove any leading or trailing spaces
dirty_mortality$Country == trimws(dirty_mortality$Country) # code checks to see if there are any leading or trailing spaces and everything is good
##   [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [76] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [91] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [106] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [121] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [136] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [151] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [166] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [181] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [196] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [211] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [226] TRUE
str(dirty_mortality) # Code is checking the columns, the mortality rate column is recognized as numerical so that is okay. 
## tibble [226 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country       : chr [1:226] "Somalia" "Central African Republic" "Equatorial Guinea" "Sierra Leone" ...
##  $ Mortality Rate: num [1:226] 81.5 79.3 76.9 70.1 65.6 63 61.1 58.6 56.7 55.8 ...
##  $ Region        : chr [1:226] "Africa" "Africa" "Africa" "Africa" ...
dirty_mortality$Region == trimws(dirty_mortality$Region) # code checks to see if there are any leading or trailing spaces in the region column and there are not
##   [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [76] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [91] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [106] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [121] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [136] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [151] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [166] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [181] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [196] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [211] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [226] TRUE

The data has been cleaned. There are no missing values or characters that may be an issue. Let’s move on to formatting.

Check numeric data

summary(dirty_mortality$`Mortality Rate`) # code is checking the statistical summary of the mortality rate column - we know that it is numerical since it gives us an analysis
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.600   5.375  11.400  17.988  27.025  81.500
str(dirty_mortality) # code is confirming that it is numerical again
## tibble [226 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country       : chr [1:226] "Somalia" "Central African Republic" "Equatorial Guinea" "Sierra Leone" ...
##  $ Mortality Rate: num [1:226] 81.5 79.3 76.9 70.1 65.6 63 61.1 58.6 56.7 55.8 ...
##  $ Region        : chr [1:226] "Africa" "Africa" "Africa" "Africa" ...

Check text format

dirty_mortality$Country=toupper(dirty_mortality$Country) # code changes all the values in the country column to uppercase
dirty_mortality$Region=toupper(dirty_mortality$Region) # code changes all the values in the region column to upper case

The dirty_mortality file has been cleaned and formatted.

Saving Files

saveRDS(dirty_mortality,"Infant_Mortality_Formatted.RDS") # code is savings the data frame as an RDS file
Infant_Mortality_FormattedRDS=readRDS("Infant_Mortality_Formatted.RDS") # code is reading the RDS file
str(Infant_Mortality_FormattedRDS) # code is checking the data types in the RDS file
## tibble [226 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country       : chr [1:226] "SOMALIA" "CENTRAL AFRICAN REPUBLIC" "EQUATORIAL GUINEA" "SIERRA LEONE" ...
##  $ Mortality Rate: num [1:226] 81.5 79.3 76.9 70.1 65.6 63 61.1 58.6 56.7 55.8 ...
##  $ Region        : chr [1:226] "AFRICA" "AFRICA" "AFRICA" "AFRICA" ...
write.csv(dirty_mortality,"Infant_Mortality_Formatted.csv", row.names = FALSE) # code is savings the data frame as a csv file
Infant_Mortality_FormattedCSV=read.csv("Infant_Mortality_Formatted.csv") # code is reading the csv file
str(Infant_Mortality_FormattedCSV) # code is checking the data types of the csv file
## 'data.frame':    226 obs. of  3 variables:
##  $ Country       : chr  "SOMALIA" "CENTRAL AFRICAN REPUBLIC" "EQUATORIAL GUINEA" "SIERRA LEONE" ...
##  $ Mortality.Rate: num  81.5 79.3 76.9 70.1 65.6 63 61.1 58.6 56.7 55.8 ...
##  $ Region        : chr  "AFRICA" "AFRICA" "AFRICA" "AFRICA" ...

Clean and Format Table 3: Life Expectancy at Birth

Import Data

library(readr)
dirty_life_expectancy <- read_csv("Life expectancy at birth.csv")
## Rows: 227 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): name, slug, region
## dbl (3): years, date_of_information, ranking
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(dirty_life_expectancy)

Checking the values

str(dirty_life_expectancy) # code requests the values in the data as well as confirming the column names
## spc_tbl_ [227 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ name               : chr [1:227] "Monaco" "Singapore" "Macau" "Japan" ...
##  $ slug               : chr [1:227] "monaco" "singapore" "macau" "japan" ...
##  $ years              : num [1:227] 89.8 86.7 85.3 85.2 84.2 84.2 84 84 83.9 83.8 ...
##  $ date_of_information: num [1:227] 2024 2024 2024 2024 2024 ...
##  $ ranking            : num [1:227] 1 2 3 4 5 6 7 8 9 10 ...
##  $ region             : chr [1:227] "Europe" "East and Southeast Asia" "East and Southeast Asia" "East and Southeast Asia" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   name = col_character(),
##   ..   slug = col_character(),
##   ..   years = col_double(),
##   ..   date_of_information = col_double(),
##   ..   ranking = col_double(),
##   ..   region = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>

Keeping certain columns and renaming the columns

names(dirty_life_expectancy) # code checks the column names of dirty
## [1] "name"                "slug"                "years"              
## [4] "date_of_information" "ranking"             "region"
dirty_life_expectancy <- dirty_life_expectancy[,c(1,3,6)] # code keeps the 3 columns needs even though their column names may not be correct yet: we want to keep the country, the variable (deaths) and the region.
as.list(dirty_life_expectancy[1,]) # code helps to notice that when the data was imported, the third column name was shifted between the third and fourth columns and created a shift in data. 
## $name
## [1] "Monaco"
## 
## $years
## [1] 89.8
## 
## $region
## [1] "Europe"
names(dirty_life_expectancy)[1:3]=c("Country","Life Expectancy","Region") # code renames the columns appropriately
str(dirty_life_expectancy) # code checks the data types again to make sure we have what we want
## tibble [227 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country        : chr [1:227] "Monaco" "Singapore" "Macau" "Japan" ...
##  $ Life Expectancy: num [1:227] 89.8 86.7 85.3 85.2 84.2 84.2 84 84 83.9 83.8 ...
##  $ Region         : chr [1:227] "Europe" "East and Southeast Asia" "East and Southeast Asia" "East and Southeast Asia" ...

Checking and Cleaning characters within the columns

dirty_life_expectancy$Country[grepl("[^a-zA-Z]", dirty_life_expectancy$Country)] # Code requests any cells in the Country column that aren't only upper and lower case letters. Helps to notice that we have spaces, commas, apostrophes, dashes, and parenthases. However, countries with spaces are fine so let's try something else. 
##  [1] "San Marino"                                   
##  [2] "Hong Kong"                                    
##  [3] "Korea, South"                                 
##  [4] "New Zealand"                                  
##  [5] "Cayman Islands"                               
##  [6] "Isle of Man"                                  
##  [7] "United Kingdom"                               
##  [8] "Puerto Rico"                                  
##  [9] "Saint Pierre and Miquelon"                    
## [10] "Faroe Islands"                                
## [11] "Turks and Caicos Islands"                     
## [12] "Wallis and Futuna"                            
## [13] "Saint Martin"                                 
## [14] "Saint Barthelemy"                             
## [15] "United States"                                
## [16] "Saint Helena, Ascension, and Tristan da Cunha"
## [17] "Virgin Islands"                               
## [18] "Costa Rica"                                   
## [19] "British Virgin Islands"                       
## [20] "United Arab Emirates"                         
## [21] "Sint Maarten"                                 
## [22] "Saint Lucia"                                  
## [23] "New Caledonia"                                
## [24] "French Polynesia"                             
## [25] "Bosnia and Herzegovina"                       
## [26] "Antigua and Barbuda"                          
## [27] "Cook Islands"                                 
## [28] "Saint Kitts and Nevis"                        
## [29] "North Macedonia"                              
## [30] "Solomon Islands"                              
## [31] "Saint Vincent and the Grenadines"             
## [32] "Saudi Arabia"                                 
## [33] "Northern Mariana Islands"                     
## [34] "Sri Lanka"                                    
## [35] "Turkey (Turkiye)"                             
## [36] "Bahamas, The"                                 
## [37] "Trinidad and Tobago"                          
## [38] "West Bank"                                    
## [39] "El Salvador"                                  
## [40] "American Samoa"                               
## [41] "Gaza Strip"                                   
## [42] "Marshall Islands"                             
## [43] "Micronesia, Federated States of"              
## [44] "Cabo Verde"                                   
## [45] "Korea, North"                                 
## [46] "Congo, Republic of the"                       
## [47] "Dominican Republic"                           
## [48] "South Africa"                                 
## [49] "Timor-Leste"                                  
## [50] "Papua New Guinea"                             
## [51] "Gambia, The"                                  
## [52] "Sao Tome and Principe"                        
## [53] "Guinea-Bissau"                                
## [54] "Burkina Faso"                                 
## [55] "Equatorial Guinea"                            
## [56] "Cote d'Ivoire"                                
## [57] "Congo, Democratic Republic of the"            
## [58] "South Sudan"                                  
## [59] "Sierra Leone"                                 
## [60] "Central African Republic"
dirty_life_expectancy$Country[grepl("[^a-zA-Z\\s]", dirty_life_expectancy$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces
##  [1] "Korea, South"                                 
##  [2] "Saint Helena, Ascension, and Tristan da Cunha"
##  [3] "Turkey (Turkiye)"                             
##  [4] "Bahamas, The"                                 
##  [5] "Micronesia, Federated States of"              
##  [6] "Korea, North"                                 
##  [7] "Congo, Republic of the"                       
##  [8] "Timor-Leste"                                  
##  [9] "Gambia, The"                                  
## [10] "Guinea-Bissau"                                
## [11] "Cote d'Ivoire"                                
## [12] "Congo, Democratic Republic of the"
gsub('-',' ',dirty_life_expectancy$Country,fixed = T) # code requests all dashes be replaced with spaces
##   [1] "Monaco"                                       
##   [2] "Singapore"                                    
##   [3] "Macau"                                        
##   [4] "Japan"                                        
##   [5] "Canada"                                       
##   [6] "San Marino"                                   
##   [7] "Hong Kong"                                    
##   [8] "Iceland"                                      
##   [9] "Switzerland"                                  
##  [10] "Andorra"                                      
##  [11] "Guernsey"                                     
##  [12] "Malta"                                        
##  [13] "Australia"                                    
##  [14] "Luxembourg"                                   
##  [15] "Korea, South"                                 
##  [16] "Israel"                                       
##  [17] "Jersey"                                       
##  [18] "Spain"                                        
##  [19] "Italy"                                        
##  [20] "Liechtenstein"                                
##  [21] "Sweden"                                       
##  [22] "Norway"                                       
##  [23] "New Zealand"                                  
##  [24] "Austria"                                      
##  [25] "France"                                       
##  [26] "Anguilla"                                     
##  [27] "Cayman Islands"                               
##  [28] "Isle of Man"                                  
##  [29] "Bermuda"                                      
##  [30] "Belgium"                                      
##  [31] "Finland"                                      
##  [32] "Slovenia"                                     
##  [33] "United Kingdom"                               
##  [34] "Puerto Rico"                                  
##  [35] "Denmark"                                      
##  [36] "Ireland"                                      
##  [37] "Portugal"                                     
##  [38] "Germany"                                      
##  [39] "Netherlands"                                  
##  [40] "Greece"                                       
##  [41] "Saint Pierre and Miquelon"                    
##  [42] "Faroe Islands"                                
##  [43] "Taiwan"                                       
##  [44] "Turks and Caicos Islands"                     
##  [45] "Wallis and Futuna"                            
##  [46] "Saint Martin"                                 
##  [47] "Saint Barthelemy"                             
##  [48] "Gibraltar"                                    
##  [49] "United States"                                
##  [50] "Saint Helena, Ascension, and Tristan da Cunha"
##  [51] "Virgin Islands"                               
##  [52] "Bahrain"                                      
##  [53] "Qatar"                                        
##  [54] "Costa Rica"                                   
##  [55] "Chile"                                        
##  [56] "Cyprus"                                       
##  [57] "British Virgin Islands"                       
##  [58] "Cuba"                                         
##  [59] "Curacao"                                      
##  [60] "United Arab Emirates"                         
##  [61] "Albania"                                      
##  [62] "Sint Maarten"                                 
##  [63] "Kuwait"                                       
##  [64] "Saint Lucia"                                  
##  [65] "New Caledonia"                                
##  [66] "Panama"                                       
##  [67] "Lebanon"                                      
##  [68] "Barbados"                                     
##  [69] "French Polynesia"                             
##  [70] "Uruguay"                                      
##  [71] "Brunei"                                       
##  [72] "Argentina"                                    
##  [73] "Paraguay"                                     
##  [74] "Dominica"                                     
##  [75] "China"                                        
##  [76] "Czechia"                                      
##  [77] "Bosnia and Herzegovina"                       
##  [78] "Aruba"                                        
##  [79] "Estonia"                                      
##  [80] "Antigua and Barbuda"                          
##  [81] "Thailand"                                     
##  [82] "Montenegro"                                   
##  [83] "Guam"                                         
##  [84] "Tonga"                                        
##  [85] "Algeria"                                      
##  [86] "Croatia"                                      
##  [87] "Libya"                                        
##  [88] "Cook Islands"                                 
##  [89] "Saint Kitts and Nevis"                        
##  [90] "Maldives"                                     
##  [91] "Oman"                                         
##  [92] "North Macedonia"                              
##  [93] "Tunisia"                                      
##  [94] "Slovakia"                                     
##  [95] "Solomon Islands"                              
##  [96] "Saint Vincent and the Grenadines"             
##  [97] "Saudi Arabia"                                 
##  [98] "Northern Mariana Islands"                     
##  [99] "Romania"                                      
## [100] "Sri Lanka"                                    
## [101] "Poland"                                       
## [102] "Armenia"                                      
## [103] "Turkey (Turkiye)"                             
## [104] "Bahamas, The"                                 
## [105] "Seychelles"                                   
## [106] "Malaysia"                                     
## [107] "Trinidad and Tobago"                          
## [108] "West Bank"                                    
## [109] "Jordan"                                       
## [110] "Latvia"                                       
## [111] "Jamaica"                                      
## [112] "Grenada"                                      
## [113] "Brazil"                                       
## [114] "Uzbekistan"                                   
## [115] "Vietnam"                                      
## [116] "Montserrat"                                   
## [117] "Lithuania"                                    
## [118] "Bulgaria"                                     
## [119] "Hungary"                                      
## [120] "Azerbaijan"                                   
## [121] "El Salvador"                                  
## [122] "American Samoa"                               
## [123] "Vanuatu"                                      
## [124] "Samoa"                                        
## [125] "Iran"                                         
## [126] "Gaza Strip"                                   
## [127] "Mauritius"                                    
## [128] "Serbia"                                       
## [129] "Bangladesh"                                   
## [130] "Marshall Islands"                             
## [131] "Palau"                                        
## [132] "Egypt"                                        
## [133] "Micronesia, Federated States of"              
## [134] "Colombia"                                     
## [135] "Ecuador"                                      
## [136] "Fiji"                                         
## [137] "Syria"                                        
## [138] "Belarus"                                      
## [139] "Nicaragua"                                    
## [140] "Mexico"                                       
## [141] "Greenland"                                    
## [142] "Venezuela"                                    
## [143] "Belize"                                       
## [144] "Cabo Verde"                                   
## [145] "Morocco"                                      
## [146] "Iraq"                                         
## [147] "Bhutan"                                       
## [148] "Indonesia"                                    
## [149] "Guatemala"                                    
## [150] "Korea, North"                                 
## [151] "Kazakhstan"                                   
## [152] "Honduras"                                     
## [153] "Kosovo"                                       
## [154] "Nepal"                                        
## [155] "Malawi"                                       
## [156] "Congo, Republic of the"                       
## [157] "Kyrgyzstan"                                   
## [158] "Georgia"                                      
## [159] "Suriname"                                     
## [160] "Dominican Republic"                           
## [161] "Bolivia"                                      
## [162] "Turkmenistan"                                 
## [163] "Guyana"                                       
## [164] "Russia"                                       
## [165] "Togo"                                         
## [166] "South Africa"                                 
## [167] "Tajikistan"                                   
## [168] "Mongolia"                                     
## [169] "Cambodia"                                     
## [170] "Philippines"                                  
## [171] "Tanzania"                                     
## [172] "Senegal"                                      
## [173] "Ukraine"                                      
## [174] "Timor Leste"                                  
## [175] "Gabon"                                        
## [176] "Kenya"                                        
## [177] "Burma"                                        
## [178] "Pakistan"                                     
## [179] "Moldova"                                      
## [180] "Ghana"                                        
## [181] "Papua New Guinea"                             
## [182] "Uganda"                                       
## [183] "Laos"                                         
## [184] "Tuvalu"                                       
## [185] "Peru"                                         
## [186] "Madagascar"                                   
## [187] "Nauru"                                        
## [188] "Kiribati"                                     
## [189] "Gambia, The"                                  
## [190] "India"                                        
## [191] "Yemen"                                        
## [192] "Burundi"                                      
## [193] "Comoros"                                      
## [194] "Sudan"                                        
## [195] "Sao Tome and Principe"                        
## [196] "Ethiopia"                                     
## [197] "Eritrea"                                      
## [198] "Zimbabwe"                                     
## [199] "Zambia"                                       
## [200] "Rwanda"                                       
## [201] "Botswana"                                     
## [202] "Djibouti"                                     
## [203] "Namibia"                                      
## [204] "Mauritania"                                   
## [205] "Haiti"                                        
## [206] "Guinea"                                       
## [207] "Guinea Bissau"                                
## [208] "Burkina Faso"                                 
## [209] "Cameroon"                                     
## [210] "Equatorial Guinea"                            
## [211] "Mali"                                         
## [212] "Cote d'Ivoire"                                
## [213] "Benin"                                        
## [214] "Angola"                                       
## [215] "Congo, Democratic Republic of the"            
## [216] "Nigeria"                                      
## [217] "Liberia"                                      
## [218] "Niger"                                        
## [219] "Eswatini"                                     
## [220] "South Sudan"                                  
## [221] "Lesotho"                                      
## [222] "Chad"                                         
## [223] "Sierra Leone"                                 
## [224] "Mozambique"                                   
## [225] "Somalia"                                      
## [226] "Central African Republic"                     
## [227] "Afghanistan"
dirty_life_expectancy$Country=gsub('-',' ',dirty_life_expectancy$Country,fixed = T) # code asks the data frame to update
dirty_life_expectancy$Country[grepl("[^a-zA-Z\\s]", dirty_life_expectancy$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces
##  [1] "Korea, South"                                 
##  [2] "Saint Helena, Ascension, and Tristan da Cunha"
##  [3] "Turkey (Turkiye)"                             
##  [4] "Bahamas, The"                                 
##  [5] "Micronesia, Federated States of"              
##  [6] "Korea, North"                                 
##  [7] "Congo, Republic of the"                       
##  [8] "Gambia, The"                                  
##  [9] "Cote d'Ivoire"                                
## [10] "Congo, Democratic Republic of the"
gsub(',','',dirty_life_expectancy$Country,fixed = T) # code requests any commas be removed
##   [1] "Monaco"                                     
##   [2] "Singapore"                                  
##   [3] "Macau"                                      
##   [4] "Japan"                                      
##   [5] "Canada"                                     
##   [6] "San Marino"                                 
##   [7] "Hong Kong"                                  
##   [8] "Iceland"                                    
##   [9] "Switzerland"                                
##  [10] "Andorra"                                    
##  [11] "Guernsey"                                   
##  [12] "Malta"                                      
##  [13] "Australia"                                  
##  [14] "Luxembourg"                                 
##  [15] "Korea South"                                
##  [16] "Israel"                                     
##  [17] "Jersey"                                     
##  [18] "Spain"                                      
##  [19] "Italy"                                      
##  [20] "Liechtenstein"                              
##  [21] "Sweden"                                     
##  [22] "Norway"                                     
##  [23] "New Zealand"                                
##  [24] "Austria"                                    
##  [25] "France"                                     
##  [26] "Anguilla"                                   
##  [27] "Cayman Islands"                             
##  [28] "Isle of Man"                                
##  [29] "Bermuda"                                    
##  [30] "Belgium"                                    
##  [31] "Finland"                                    
##  [32] "Slovenia"                                   
##  [33] "United Kingdom"                             
##  [34] "Puerto Rico"                                
##  [35] "Denmark"                                    
##  [36] "Ireland"                                    
##  [37] "Portugal"                                   
##  [38] "Germany"                                    
##  [39] "Netherlands"                                
##  [40] "Greece"                                     
##  [41] "Saint Pierre and Miquelon"                  
##  [42] "Faroe Islands"                              
##  [43] "Taiwan"                                     
##  [44] "Turks and Caicos Islands"                   
##  [45] "Wallis and Futuna"                          
##  [46] "Saint Martin"                               
##  [47] "Saint Barthelemy"                           
##  [48] "Gibraltar"                                  
##  [49] "United States"                              
##  [50] "Saint Helena Ascension and Tristan da Cunha"
##  [51] "Virgin Islands"                             
##  [52] "Bahrain"                                    
##  [53] "Qatar"                                      
##  [54] "Costa Rica"                                 
##  [55] "Chile"                                      
##  [56] "Cyprus"                                     
##  [57] "British Virgin Islands"                     
##  [58] "Cuba"                                       
##  [59] "Curacao"                                    
##  [60] "United Arab Emirates"                       
##  [61] "Albania"                                    
##  [62] "Sint Maarten"                               
##  [63] "Kuwait"                                     
##  [64] "Saint Lucia"                                
##  [65] "New Caledonia"                              
##  [66] "Panama"                                     
##  [67] "Lebanon"                                    
##  [68] "Barbados"                                   
##  [69] "French Polynesia"                           
##  [70] "Uruguay"                                    
##  [71] "Brunei"                                     
##  [72] "Argentina"                                  
##  [73] "Paraguay"                                   
##  [74] "Dominica"                                   
##  [75] "China"                                      
##  [76] "Czechia"                                    
##  [77] "Bosnia and Herzegovina"                     
##  [78] "Aruba"                                      
##  [79] "Estonia"                                    
##  [80] "Antigua and Barbuda"                        
##  [81] "Thailand"                                   
##  [82] "Montenegro"                                 
##  [83] "Guam"                                       
##  [84] "Tonga"                                      
##  [85] "Algeria"                                    
##  [86] "Croatia"                                    
##  [87] "Libya"                                      
##  [88] "Cook Islands"                               
##  [89] "Saint Kitts and Nevis"                      
##  [90] "Maldives"                                   
##  [91] "Oman"                                       
##  [92] "North Macedonia"                            
##  [93] "Tunisia"                                    
##  [94] "Slovakia"                                   
##  [95] "Solomon Islands"                            
##  [96] "Saint Vincent and the Grenadines"           
##  [97] "Saudi Arabia"                               
##  [98] "Northern Mariana Islands"                   
##  [99] "Romania"                                    
## [100] "Sri Lanka"                                  
## [101] "Poland"                                     
## [102] "Armenia"                                    
## [103] "Turkey (Turkiye)"                           
## [104] "Bahamas The"                                
## [105] "Seychelles"                                 
## [106] "Malaysia"                                   
## [107] "Trinidad and Tobago"                        
## [108] "West Bank"                                  
## [109] "Jordan"                                     
## [110] "Latvia"                                     
## [111] "Jamaica"                                    
## [112] "Grenada"                                    
## [113] "Brazil"                                     
## [114] "Uzbekistan"                                 
## [115] "Vietnam"                                    
## [116] "Montserrat"                                 
## [117] "Lithuania"                                  
## [118] "Bulgaria"                                   
## [119] "Hungary"                                    
## [120] "Azerbaijan"                                 
## [121] "El Salvador"                                
## [122] "American Samoa"                             
## [123] "Vanuatu"                                    
## [124] "Samoa"                                      
## [125] "Iran"                                       
## [126] "Gaza Strip"                                 
## [127] "Mauritius"                                  
## [128] "Serbia"                                     
## [129] "Bangladesh"                                 
## [130] "Marshall Islands"                           
## [131] "Palau"                                      
## [132] "Egypt"                                      
## [133] "Micronesia Federated States of"             
## [134] "Colombia"                                   
## [135] "Ecuador"                                    
## [136] "Fiji"                                       
## [137] "Syria"                                      
## [138] "Belarus"                                    
## [139] "Nicaragua"                                  
## [140] "Mexico"                                     
## [141] "Greenland"                                  
## [142] "Venezuela"                                  
## [143] "Belize"                                     
## [144] "Cabo Verde"                                 
## [145] "Morocco"                                    
## [146] "Iraq"                                       
## [147] "Bhutan"                                     
## [148] "Indonesia"                                  
## [149] "Guatemala"                                  
## [150] "Korea North"                                
## [151] "Kazakhstan"                                 
## [152] "Honduras"                                   
## [153] "Kosovo"                                     
## [154] "Nepal"                                      
## [155] "Malawi"                                     
## [156] "Congo Republic of the"                      
## [157] "Kyrgyzstan"                                 
## [158] "Georgia"                                    
## [159] "Suriname"                                   
## [160] "Dominican Republic"                         
## [161] "Bolivia"                                    
## [162] "Turkmenistan"                               
## [163] "Guyana"                                     
## [164] "Russia"                                     
## [165] "Togo"                                       
## [166] "South Africa"                               
## [167] "Tajikistan"                                 
## [168] "Mongolia"                                   
## [169] "Cambodia"                                   
## [170] "Philippines"                                
## [171] "Tanzania"                                   
## [172] "Senegal"                                    
## [173] "Ukraine"                                    
## [174] "Timor Leste"                                
## [175] "Gabon"                                      
## [176] "Kenya"                                      
## [177] "Burma"                                      
## [178] "Pakistan"                                   
## [179] "Moldova"                                    
## [180] "Ghana"                                      
## [181] "Papua New Guinea"                           
## [182] "Uganda"                                     
## [183] "Laos"                                       
## [184] "Tuvalu"                                     
## [185] "Peru"                                       
## [186] "Madagascar"                                 
## [187] "Nauru"                                      
## [188] "Kiribati"                                   
## [189] "Gambia The"                                 
## [190] "India"                                      
## [191] "Yemen"                                      
## [192] "Burundi"                                    
## [193] "Comoros"                                    
## [194] "Sudan"                                      
## [195] "Sao Tome and Principe"                      
## [196] "Ethiopia"                                   
## [197] "Eritrea"                                    
## [198] "Zimbabwe"                                   
## [199] "Zambia"                                     
## [200] "Rwanda"                                     
## [201] "Botswana"                                   
## [202] "Djibouti"                                   
## [203] "Namibia"                                    
## [204] "Mauritania"                                 
## [205] "Haiti"                                      
## [206] "Guinea"                                     
## [207] "Guinea Bissau"                              
## [208] "Burkina Faso"                               
## [209] "Cameroon"                                   
## [210] "Equatorial Guinea"                          
## [211] "Mali"                                       
## [212] "Cote d'Ivoire"                              
## [213] "Benin"                                      
## [214] "Angola"                                     
## [215] "Congo Democratic Republic of the"           
## [216] "Nigeria"                                    
## [217] "Liberia"                                    
## [218] "Niger"                                      
## [219] "Eswatini"                                   
## [220] "South Sudan"                                
## [221] "Lesotho"                                    
## [222] "Chad"                                       
## [223] "Sierra Leone"                               
## [224] "Mozambique"                                 
## [225] "Somalia"                                    
## [226] "Central African Republic"                   
## [227] "Afghanistan"
dirty_life_expectancy$Country=gsub(',','',dirty_life_expectancy$Country,fixed = T) # code asks the data frame to update
dirty_life_expectancy$Country[grepl("[^a-zA-Z\\s]", dirty_life_expectancy$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces. Only two names left that are an issue
## [1] "Turkey (Turkiye)" "Cote d'Ivoire"
gsub("\\(.*\\)",'',dirty_life_expectancy$Country,perl = T) # code asks anything inside of parentheses to be removed to get rid of (Turkiye)
##   [1] "Monaco"                                     
##   [2] "Singapore"                                  
##   [3] "Macau"                                      
##   [4] "Japan"                                      
##   [5] "Canada"                                     
##   [6] "San Marino"                                 
##   [7] "Hong Kong"                                  
##   [8] "Iceland"                                    
##   [9] "Switzerland"                                
##  [10] "Andorra"                                    
##  [11] "Guernsey"                                   
##  [12] "Malta"                                      
##  [13] "Australia"                                  
##  [14] "Luxembourg"                                 
##  [15] "Korea South"                                
##  [16] "Israel"                                     
##  [17] "Jersey"                                     
##  [18] "Spain"                                      
##  [19] "Italy"                                      
##  [20] "Liechtenstein"                              
##  [21] "Sweden"                                     
##  [22] "Norway"                                     
##  [23] "New Zealand"                                
##  [24] "Austria"                                    
##  [25] "France"                                     
##  [26] "Anguilla"                                   
##  [27] "Cayman Islands"                             
##  [28] "Isle of Man"                                
##  [29] "Bermuda"                                    
##  [30] "Belgium"                                    
##  [31] "Finland"                                    
##  [32] "Slovenia"                                   
##  [33] "United Kingdom"                             
##  [34] "Puerto Rico"                                
##  [35] "Denmark"                                    
##  [36] "Ireland"                                    
##  [37] "Portugal"                                   
##  [38] "Germany"                                    
##  [39] "Netherlands"                                
##  [40] "Greece"                                     
##  [41] "Saint Pierre and Miquelon"                  
##  [42] "Faroe Islands"                              
##  [43] "Taiwan"                                     
##  [44] "Turks and Caicos Islands"                   
##  [45] "Wallis and Futuna"                          
##  [46] "Saint Martin"                               
##  [47] "Saint Barthelemy"                           
##  [48] "Gibraltar"                                  
##  [49] "United States"                              
##  [50] "Saint Helena Ascension and Tristan da Cunha"
##  [51] "Virgin Islands"                             
##  [52] "Bahrain"                                    
##  [53] "Qatar"                                      
##  [54] "Costa Rica"                                 
##  [55] "Chile"                                      
##  [56] "Cyprus"                                     
##  [57] "British Virgin Islands"                     
##  [58] "Cuba"                                       
##  [59] "Curacao"                                    
##  [60] "United Arab Emirates"                       
##  [61] "Albania"                                    
##  [62] "Sint Maarten"                               
##  [63] "Kuwait"                                     
##  [64] "Saint Lucia"                                
##  [65] "New Caledonia"                              
##  [66] "Panama"                                     
##  [67] "Lebanon"                                    
##  [68] "Barbados"                                   
##  [69] "French Polynesia"                           
##  [70] "Uruguay"                                    
##  [71] "Brunei"                                     
##  [72] "Argentina"                                  
##  [73] "Paraguay"                                   
##  [74] "Dominica"                                   
##  [75] "China"                                      
##  [76] "Czechia"                                    
##  [77] "Bosnia and Herzegovina"                     
##  [78] "Aruba"                                      
##  [79] "Estonia"                                    
##  [80] "Antigua and Barbuda"                        
##  [81] "Thailand"                                   
##  [82] "Montenegro"                                 
##  [83] "Guam"                                       
##  [84] "Tonga"                                      
##  [85] "Algeria"                                    
##  [86] "Croatia"                                    
##  [87] "Libya"                                      
##  [88] "Cook Islands"                               
##  [89] "Saint Kitts and Nevis"                      
##  [90] "Maldives"                                   
##  [91] "Oman"                                       
##  [92] "North Macedonia"                            
##  [93] "Tunisia"                                    
##  [94] "Slovakia"                                   
##  [95] "Solomon Islands"                            
##  [96] "Saint Vincent and the Grenadines"           
##  [97] "Saudi Arabia"                               
##  [98] "Northern Mariana Islands"                   
##  [99] "Romania"                                    
## [100] "Sri Lanka"                                  
## [101] "Poland"                                     
## [102] "Armenia"                                    
## [103] "Turkey "                                    
## [104] "Bahamas The"                                
## [105] "Seychelles"                                 
## [106] "Malaysia"                                   
## [107] "Trinidad and Tobago"                        
## [108] "West Bank"                                  
## [109] "Jordan"                                     
## [110] "Latvia"                                     
## [111] "Jamaica"                                    
## [112] "Grenada"                                    
## [113] "Brazil"                                     
## [114] "Uzbekistan"                                 
## [115] "Vietnam"                                    
## [116] "Montserrat"                                 
## [117] "Lithuania"                                  
## [118] "Bulgaria"                                   
## [119] "Hungary"                                    
## [120] "Azerbaijan"                                 
## [121] "El Salvador"                                
## [122] "American Samoa"                             
## [123] "Vanuatu"                                    
## [124] "Samoa"                                      
## [125] "Iran"                                       
## [126] "Gaza Strip"                                 
## [127] "Mauritius"                                  
## [128] "Serbia"                                     
## [129] "Bangladesh"                                 
## [130] "Marshall Islands"                           
## [131] "Palau"                                      
## [132] "Egypt"                                      
## [133] "Micronesia Federated States of"             
## [134] "Colombia"                                   
## [135] "Ecuador"                                    
## [136] "Fiji"                                       
## [137] "Syria"                                      
## [138] "Belarus"                                    
## [139] "Nicaragua"                                  
## [140] "Mexico"                                     
## [141] "Greenland"                                  
## [142] "Venezuela"                                  
## [143] "Belize"                                     
## [144] "Cabo Verde"                                 
## [145] "Morocco"                                    
## [146] "Iraq"                                       
## [147] "Bhutan"                                     
## [148] "Indonesia"                                  
## [149] "Guatemala"                                  
## [150] "Korea North"                                
## [151] "Kazakhstan"                                 
## [152] "Honduras"                                   
## [153] "Kosovo"                                     
## [154] "Nepal"                                      
## [155] "Malawi"                                     
## [156] "Congo Republic of the"                      
## [157] "Kyrgyzstan"                                 
## [158] "Georgia"                                    
## [159] "Suriname"                                   
## [160] "Dominican Republic"                         
## [161] "Bolivia"                                    
## [162] "Turkmenistan"                               
## [163] "Guyana"                                     
## [164] "Russia"                                     
## [165] "Togo"                                       
## [166] "South Africa"                               
## [167] "Tajikistan"                                 
## [168] "Mongolia"                                   
## [169] "Cambodia"                                   
## [170] "Philippines"                                
## [171] "Tanzania"                                   
## [172] "Senegal"                                    
## [173] "Ukraine"                                    
## [174] "Timor Leste"                                
## [175] "Gabon"                                      
## [176] "Kenya"                                      
## [177] "Burma"                                      
## [178] "Pakistan"                                   
## [179] "Moldova"                                    
## [180] "Ghana"                                      
## [181] "Papua New Guinea"                           
## [182] "Uganda"                                     
## [183] "Laos"                                       
## [184] "Tuvalu"                                     
## [185] "Peru"                                       
## [186] "Madagascar"                                 
## [187] "Nauru"                                      
## [188] "Kiribati"                                   
## [189] "Gambia The"                                 
## [190] "India"                                      
## [191] "Yemen"                                      
## [192] "Burundi"                                    
## [193] "Comoros"                                    
## [194] "Sudan"                                      
## [195] "Sao Tome and Principe"                      
## [196] "Ethiopia"                                   
## [197] "Eritrea"                                    
## [198] "Zimbabwe"                                   
## [199] "Zambia"                                     
## [200] "Rwanda"                                     
## [201] "Botswana"                                   
## [202] "Djibouti"                                   
## [203] "Namibia"                                    
## [204] "Mauritania"                                 
## [205] "Haiti"                                      
## [206] "Guinea"                                     
## [207] "Guinea Bissau"                              
## [208] "Burkina Faso"                               
## [209] "Cameroon"                                   
## [210] "Equatorial Guinea"                          
## [211] "Mali"                                       
## [212] "Cote d'Ivoire"                              
## [213] "Benin"                                      
## [214] "Angola"                                     
## [215] "Congo Democratic Republic of the"           
## [216] "Nigeria"                                    
## [217] "Liberia"                                    
## [218] "Niger"                                      
## [219] "Eswatini"                                   
## [220] "South Sudan"                                
## [221] "Lesotho"                                    
## [222] "Chad"                                       
## [223] "Sierra Leone"                               
## [224] "Mozambique"                                 
## [225] "Somalia"                                    
## [226] "Central African Republic"                   
## [227] "Afghanistan"
dirty_life_expectancy$Country=gsub("\\(.*\\)",'',dirty_life_expectancy$Country,perl = T) # code asks the data frame to update
dirty_life_expectancy$Country[grepl("[^a-zA-Z\\s]", dirty_life_expectancy$Country,perl = T)] # code requests any cell values in the country column that are not upper or lower case letters or spaces. Only one names left that are an issue but we are going to leave it be
## [1] "Cote d'Ivoire"
dirty_life_expectancy$Country == trimws(dirty_life_expectancy$Country) # code checks to see if there are any leading or trailing spaces
##   [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [25]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [37]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [49]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [61]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [73]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [85]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [97]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
## [109]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [121]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [133]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [145]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [157]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [169]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [181]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [193]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [205]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [217]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
dirty_life_expectancy$Country <- trimws(dirty_life_expectancy$Country) # code requests to remove any leading or trailing spaces
dirty_life_expectancy$Country == trimws(dirty_life_expectancy$Country) # code checks to see if there are any leading or trailing spaces and everything is good
##   [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [76] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [91] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [106] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [121] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [136] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [151] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [166] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [181] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [196] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [211] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [226] TRUE TRUE
str(dirty_life_expectancy) # Code is checking the columns, the mortality rate column is recognized as numerical so that is okay. 
## tibble [227 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country        : chr [1:227] "Monaco" "Singapore" "Macau" "Japan" ...
##  $ Life Expectancy: num [1:227] 89.8 86.7 85.3 85.2 84.2 84.2 84 84 83.9 83.8 ...
##  $ Region         : chr [1:227] "Europe" "East and Southeast Asia" "East and Southeast Asia" "East and Southeast Asia" ...
dirty_life_expectancy$Region == trimws(dirty_life_expectancy$Region) # code checks to see if there are any leading or trailing spaces in the region column and there are not
##   [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [76] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##  [91] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [106] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [121] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [136] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [151] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [166] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [181] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [196] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [211] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [226] TRUE TRUE

The data has been cleaned. There are no missing values or characters that may be an issue. Let’s move on to formatting.

Check numeric data

summary(dirty_life_expectancy$`Life Expectancy`) # code is checking the statistical summary of the mortality rate column - we know that it is numerical since it gives us an analysis
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   54.40   70.80   76.20   75.06   80.10   89.80
str(dirty_life_expectancy) # code is confirming that it is numerical again
## tibble [227 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country        : chr [1:227] "Monaco" "Singapore" "Macau" "Japan" ...
##  $ Life Expectancy: num [1:227] 89.8 86.7 85.3 85.2 84.2 84.2 84 84 83.9 83.8 ...
##  $ Region         : chr [1:227] "Europe" "East and Southeast Asia" "East and Southeast Asia" "East and Southeast Asia" ...

Check text format

dirty_life_expectancy$Country=toupper(dirty_life_expectancy$Country) # code changes all the values in the country column to uppercase
dirty_life_expectancy$Region=toupper(dirty_life_expectancy$Region) # code changes all the values in the region column to upper case

The dirty_mortality file has been cleaned and formatted.

Saving Files

saveRDS(dirty_life_expectancy,"Life_Expectancy_Formatted.RDS") # code is saving the file as an RDS file
Life_Expectancy_FormattedRDS=readRDS("Life_Expectancy_Formatted.RDS") # code is reading the RDS file
str(Life_Expectancy_FormattedRDS) # code is checking the data types of the RDS file
## tibble [227 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Country        : chr [1:227] "MONACO" "SINGAPORE" "MACAU" "JAPAN" ...
##  $ Life Expectancy: num [1:227] 89.8 86.7 85.3 85.2 84.2 84.2 84 84 83.9 83.8 ...
##  $ Region         : chr [1:227] "EUROPE" "EAST AND SOUTHEAST ASIA" "EAST AND SOUTHEAST ASIA" "EAST AND SOUTHEAST ASIA" ...
write.csv(dirty_life_expectancy,"Life_Expectancy_Formatted.csv", row.names = FALSE) # code is saving the data frame as a csv file
Life_Expectancy_FormattedCSV=read.csv("Life_Expectancy_Formatted.csv") # code is reading the csv file
str(Life_Expectancy_FormattedCSV) # code is checking the data types of the csv file
## 'data.frame':    227 obs. of  3 variables:
##  $ Country        : chr  "MONACO" "SINGAPORE" "MACAU" "JAPAN" ...
##  $ Life.Expectancy: num  89.8 86.7 85.3 85.2 84.2 84.2 84 84 83.9 83.8 ...
##  $ Region         : chr  "EUROPE" "EAST AND SOUTHEAST ASIA" "EAST AND SOUTHEAST ASIA" "EAST AND SOUTHEAST ASIA" ...