library(sf)
## Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(sp)
library(spData)
library(raster)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(stringr)
library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:raster':
## 
##     extract
library(ggplot2)

#Question 1

us_states_name <- us_states %>% select(NAME)
class(us_states_name)
## [1] "sf"         "data.frame"
#dataframe with only state name and geometry
#class is 'sf' and 'data.frame'
us_states_name2 <- st_drop_geometry(us_states_name)
class(us_states_name2)
## [1] "data.frame"
#now there is no geometry, so its only a "data.frame"



#Question 2

us_pop <- us_states %>% select(total_pop_10, total_pop_15)
us_pop
## Simple feature collection with 49 features and 2 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
## First 10 features:
##    total_pop_10 total_pop_15                       geometry
## 1       4712651      4830620 MULTIPOLYGON (((-88.20006 3...
## 2       6246816      6641928 MULTIPOLYGON (((-114.7196 3...
## 3       4887061      5278906 MULTIPOLYGON (((-109.0501 4...
## 4       3545837      3593222 MULTIPOLYGON (((-73.48731 4...
## 5      18511620     19645772 MULTIPOLYGON (((-81.81169 2...
## 6       9468815     10006693 MULTIPOLYGON (((-85.60516 3...
## 7       1526797      1616547 MULTIPOLYGON (((-116.916 45...
## 8       6417398      6568645 MULTIPOLYGON (((-87.52404 4...
## 9       2809329      2892987 MULTIPOLYGON (((-102.0517 4...
## 10      4429940      4625253 MULTIPOLYGON (((-92.01783 2...
#First way, with select

us_states %>% select(contains("total_pop"))
## Simple feature collection with 49 features and 2 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
## First 10 features:
##    total_pop_10 total_pop_15                       geometry
## 1       4712651      4830620 MULTIPOLYGON (((-88.20006 3...
## 2       6246816      6641928 MULTIPOLYGON (((-114.7196 3...
## 3       4887061      5278906 MULTIPOLYGON (((-109.0501 4...
## 4       3545837      3593222 MULTIPOLYGON (((-73.48731 4...
## 5      18511620     19645772 MULTIPOLYGON (((-81.81169 2...
## 6       9468815     10006693 MULTIPOLYGON (((-85.60516 3...
## 7       1526797      1616547 MULTIPOLYGON (((-116.916 45...
## 8       6417398      6568645 MULTIPOLYGON (((-87.52404 4...
## 9       2809329      2892987 MULTIPOLYGON (((-102.0517 4...
## 10      4429940      4625253 MULTIPOLYGON (((-92.01783 2...
#Second way with contains 

us_states %>% select(starts_with("total_pop"))
## Simple feature collection with 49 features and 2 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
## First 10 features:
##    total_pop_10 total_pop_15                       geometry
## 1       4712651      4830620 MULTIPOLYGON (((-88.20006 3...
## 2       6246816      6641928 MULTIPOLYGON (((-114.7196 3...
## 3       4887061      5278906 MULTIPOLYGON (((-109.0501 4...
## 4       3545837      3593222 MULTIPOLYGON (((-73.48731 4...
## 5      18511620     19645772 MULTIPOLYGON (((-81.81169 2...
## 6       9468815     10006693 MULTIPOLYGON (((-85.60516 3...
## 7       1526797      1616547 MULTIPOLYGON (((-116.916 45...
## 8       6417398      6568645 MULTIPOLYGON (((-87.52404 4...
## 9       2809329      2892987 MULTIPOLYGON (((-102.0517 4...
## 10      4429940      4625253 MULTIPOLYGON (((-92.01783 2...
#Third way, with starts with



#Question 3

midwest <- us_states %>% filter(REGION == "Midwest")

west <- us_states %>% filter(REGION == "West", AREA > units::set_units(250000, km^2),
                     total_pop_15 > 5000000)

south <- us_states %>% filter(REGION == "South", AREA > units::set_units(150000, km^2),
                              total_pop_15 > 7000000)




#Question 4 

us_states %>% summarize(total_pop = sum(total_pop_15),
                        min_pop = min(total_pop_15),
                        max_pop = max(total_pop_15))
## Simple feature collection with 1 feature and 3 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
##   total_pop min_pop  max_pop                       geometry
## 1 314375347  579679 38421464 MULTIPOLYGON (((-81.81169 2...
#The sum of total population in 2015 is 3,143,575,347
#The minimum is 579,679 and maximum is 38,421,464





#Question 5

us_states %>% group_by(REGION) %>% 
  summarise(NumberofStates = n())
## `summarise()` ungrouping output (override with `.groups` argument)
## Simple feature collection with 4 features and 2 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
## # A tibble: 4 x 3
##   REGION   NumberofStates                                               geometry
##   <fct>             <int>                                     <MULTIPOLYGON [°]>
## 1 Norteast              9 (((-75.55945 39.62981, -75.50974 39.68611, -75.41506 …
## 2 Midwest              12 (((-87.80048 42.49192, -87.83477 42.30152, -87.80007 …
## 3 South                17 (((-81.81169 24.56874, -81.74565 24.65988, -81.44351 …
## 4 West                 11 (((-118.6055 33.031, -118.37 32.83927, -118.4963 32.8…
#There are 9 states in the northeast, 12 in the midwest, 17 in the south, and 11 in the west




#Question 6

us_states %>% group_by(REGION) %>% 
  summarise(total = sum(total_pop_15), 
            min = min(total_pop_15),
            max = max(total_pop_15))
## `summarise()` ungrouping output (override with `.groups` argument)
## Simple feature collection with 4 features and 4 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
## # A tibble: 4 x 5
##   REGION     total    min    max                                        geometry
##   <fct>      <dbl>  <dbl>  <dbl>                              <MULTIPOLYGON [°]>
## 1 Nortea…   5.60e7 626604 1.97e7 (((-75.55945 39.62981, -75.50974 39.68611, -75…
## 2 Midwest   6.75e7 721640 1.29e7 (((-87.80048 42.49192, -87.83477 42.30152, -87…
## 3 South     1.19e8 647484 2.65e7 (((-81.81169 24.56874, -81.74565 24.65988, -81…
## 4 West      7.23e7 579679 3.84e7 (((-118.6055 33.031, -118.37 32.83927, -118.49…
#Question 7

us_states_stats <- us_states %>% 
  left_join(us_states_df, by = c("NAME" = "state"))
class(us_states_stats)
## [1] "sf"         "data.frame"
#used left_join because it allowed to keep the same number of rows as the object on the left(us_states)
#the key variable in both is the state name, but they are different
#the new class is 'sf' data.frame' 




#Question 8 

setdiff(us_states_df$state, us_states$NAME)
## [1] "Alaska" "Hawaii"
#shows the states not included in us_states_df

us_states_stats5 <- us_states_df  %>% 
  anti_join(us_states, by = c("state" = "NAME"))
us_states_stats5
## # A tibble: 2 x 5
##   state  median_income_10 median_income_15 poverty_level_10 poverty_level_15
##   <chr>             <dbl>            <dbl>            <dbl>            <dbl>
## 1 Alaska            29509            31455            64245            72957
## 2 Hawaii            29945            31051           124627           153944
#anti_join only returns rows that don't match, so Hawaii and Alaska



#Question 9

us_states9 <- us_states %>%
  mutate(popDen15 = total_pop_15/AREA)
us_states9
## Simple feature collection with 49 features and 7 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
## First 10 features:
##    GEOID        NAME   REGION             AREA total_pop_10 total_pop_15
## 1     01     Alabama    South 133709.27 [km^2]      4712651      4830620
## 2     04     Arizona     West 295281.25 [km^2]      6246816      6641928
## 3     08    Colorado     West 269573.06 [km^2]      4887061      5278906
## 4     09 Connecticut Norteast  12976.59 [km^2]      3545837      3593222
## 5     12     Florida    South 151052.01 [km^2]     18511620     19645772
## 6     13     Georgia    South 152725.21 [km^2]      9468815     10006693
## 7     16       Idaho     West 216512.66 [km^2]      1526797      1616547
## 8     18     Indiana  Midwest  93648.40 [km^2]      6417398      6568645
## 9     20      Kansas  Midwest 213037.08 [km^2]      2809329      2892987
## 10    22   Louisiana    South 122345.76 [km^2]      4429940      4625253
##                          geometry            popDen15
## 1  MULTIPOLYGON (((-88.20006 3...  36.127786 [1/km^2]
## 2  MULTIPOLYGON (((-114.7196 3...  22.493565 [1/km^2]
## 3  MULTIPOLYGON (((-109.0501 4...  19.582469 [1/km^2]
## 4  MULTIPOLYGON (((-73.48731 4... 276.900364 [1/km^2]
## 5  MULTIPOLYGON (((-81.81169 2... 130.059657 [1/km^2]
## 6  MULTIPOLYGON (((-85.60516 3...  65.520897 [1/km^2]
## 7  MULTIPOLYGON (((-116.916 45...   7.466293 [1/km^2]
## 8  MULTIPOLYGON (((-87.52404 4...  70.141565 [1/km^2]
## 9  MULTIPOLYGON (((-102.0517 4...  13.579734 [1/km^2]
## 10 MULTIPOLYGON (((-92.01783 2...  37.804767 [1/km^2]
us_states99 <- us_states9 %>%
  mutate(popDen10 = total_pop_10/AREA)
us_states99
## Simple feature collection with 49 features and 8 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
## First 10 features:
##    GEOID        NAME   REGION             AREA total_pop_10 total_pop_15
## 1     01     Alabama    South 133709.27 [km^2]      4712651      4830620
## 2     04     Arizona     West 295281.25 [km^2]      6246816      6641928
## 3     08    Colorado     West 269573.06 [km^2]      4887061      5278906
## 4     09 Connecticut Norteast  12976.59 [km^2]      3545837      3593222
## 5     12     Florida    South 151052.01 [km^2]     18511620     19645772
## 6     13     Georgia    South 152725.21 [km^2]      9468815     10006693
## 7     16       Idaho     West 216512.66 [km^2]      1526797      1616547
## 8     18     Indiana  Midwest  93648.40 [km^2]      6417398      6568645
## 9     20      Kansas  Midwest 213037.08 [km^2]      2809329      2892987
## 10    22   Louisiana    South 122345.76 [km^2]      4429940      4625253
##                          geometry            popDen15            popDen10
## 1  MULTIPOLYGON (((-88.20006 3...  36.127786 [1/km^2]  35.245506 [1/km^2]
## 2  MULTIPOLYGON (((-114.7196 3...  22.493565 [1/km^2]  21.155478 [1/km^2]
## 3  MULTIPOLYGON (((-109.0501 4...  19.582469 [1/km^2]  18.128893 [1/km^2]
## 4  MULTIPOLYGON (((-73.48731 4... 276.900364 [1/km^2] 273.248788 [1/km^2]
## 5  MULTIPOLYGON (((-81.81169 2... 130.059657 [1/km^2] 122.551303 [1/km^2]
## 6  MULTIPOLYGON (((-85.60516 3...  65.520897 [1/km^2]  61.999029 [1/km^2]
## 7  MULTIPOLYGON (((-116.916 45...   7.466293 [1/km^2]   7.051768 [1/km^2]
## 8  MULTIPOLYGON (((-87.52404 4...  70.141565 [1/km^2]  68.526513 [1/km^2]
## 9  MULTIPOLYGON (((-102.0517 4...  13.579734 [1/km^2]  13.187042 [1/km^2]
## 10 MULTIPOLYGON (((-92.01783 2...  37.804767 [1/km^2]  36.208365 [1/km^2]
#now has a popDen10 and popDen15 column



#Question 10

us_states999 <- us_states99 %>%
  mutate(popDiff = popDen15 - popDen10)
us_states999
## Simple feature collection with 49 features and 9 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
## First 10 features:
##    GEOID        NAME   REGION             AREA total_pop_10 total_pop_15
## 1     01     Alabama    South 133709.27 [km^2]      4712651      4830620
## 2     04     Arizona     West 295281.25 [km^2]      6246816      6641928
## 3     08    Colorado     West 269573.06 [km^2]      4887061      5278906
## 4     09 Connecticut Norteast  12976.59 [km^2]      3545837      3593222
## 5     12     Florida    South 151052.01 [km^2]     18511620     19645772
## 6     13     Georgia    South 152725.21 [km^2]      9468815     10006693
## 7     16       Idaho     West 216512.66 [km^2]      1526797      1616547
## 8     18     Indiana  Midwest  93648.40 [km^2]      6417398      6568645
## 9     20      Kansas  Midwest 213037.08 [km^2]      2809329      2892987
## 10    22   Louisiana    South 122345.76 [km^2]      4429940      4625253
##                          geometry            popDen15            popDen10
## 1  MULTIPOLYGON (((-88.20006 3...  36.127786 [1/km^2]  35.245506 [1/km^2]
## 2  MULTIPOLYGON (((-114.7196 3...  22.493565 [1/km^2]  21.155478 [1/km^2]
## 3  MULTIPOLYGON (((-109.0501 4...  19.582469 [1/km^2]  18.128893 [1/km^2]
## 4  MULTIPOLYGON (((-73.48731 4... 276.900364 [1/km^2] 273.248788 [1/km^2]
## 5  MULTIPOLYGON (((-81.81169 2... 130.059657 [1/km^2] 122.551303 [1/km^2]
## 6  MULTIPOLYGON (((-85.60516 3...  65.520897 [1/km^2]  61.999029 [1/km^2]
## 7  MULTIPOLYGON (((-116.916 45...   7.466293 [1/km^2]   7.051768 [1/km^2]
## 8  MULTIPOLYGON (((-87.52404 4...  70.141565 [1/km^2]  68.526513 [1/km^2]
## 9  MULTIPOLYGON (((-102.0517 4...  13.579734 [1/km^2]  13.187042 [1/km^2]
## 10 MULTIPOLYGON (((-92.01783 2...  37.804767 [1/km^2]  36.208365 [1/km^2]
##               popDiff
## 1  0.8822799 [1/km^2]
## 2  1.3380870 [1/km^2]
## 3  1.4535763 [1/km^2]
## 4  3.6515761 [1/km^2]
## 5  7.5083545 [1/km^2]
## 6  3.5218677 [1/km^2]
## 7  0.4145254 [1/km^2]
## 8  1.6150517 [1/km^2]
## 9  0.3926922 [1/km^2]
## 10 1.5964018 [1/km^2]
plot(us_states999["popDiff"], nbreaks = 5, breaks = "jenks")

#Question 11

tolower(us_states$NAME)
##  [1] "alabama"              "arizona"              "colorado"            
##  [4] "connecticut"          "florida"              "georgia"             
##  [7] "idaho"                "indiana"              "kansas"              
## [10] "louisiana"            "massachusetts"        "minnesota"           
## [13] "missouri"             "montana"              "nevada"              
## [16] "new jersey"           "new york"             "north dakota"        
## [19] "oklahoma"             "pennsylvania"         "south carolina"      
## [22] "south dakota"         "texas"                "vermont"             
## [25] "west virginia"        "arkansas"             "california"          
## [28] "delaware"             "district of columbia" "illinois"            
## [31] "iowa"                 "kentucky"             "maine"               
## [34] "maryland"             "michigan"             "mississippi"         
## [37] "nebraska"             "new hampshire"        "new mexico"          
## [40] "north carolina"       "ohio"                 "oregon"              
## [43] "rhode island"         "tennessee"            "utah"                
## [46] "virginia"             "washington"           "wisconsin"           
## [49] "wyoming"
#lowers all states names

us_states %>%
  setNames(tolower(colnames(us_states)))
## Simple feature collection with 49 features and 6 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
## First 10 features:
##    geoid        name   region             area total_pop_10 total_pop_15
## 1     01     Alabama    South 133709.27 [km^2]      4712651      4830620
## 2     04     Arizona     West 295281.25 [km^2]      6246816      6641928
## 3     08    Colorado     West 269573.06 [km^2]      4887061      5278906
## 4     09 Connecticut Norteast  12976.59 [km^2]      3545837      3593222
## 5     12     Florida    South 151052.01 [km^2]     18511620     19645772
## 6     13     Georgia    South 152725.21 [km^2]      9468815     10006693
## 7     16       Idaho     West 216512.66 [km^2]      1526797      1616547
## 8     18     Indiana  Midwest  93648.40 [km^2]      6417398      6568645
## 9     20      Kansas  Midwest 213037.08 [km^2]      2809329      2892987
## 10    22   Louisiana    South 122345.76 [km^2]      4429940      4625253
##                          geometry
## 1  MULTIPOLYGON (((-88.20006 3...
## 2  MULTIPOLYGON (((-114.7196 3...
## 3  MULTIPOLYGON (((-109.0501 4...
## 4  MULTIPOLYGON (((-73.48731 4...
## 5  MULTIPOLYGON (((-81.81169 2...
## 6  MULTIPOLYGON (((-85.60516 3...
## 7  MULTIPOLYGON (((-116.916 45...
## 8  MULTIPOLYGON (((-87.52404 4...
## 9  MULTIPOLYGON (((-102.0517 4...
## 10 MULTIPOLYGON (((-92.01783 2...
#set all column names to lowercase




#Question 12

us_states_sel <- us_states_stats %>%
  select(Income = median_income_15)
us_states_sel
## Simple feature collection with 49 features and 1 field
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
## First 10 features:
##    Income                       geometry
## 1   22890 MULTIPOLYGON (((-88.20006 3...
## 2   26156 MULTIPOLYGON (((-114.7196 3...
## 3   30752 MULTIPOLYGON (((-109.0501 4...
## 4   33226 MULTIPOLYGON (((-73.48731 4...
## 5   24654 MULTIPOLYGON (((-81.81169 2...
## 6   25588 MULTIPOLYGON (((-85.60516 3...
## 7   23558 MULTIPOLYGON (((-116.916 45...
## 8   25834 MULTIPOLYGON (((-87.52404 4...
## 9   27315 MULTIPOLYGON (((-102.0517 4...
## 10  24014 MULTIPOLYGON (((-92.01783 2...
#new object with just income and geometry 
ggplot(us_states_sel) + geom_sf(aes(fill = Income))

#Question 13

us_states13 <- us_states_stats %>%
  mutate(incomeDiff = median_income_15 - median_income_10)
us_states13
## Simple feature collection with 49 features and 11 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7042 ymin: 24.55868 xmax: -66.9824 ymax: 49.38436
## geographic CRS: NAD83
## First 10 features:
##    GEOID        NAME   REGION             AREA total_pop_10 total_pop_15
## 1     01     Alabama    South 133709.27 [km^2]      4712651      4830620
## 2     04     Arizona     West 295281.25 [km^2]      6246816      6641928
## 3     08    Colorado     West 269573.06 [km^2]      4887061      5278906
## 4     09 Connecticut Norteast  12976.59 [km^2]      3545837      3593222
## 5     12     Florida    South 151052.01 [km^2]     18511620     19645772
## 6     13     Georgia    South 152725.21 [km^2]      9468815     10006693
## 7     16       Idaho     West 216512.66 [km^2]      1526797      1616547
## 8     18     Indiana  Midwest  93648.40 [km^2]      6417398      6568645
## 9     20      Kansas  Midwest 213037.08 [km^2]      2809329      2892987
## 10    22   Louisiana    South 122345.76 [km^2]      4429940      4625253
##    median_income_10 median_income_15 poverty_level_10 poverty_level_15
## 1             21746            22890           786544           887260
## 2             26412            26156           933113          1180690
## 3             29365            30752           584184           653969
## 4             32258            33226           314306           366351
## 5             24812            24654          2502365          3180109
## 6             25596            25588          1445752          1788947
## 7             22866            23558           203177           245177
## 8             24934            25834           842540           978043
## 9             25696            27315           338792           381353
## 10            22053            24014           780359           888280
##                          geometry incomeDiff
## 1  MULTIPOLYGON (((-88.20006 3...       1144
## 2  MULTIPOLYGON (((-114.7196 3...       -256
## 3  MULTIPOLYGON (((-109.0501 4...       1387
## 4  MULTIPOLYGON (((-73.48731 4...        968
## 5  MULTIPOLYGON (((-81.81169 2...       -158
## 6  MULTIPOLYGON (((-85.60516 3...         -8
## 7  MULTIPOLYGON (((-116.916 45...        692
## 8  MULTIPOLYGON (((-87.52404 4...        900
## 9  MULTIPOLYGON (((-102.0517 4...       1619
## 10 MULTIPOLYGON (((-92.01783 2...       1961
#has a column with the difference in income between 2015 and 2010




#Question 14

q14 <- raster(nrow=9, ncol=9, res=0.5)


#Question 15 

grain_size = c("clay", "silt", "sand")
grain = raster(nrow = 6, ncol = 6, res = 0.5, 
               xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5,
               vals = factor(sample(grain_size, 36, replace = TRUE), 
                             levels = grain_size))
#from tutorial

factorValues(grain, modal(values(grain)))
##   VALUE
## 1  clay
#Question 16

data(dem, package = "spDataLarge")
par(mfrow = c(1, 2))
hist(dem)
boxplot(dem)

#Question 1 

canterbury <- nz %>% filter(Name == "Canterbury")
canterbury
## Simple feature collection with 1 feature and 6 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 1325039 ymin: 5004766 xmax: 1686902 ymax: 5360239
## projected CRS:  NZGD2000 / New Zealand Transverse Mercator 2000
##         Name Island Land_area Population Median_income Sex_ratio
## 1 Canterbury  South   44504.5     612000         30100 0.9753265
##                             geom
## 1 MULTIPOLYGON (((1686902 535...
c_height <- nz_height[canterbury, ]
c_height
## Simple feature collection with 70 features and 2 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 1365809 ymin: 5158491 xmax: 1654899 ymax: 5350463
## projected CRS:  NZGD2000 / New Zealand Transverse Mercator 2000
## First 10 features:
##    t50_fid elevation                geometry
## 5  2362630      2749 POINT (1378170 5158491)
## 6  2362814      2822 POINT (1389460 5168749)
## 7  2362817      2778 POINT (1390166 5169466)
## 8  2363991      3004 POINT (1372357 5172729)
## 9  2363993      3114 POINT (1372062 5173236)
## 10 2363994      2882 POINT (1372810 5173419)
## 11 2363995      2796 POINT (1372579 5173989)
## 13 2363997      3070 POINT (1373796 5174144)
## 14 2363998      3061 POINT (1373955 5174231)
## 15 2363999      3077 POINT (1373984 5175228)
#70 peaks in the canterbury region



#Question 2

nz_height_count <-  aggregate(nz_height, nz, length)
nz_height_combined <-  cbind(nz, count = nz_height_count$elevation)
nz_height_combined %>% 
  st_set_geometry(NULL) %>% 
  select(Name, count) %>% 
  arrange(desc(count)) %>% 
  slice(2)
##         Name count
## 1 West Coast    22
#Question 3

nz_height_combined %>% 
  st_set_geometry(NULL) %>% 
  select(Name, count) %>% 
  arrange(desc(count)) %>%
  na.omit()
##                Name count
## 1        Canterbury    70
## 2        West Coast    22
## 3           Waikato     3
## 4 Manawatu-Wanganui     2
## 5             Otago     2
## 6         Southland     1
## 7       Marlborough     1
#Question 4 

library(classInt)

data(dem, package = "spDataLarge")
data(ndvi, package = "spDataLarge")
summary(dem)
##          dem
## Min.     238
## 1st Qu.  366
## Median   478
## 3rd Qu.  748
## Max.    1094
## NA's       0
brk <-  classIntervals(values(dem), n = 3)$brk
rcl <-  matrix(c(brk[1] - 1, brk[2], 1, brk[2], brk[3], 2, brk[3], brk[4], 3), 
             ncol = 3, byrow = TRUE)
recl <-  reclassify(dem, rcl = rcl)
zonal(stack(dem, ndvi), recl, fun = "mean")
##      zone      dem       ndvi
## [1,]    1 329.7571 -0.3473349
## [2,]    2 492.8862 -0.1311101
## [3,]    3 846.9908 -0.2944226