Q1. Load the maps package into memory.

library(maps)


Q2. The maps package comes with a dataset called world.cities. What kind of object is ‘world.cities’?

class(world.cities)
## [1] "data.frame"


Q3. How many rows and columns does world.cities have?

dim(world.cities)
## [1] 43645     6

Or:

nrow(world.cities)
## [1] 43645
ncol(world.cities)
## [1] 6


Q4. What is the original data source of world.cities? Hint: datasets have help pages too!

?world.cities

“The data were originally obtained from Stefan Helders’ website (http://www.world-gazetteer.com), which now redirects to http://www.populationmondiale.com.”


Q5. What is the average population of the world cities?

mean(world.cities$pop)
## [1] 57822.31


Q6. How many world cities have a population larger than 1,000,000?

sum(world.cities$pop >= 1000000)
## [1] 313


Q7. The maps package also has a dataset called us.cities, which includes columns for longitude and latitude.

Make a scatter plot of the longitude and latitude ## coordinates.

plot(us.cities$long, us.cities$lat, pch = 16, asp = 1, cex = 0.5)


Q8. Import the following CSV file as a data frame:

https://raw.githubusercontent.com/ajlyons/rspatial_bgs23/main/notebooks/data/sf_libraries.csv

How many libraries are there in San Francisco?

libraries <- read.csv("https://raw.githubusercontent.com/ajlyons/rspatial_bgs23/main/notebooks/data/sf_libraries.csv")

nrow(libraries)
## [1] 28


Q09. Use runif() to generate 20 random numbers uniformly distributed between 100 and 200.

runif(20, min = 100, max = 200)
##  [1] 159.0500 139.6720 106.6303 192.1696 151.9047 195.9522 191.5418 129.5753
##  [9] 196.5169 129.3571 155.2220 127.4217 144.6266 149.8893 119.4761 174.9597
## [17] 144.2200 168.2474 162.9821 174.2144


Q10. Rewrite the following expression with pipes. You can use either the traditional pipe (%>%) from the magrittr package, or the more recent native pipe (|>).

sum(round(rnorm(n = 200, mean = 50, sd = 5)))


rnorm(n = 200, mean = 50, sd = 5) |> round() |> sum()
## [1] 9981

or

library(magrittr)
rnorm(n = 200, mean = 50, sd = 5) %>% round() %>% sum()
## [1] 10023