Amoung the 266 cities listed as the most creative, an astounding 34 of them have names that begin with the letter B. Clearly this is the best letter to begin a city’s name with, and so therefore they are the best cities in the world. We should look at these cities closer to see which of them is the Breme de la Breme. Lets get started.

Packages and importing list of city names (not very interesting):

Packages

library(dplyr)
library(readr)
library(leaflet)
library(httr)
library(jsonlite)
library(anytime)
library(stringr)
library(tidyr)
cityclean <- function(city){
  city = str_remove_all(city, ",")
  city = str_replace_all(city, " ", "-")
  city = str_to_lower(city)
  return(city)
}

bcity <- read_delim("cities.txt", ";", escape_double = FALSE, trim_ws = TRUE)

bcity <- bcity %>%
  mutate(citget = cityclean(city))


Writing function to get both the location in latitiude and longitude of any one city as well as economic data.

city_get <- function(city){
  loc_str <- paste("https://api.teleport.org/api/urban_areas/slug:", city, "/", sep = "") #getting location data
  loc_json <- GET(loc_str)
  loc_data <- fromJSON(rawToChar(loc_json$content))
  
  city_str <- paste("https://api.teleport.org/api/urban_areas/slug:", city, "/scores/", sep = "") #getting econ data
  city_json = GET(city_str)  
  citydat <- fromJSON(rawToChar(city_json$content)) 
  citydat <- citydat$categories %>% #econ data is in long format and I want it *thicccc*
    select(name, score_out_of_10) %>%
    spread(name, score_out_of_10)
  
  citydat <- citydat %>% #Puttin gtogether location and econ data
    mutate(lat = loc_data$bounding_box$latlon$east,
           lon = loc_data$bounding_box$latlon$north)
  return(citydat) 
}

Iterating through every one of my B cities and storing their data in one data frame.

city_econ_list <- list()
city_econ <- data.frame()
for (i in 1:nrow(bcity)){
  city_econ_list[[i]] <-  bind_rows(city_econ, city_get(bcity$citget[i]))
}
city_econ <- bind_rows(city_econ_list)
bcity <- bind_cols(bcity, city_econ)
head(bcity)

The most common letter that these statistics begin was tied between t and e, and so they must be the most important. So let us come up with an arbitrary statistic that is the square of them sum of these six measures: Economy, Education, Environmental Quality, Travel Connectivity, Tolerance, and Taxation divided by 18. This formula is carefully crafted to suss out exemplary cities from the mere pretenders.

bcity <- bcity %>%
  mutate(tes = (1.2* `Travel Connectivity` + `Tolerance` + `Taxation` + `Economy` + `Education` + `Environmental Quality`)^1.5 / 18)

citymap <- leaflet(bcity) %>%
  addTiles() %>%
  addCircleMarkers(
    lng = bcity$lat,
    lat = bcity$lon,
    radius = ~ tes,
    stroke = F, fillOpacity = 0.5,
    label = bcity$city,
    popup = paste(bcity$city, " T Scores:", "<br>",  
      "Taxation score: ", bcity$Taxation, "<br>",
      "Tolerance score: ", bcity$Tolerance, "<br>",
      "Travel score: ", bcity$`Travel Connectivity`, "<br>",
      "Sum: ", bcity$tes, sep = ""),
    color = "Red"
 )
  
citymap
bcity %>%
  select(city, tes) %>%
  arrange(desc(tes))

Wow. Who could have guessed that the best place to live in the world is the small coastal town of Brighton, East Sussex, UK. Hope you like wet weather.