+
+
+
+
+
+1. Read in the Data
+
+
library (data.table)
+library (dtplyr)
+library (dplyr)
+
+
+Attaching package: 'dplyr'
+
+
+
The following objects are masked from 'package:data.table':
+
+ between, first, last
+
+
+
The following objects are masked from 'package:stats':
+
+ filter, lag
+
+
+
The following objects are masked from 'package:base':
+
+ intersect, setdiff, setequal, union
+
+
met <- data.table:: fread ("C:/Users/ellya/OneDrive/Desktop/PM566labs/met_all.gz" )
+
+ stations <- data.table:: fread ("https://noaa-isd-pds.s3.amazonaws.com/isd-history.csv" )
+
+
+
+2. Setup the Data
+
+
stations[, USAF : = as.integer (USAF)]
+
+
Warning in eval(jsub, SDenv, parent.frame()): NAs introduced by coercion
+
+
stations[, USAF : = fifelse (USAF == 999999 , NA_integer_ , USAF)]
+ stations[, CTRY : = fifelse (CTRY == "" , NA_character_ , CTRY)]
+ stations[, STATE : = fifelse (STATE == "" , NA_character_ , STATE)]
+
+ stations <- unique (stations[, list (USAF, CTRY, STATE)])
+
+ stations <- stations[! is.na (USAF)]
+
+ stations[, n : = 1 : .N, by = .(USAF)]
+ stations <- stations[n == 1 ,][, n : = NULL ]
+
+ met <- met[, c ('USAFID' , 'WBAN' , 'year' , 'month' , 'day' , 'hour' , 'min' , 'lat' , 'lon' , 'elev' , 'wind.sp' , 'temp' , 'atm.press' )]
+
+ met <- merge (
+ x = met,
+ y = stations,
+ by.x = "USAFID" ,
+ by.y = "USAF" ,
+ all.x = TRUE ,
+ all.y = FALSE
+ )
+
+nrow (met)
+
+
head (met[, c ('USAFID' , 'WBAN' , 'STATE' )], n = 4 )
+
+
Key: <USAFID>
+ USAFID WBAN STATE
+ <int> <int> <char>
+1: 690150 93121 CA
+2: 690150 93121 CA
+3: 690150 93121 CA
+4: 690150 93121 CA
+
+
+
+
+Question 1: Representative Station for the US
+
+
+
+Question 2: Representative Station Per State
+Identify the most representative, the median, station per state using the euclidean distance. If multiple stations show in the median, select the one located at the lowest latitude.
+
+
state_met1 <- met[,.(
+ temp_med = median (temp, na.rm = TRUE ),
+ wind.sp_med = median (wind.sp, na.rm = TRUE ),
+ atm.press_med = median (atm.press, na.rm = TRUE )
+ ), by = STATE]
+
+ met1 <- merge (
+ x = met,
+ y = state_met1,
+ by.x = "STATE" ,
+ by.y = "STATE" ,
+ all.x = TRUE ,
+ all.y = FALSE
+ )
+
+ met1[, euc : = sqrt (
+ (temp - temp_med)^ 2 +
+ (wind.sp - wind.sp_med)^ 2 +
+ (atm.press - atm.press_med)^ 2
+ )]
+
+ staterep <- met1[order (euc, lat), .SD[1 ], by = STATE]
+ staterep[order (STATE)]
+
+
STATE USAFID WBAN year month day hour min lat lon elev
+ <char> <int> <int> <int> <int> <int> <int> <int> <num> <num> <int>
+ 1: AL 722265 13821 2019 8 18 6 56 32.383 -86.350 52
+ 2: AR 723419 93992 2019 8 19 6 53 33.221 -92.814 87
+ 3: AZ 722730 3124 2019 8 31 22 55 31.583 -110.344 1438
+ 4: CA 722903 3131 2019 8 3 3 53 32.816 -117.139 129
+ 5: CO 724665 93010 2019 8 21 9 55 39.275 -103.666 1635
+ 6: CT 725027 54788 2019 8 12 12 53 41.510 -72.828 32
+ 7: DE 724180 13781 2019 8 16 0 51 39.674 -75.606 24
+ 8: FL 722210 13858 2019 8 26 3 56 30.483 -86.517 27
+ 9: GA 722175 13860 2019 8 27 1 22 32.633 -83.600 90
+10: IA 725470 94908 2019 8 21 23 53 42.398 -90.704 329
+11: ID 726810 24131 2019 8 28 5 53 43.567 -116.240 874
+12: IL 725430 94822 2019 8 21 20 54 42.193 -89.093 226
+13: IN 725335 94833 2019 8 22 9 56 40.650 -86.150 247
+14: KS 724520 3957 2019 8 24 2 56 37.284 -98.553 469
+15: KY 724240 13807 2019 8 26 2 50 37.900 -85.967 238
+16: LA 722400 3937 2019 8 7 8 53 30.125 -93.228 3
+17: MA 725065 94726 2019 8 11 13 53 41.676 -70.958 24
+18: MD 724060 93721 2019 8 16 3 54 39.173 -76.684 47
+19: ME 726083 4836 2019 8 1 0 53 47.286 -68.313 305
+20: MI 725386 94898 2019 8 19 11 51 44.022 -82.793 180
+21: MN 726440 14925 2019 8 5 6 54 43.904 -92.492 402
+22: MO 724450 3945 2019 8 30 5 54 38.817 -92.218 274
+23: MS 722358 93919 2019 8 15 12 53 31.183 -90.471 129
+24: MT 726770 24033 2019 8 11 3 0 45.800 -108.533 1088
+25: NC 723066 13713 2019 8 18 2 56 35.344 -77.965 34
+26: ND 720491 150 2019 8 1 0 15 46.217 -97.633 386
+27: NE 725555 94946 2019 8 9 10 53 41.433 -99.633 771
+28: NH 726164 54728 2019 8 21 14 52 44.368 -71.545 327
+29: NJ 724096 14706 2019 8 11 23 56 40.016 -74.592 43
+30: NM 723600 23051 2019 8 21 3 55 36.449 -103.153 1515
+31: NV 725825 24121 2019 8 6 3 56 40.829 -115.788 1547
+32: NY 726228 94740 2019 8 4 21 51 44.385 -74.207 507
+33: OH 724290 93815 2019 8 16 3 0 39.867 -84.117 306
+34: OK 723544 3954 2019 8 6 10 53 35.534 -97.647 396
+35: OR 720365 24267 2019 8 9 20 56 42.070 -124.290 140
+36: PA 725116 54737 2019 8 5 10 53 40.120 -76.294 123
+37: RI 725079 14787 2019 8 23 17 53 41.533 -71.283 52
+38: SC 723106 13744 2019 8 18 5 53 34.188 -79.731 46
+39: SD 726519 94993 2019 8 4 12 56 45.669 -96.991 354
+40: TN 723340 13893 2019 8 3 12 0 35.050 -90.000 87
+41: TX 722536 12911 2019 8 23 14 51 29.533 -98.262 232
+42: UT 725720 24127 2019 8 5 6 54 40.778 -111.969 1288
+43: VA 724035 13773 2019 8 28 0 56 38.504 -77.305 4
+44: VT 725165 94737 2019 8 21 12 56 43.533 -72.950 240
+45: WA 720254 119 2019 8 1 5 55 46.677 -122.983 54
+46: WI 726424 94818 2019 8 19 9 53 42.761 -87.814 205
+47: WV 724176 13736 2019 8 19 9 53 39.643 -79.916 382
+48: WY 725690 24089 2019 8 4 4 53 42.898 -106.473 1612
+ STATE USAFID WBAN year month day hour min lat lon elev
+ wind.sp temp atm.press CTRY temp_med wind.sp_med atm.press_med euc
+ <num> <num> <num> <char> <num> <num> <num> <num>
+ 1: 1.5 25.3 1014.9 US 25.3 1.5 1014.8 0.1000000
+ 2: 2.1 25.6 1014.5 US 25.6 2.1 1014.5 0.0000000
+ 3: 3.1 29.0 1010.7 US 29.0 3.1 1010.8 0.1000000
+ 4: 2.6 21.1 1012.8 US 21.1 2.6 1012.8 0.0000000
+ 5: 2.6 18.9 1013.6 US 18.9 2.6 1013.7 0.1000000
+ 6: 2.1 22.2 1015.2 US 22.2 2.1 1015.2 0.0000000
+ 7: 2.6 24.4 1015.4 US 24.4 2.6 1015.4 0.0000000
+ 8: 2.6 26.9 1015.1 US 27.0 2.6 1015.1 0.1000000
+ 9: 1.5 26.0 1014.9 US 26.0 1.5 1014.9 0.0000000
+10: 2.6 21.1 1014.9 US 21.0 2.6 1014.9 0.1000000
+11: 2.6 20.0 1013.0 US 20.0 2.1 1013.1 0.5099020
+12: 2.1 22.2 1014.7 US 22.2 2.1 1014.6 0.1000000
+13: 2.1 22.0 1014.6 US 22.0 2.1 1014.8 0.2000000
+14: 3.6 23.3 1013.6 US 23.3 3.6 1013.5 0.1000000
+15: 1.5 23.0 1015.3 US 23.0 1.5 1015.3 0.0000000
+16: 1.5 27.8 1014.6 US 27.8 1.5 1014.6 0.0000000
+17: 2.6 21.7 1015.0 US 21.7 2.6 1014.9 0.1000000
+18: 2.1 24.4 1015.6 US 24.4 2.1 1015.3 0.3000000
+19: 2.1 18.9 1014.1 US 18.9 2.1 1014.1 0.0000000
+20: 2.1 20.6 1014.3 US 20.6 2.1 1014.4 0.1000000
+21: 2.1 18.9 1014.9 US 19.0 2.1 1014.7 0.2236068
+22: 2.1 23.3 1014.8 US 23.3 2.1 1014.8 0.0000000
+23: 1.5 26.1 1014.9 US 26.0 1.5 1014.9 0.1000000
+24: 3.1 18.3 1014.5 US 18.3 3.1 1014.3 0.2000000
+25: 1.5 24.0 1015.8 US 24.0 1.5 1015.7 0.1000000
+26: 5.1 27.0 NA US 18.0 3.6 NA NA
+27: 3.1 21.7 1014.3 US 21.7 3.1 1014.3 0.0000000
+28: 1.5 18.9 1014.8 US 18.9 1.5 1014.6 0.2000000
+29: 1.5 23.3 1015.1 US 23.3 1.5 1015.1 0.0000000
+30: 3.1 24.4 1012.0 US 24.4 3.1 1012.0 0.0000000
+31: 2.6 26.7 1011.8 US 27.0 2.6 1011.8 0.3000000
+32: 2.1 20.6 1014.9 US 20.6 2.1 1014.9 0.0000000
+33: 2.6 21.7 1015.0 US 21.7 2.6 1015.0 0.0000000
+34: 3.1 26.7 1012.8 US 26.7 3.1 1012.8 0.0000000
+35: 2.1 17.2 1015.5 US 17.2 2.1 1015.4 0.1000000
+36: 1.5 21.1 1015.6 US 21.1 1.5 1015.6 0.0000000
+37: 2.6 22.2 1014.1 US 22.2 2.6 1014.0 0.1000000
+38: 1.5 25.0 1015.3 US 25.0 1.5 1015.3 0.0000000
+39: 3.6 20.0 1014.1 US 20.0 3.6 1014.3 0.2000000
+40: 1.5 23.9 1014.9 US 24.0 1.5 1014.9 0.1000000
+41: 3.1 29.0 1012.6 US 29.0 3.1 1012.6 0.0000000
+42: 4.1 26.1 1012.2 US 26.1 4.1 1012.1 0.1000000
+43: 1.5 23.3 1015.2 US 23.4 1.5 1015.2 0.1000000
+44: 1.5 18.9 1014.5 US 18.9 1.5 1014.5 0.0000000
+45: 0.0 17.0 NA US 18.0 0.0 NA NA
+46: 2.1 18.9 1014.6 US 18.6 2.1 1014.6 0.3000000
+47: 1.5 21.1 1015.9 US 21.1 1.5 1015.7 0.2000000
+48: 3.6 18.3 1014.1 US 18.3 3.6 1014.0 0.1000000
+ wind.sp temp atm.press CTRY temp_med wind.sp_med atm.press_med euc
+
+
+
+
+3. In the Middle?
+Identify what is the station that is closest to the mid-point of the state. Combining these with the stations you identified in the previous question, use leaflet()
to visualize all ~100 points in the same figure, applying different colors for those identified in this question.
+
+
statecenters <- data.frame (
+ longitude = state.center[1 ],
+ latitude = state.center[2 ],
+ STATE = state.abb,
+ stringsAsFactors = FALSE
+ )
+
+ statecenters[statecenters$ state == "AK" , c ("x" , "y" )] <- c (- 152.00 , 63.83 )
+ statecenters[statecenters$ state == "HI" , c ("x" , "y" )] <- c (- 156.33 , 20.25 )
+
+ statecenters <- statecenters %>%
+ rename (
+ longitude = x,
+ latitude = y)
+
+ met2 <- merge (
+ x = met1,
+ y = statecenters,
+ by.x = "STATE" ,
+ by.y = "STATE" ,
+ all.x = TRUE ,
+ all.y = FALSE
+ )
+
+ met2[, euc2 : = sqrt (
+ (lat - latitude)^ 2 +
+ (lon - longitude)^ 2
+ )]
+
+ centerstation <- met2[order (euc2), .SD[1 ], by = STATE]
+ centerstation[order (STATE)]
+
+
STATE USAFID WBAN year month day hour min lat lon elev
+ <char> <int> <int> <int> <int> <int> <int> <int> <num> <num> <int>
+ 1: AL 722265 13821 2019 8 1 10 56 32.383 -86.366 52
+ 2: AR 723403 13963 2019 8 1 0 53 34.727 -92.239 78
+ 3: AZ 723745 374 2019 8 1 0 15 34.257 -111.339 1572
+ 4: CA 723890 93193 2019 8 1 0 0 36.767 -119.717 100
+ 5: CO 726396 422 2019 8 1 0 10 39.050 -105.516 3438
+ 6: CT 725084 54767 2019 8 1 0 52 41.742 -72.184 75
+ 7: DE 724093 13764 2019 8 12 0 54 38.689 -75.359 15
+ 8: FL 722123 12809 2019 8 1 18 20 27.943 -81.783 38
+ 9: GA 720962 337 2019 8 1 0 15 32.214 -83.128 93
+10: IA 725472 94989 2019 8 1 0 53 41.991 -93.619 291
+11: ID 725865 94161 2019 8 1 12 50 43.504 -114.296 1622
+12: IL 724390 93822 2019 8 1 0 52 39.845 -89.684 187
+13: IN 720575 173 2019 8 1 0 15 40.031 -86.251 281
+14: KS 724506 13986 2019 8 13 0 52 38.066 -97.861 470
+15: KY 720448 144 2019 8 1 0 15 37.578 -84.770 312
+16: LA 720468 466 2019 8 1 0 15 30.558 -92.099 23
+17: MA 725107 4780 2019 8 1 0 52 42.552 -71.756 106
+18: MD 724060 93721 2019 8 1 0 0 39.183 -76.667 47
+19: ME 726196 14610 2019 8 1 0 36 45.648 -68.693 123
+20: MI 725405 54816 2019 8 1 0 15 43.322 -84.688 230
+21: MN 726555 94938 2019 8 1 13 53 46.398 -94.138 374
+22: MO 724459 53931 2019 8 1 8 56 38.096 -92.549 265
+23: MS 722350 3940 2019 8 1 0 38 32.321 -90.078 101
+24: MT 726776 24036 2019 8 1 0 54 47.049 -109.457 1264
+25: NC 722131 3713 2019 8 1 0 15 35.541 -78.390 50
+26: ND 720871 296 2019 8 1 0 15 46.768 -100.894 593
+27: NE 725555 94946 2019 8 1 0 53 41.433 -99.633 771
+28: NH 726155 54736 2019 8 1 0 56 43.567 -71.433 166
+29: NJ 720407 462 2019 8 1 0 56 39.928 -74.292 25
+30: NM 722677 3027 2019 8 13 2 53 35.000 -105.670 2160
+31: NV 724770 3170 2019 8 1 12 53 39.600 -116.010 1812
+32: NY 725196 64775 2019 8 1 17 53 43.234 -75.407 154
+33: OH 720414 139 2019 8 1 0 19 40.333 -82.517 363
+34: OK 722187 93911 2019 8 1 19 55 35.358 -96.943 327
+35: OR 720638 224 2019 8 1 0 15 44.095 -121.200 1055
+36: PA 725128 54739 2019 8 1 5 0 40.849 -77.849 378
+37: RI 725079 14787 2019 8 13 1 53 41.532 -71.282 52
+38: SC 723107 371 2019 8 1 0 15 33.587 -80.209 31
+39: SD 726560 24025 2019 8 1 0 53 44.381 -100.285 526
+40: TN 723273 13827 2019 8 1 0 6 36.009 -86.520 166
+41: TX 722666 93943 2019 8 1 12 55 31.794 -98.956 423
+42: UT 724700 93141 2019 8 1 0 53 39.609 -110.754 1785
+43: VA 724017 3707 2019 8 1 0 15 37.358 -78.438 127
+44: VT 726145 94705 2019 8 1 0 34 44.204 -72.562 342
+45: WA 720388 469 2019 8 1 10 55 47.100 -122.283 164
+46: WI 726574 94985 2019 8 1 0 54 44.638 -90.188 389
+47: WV 724127 53801 2019 8 1 0 10 37.867 -80.400 702
+48: WY 725690 24089 2019 8 1 6 59 42.898 -106.474 1612
+ STATE USAFID WBAN year month day hour min lat lon elev
+ wind.sp temp atm.press CTRY temp_med wind.sp_med atm.press_med euc
+ <num> <num> <num> <char> <num> <num> <num> <num>
+ 1: 1.5 22.9 1016.7 US 25.3 1.5 1014.8 3.061046
+ 2: 3.1 29.4 1016.5 US 25.6 2.1 1014.5 4.409082
+ 3: 0.0 26.0 NA US 29.0 3.1 1010.8 NA
+ 4: 3.1 36.7 1010.3 US 21.1 2.6 1012.8 15.806960
+ 5: 4.6 13.0 NA US 18.9 2.6 1013.7 NA
+ 6: 0.0 21.1 1016.8 US 22.2 2.1 1015.2 2.860070
+ 7: 0.0 22.8 1017.0 US 24.4 2.6 1015.4 3.446738
+ 8: 11.3 30.0 NA US 27.0 2.6 1015.1 NA
+ 9: 0.0 29.5 NA US 26.0 1.5 1014.9 NA
+10: 1.5 20.6 1020.0 US 21.0 2.6 1014.9 5.232590
+11: 4.6 14.0 NA US 20.0 2.1 1013.1 NA
+12: 2.1 22.8 1019.6 US 22.2 2.1 1014.6 5.035871
+13: 2.6 22.2 NA US 22.0 2.1 1014.8 NA
+14: 4.1 31.7 1008.0 US 23.3 3.6 1013.5 10.052860
+15: 0.0 26.0 NA US 23.0 1.5 1015.3 NA
+16: 2.6 27.8 NA US 27.8 1.5 1014.6 NA
+17: 1.5 22.8 1016.4 US 21.7 2.6 1014.9 2.161018
+18: 2.1 27.2 1016.5 US 24.4 2.1 1015.3 3.046309
+19: 0.0 21.1 NA US 18.9 2.1 1014.1 NA
+20: 1.5 22.7 NA US 20.6 2.1 1014.4 NA
+21: 4.6 22.2 1021.6 US 19.0 2.1 1014.7 8.006248
+22: 1.5 20.0 1019.2 US 23.3 2.1 1014.8 5.532630
+23: 3.1 22.8 NA US 26.0 1.5 1014.9 NA
+24: 4.6 25.0 1014.2 US 18.3 3.1 1014.3 6.866586
+25: 0.0 26.0 NA US 24.0 1.5 1015.7 NA
+26: 4.6 27.0 NA US 18.0 3.6 NA NA
+27: 3.1 26.7 1014.6 US 21.7 3.1 1014.3 5.008992
+28: 2.1 21.7 1016.2 US 18.9 1.5 1014.6 3.280244
+29: 0.0 NA NA US 23.3 1.5 1015.1 NA
+30: 3.1 21.7 1015.7 US 24.4 3.1 1012.0 4.580393
+31: 1.5 12.2 1014.3 US 27.0 2.6 1011.8 15.049917
+32: 4.1 26.7 1020.6 US 20.6 2.1 1014.9 8.584870
+33: 2.1 24.0 NA US 21.7 2.6 1015.0 NA
+34: 6.2 33.1 NA US 26.7 3.1 1012.8 NA
+35: 4.6 31.0 NA US 17.2 2.1 1015.4 NA
+36: 0.0 17.8 NA US 21.1 1.5 1015.6 NA
+37: 3.1 22.2 1011.5 US 22.2 2.6 1014.0 2.549510
+38: 1.5 26.0 NA US 25.0 1.5 1015.3 NA
+39: 6.2 26.1 1013.7 US 20.0 3.6 1014.3 6.658078
+40: 2.6 29.0 NA US 24.0 1.5 1014.9 NA
+41: 2.1 24.0 NA US 29.0 3.1 1012.6 NA
+42: 4.6 22.2 1014.7 US 26.1 4.1 1012.1 4.713809
+43: 0.0 19.9 NA US 23.4 1.5 1015.2 NA
+44: 0.0 19.4 NA US 18.9 1.5 1014.5 NA
+45: 0.0 15.0 NA US 18.0 0.0 NA NA
+46: 0.0 22.2 1021.4 US 18.6 2.1 1014.6 7.975588
+47: 1.5 22.3 NA US 21.1 1.5 1015.7 NA
+48: NA NA NA US 18.3 3.6 1014.0 NA
+ wind.sp temp atm.press CTRY temp_med wind.sp_med atm.press_med euc
+ longitude latitude euc2
+ <num> <num> <num>
+ 1: -86.7509 32.5901 0.43707942
+ 2: -92.2992 34.7336 0.06056071
+ 3: -111.6250 34.2192 0.28848716
+ 4: -119.7730 36.5341 0.23953791
+ 5: -105.5130 38.6777 0.37231209
+ 6: -72.3573 41.5928 0.22867779
+ 7: -74.9841 38.6777 0.37507026
+ 8: -81.6850 27.8744 0.11962425
+ 9: -83.3736 32.3329 0.27286731
+10: -93.3714 41.9358 0.25367854
+11: -113.9300 43.5648 0.37101569
+12: -89.3776 40.0495 0.36837645
+13: -86.0808 40.0495 0.17120248
+14: -98.1156 38.4204 0.43637200
+15: -84.7674 37.3915 0.18651812
+16: -92.2724 30.6181 0.18351994
+17: -71.5800 42.3645 0.25716191
+18: -76.6459 39.2778 0.09711977
+19: -68.9801 45.6226 0.28822139
+20: -84.6870 43.1361 0.18590269
+21: -94.6043 46.3943 0.46631468
+22: -92.5137 38.3347 0.24129604
+23: -89.8065 32.6758 0.44676089
+24: -109.3200 46.8230 0.26428205
+25: -78.4686 35.4195 0.14470733
+26: -100.0990 47.2517 0.93058621
+27: -99.5898 41.3356 0.10655046
+28: -71.3924 43.3934 0.17828438
+29: -74.2336 39.9637 0.06844743
+30: -105.9420 34.4764 0.59003471
+31: -116.8510 39.1063 0.97520290
+32: -75.1449 43.1361 0.27978710
+33: -82.5963 40.2210 0.13723152
+34: -97.1239 35.5053 0.23328545
+35: -120.0680 43.9078 1.14737432
+36: -77.4500 40.9069 0.40317913
+37: -71.1244 41.5928 0.16892128
+38: -80.5056 33.6190 0.29832124
+39: -99.7238 44.3365 0.56296154
+40: -86.4560 35.6767 0.33840699
+41: -98.7857 31.3897 0.43870329
+42: -111.3300 39.1063 0.76451507
+43: -78.2005 37.5630 0.31373755
+44: -72.5450 44.2508 0.04979197
+45: -119.7460 47.4231 2.55749147
+46: -89.9941 44.5937 0.19889620
+47: -80.6665 38.4204 0.61422619
+48: -107.2560 43.0504 0.79671184
+ longitude latitude euc2
+
+
library (leaflet)
+ stationmap <- leaflet () |>
+ addProviderTiles ('CartoDB.Positron' ) |>
+ addCircles (
+ data = staterep,
+ lat = ~ lat, lng = ~ lon,
+ color = "yellow" ,
+ fillColor = "yellow" ,
+ opacity = 1 , fillOpacity = 1 , radius = 500
+ ) |>
+ addCircles (
+ data = centerstation,
+ lat = ~ lat, lng = ~ lon,
+ color = "purple" ,
+ fillColor = "purple" ,
+ opacity = 1 , fillOpacity = 1 , radius = 500
+ ) |>
+ addLegend ('bottomleft' ,
+ title = 'Monitoring Stations' ,
+ colors = c ("yellow" , "purple" ),
+ labels = c ("State Representative" , "Closest to Center" ),
+ opacity = 1 )
+
+ stationmap
+
+
+
+
+4. Means of means
+Using the quantile()
function, generate a summary table that shows the number of states included, average temperature, wind-speed, and atmospheric pressure by the variable “average temperature level,” which you’ll need to create.
+
+
statetemplevel <- met[,.(
+ temp_mean = mean (temp, na.rm = TRUE ),
+ wind.sp_mean = mean (wind.sp, na.rm = TRUE ),
+ atm.press_mean = mean (atm.press, na.rm = TRUE ),
+ temp, wind.sp, atm.press, USAFID
+ ), by = STATE]
+
+ statetemplevel[, templevel : = fifelse (temp_mean < 20 , "Low" ,
+ fifelse (temp_mean >= 20 & temp_mean < 25 , "Mid" , "High" ))]
+ statetemplevel
+
+
STATE temp_mean wind.sp_mean atm.press_mean temp wind.sp atm.press
+ <char> <num> <num> <num> <num> <num> <num>
+ 1: CA 22.36199 2.614120 1012.640 37.2 5.7 1009.9
+ 2: CA 22.36199 2.614120 1012.640 35.6 8.2 1010.3
+ 3: CA 22.36199 2.614120 1012.640 34.4 6.7 1010.6
+ 4: CA 22.36199 2.614120 1012.640 33.3 5.1 1011.6
+ 5: CA 22.36199 2.614120 1012.640 32.8 2.1 1012.7
+ ---
+2377339: MT 18.16680 3.426447 1014.095 30.6 3.1 1014.7
+2377340: MT 18.16680 3.426447 1014.095 31.1 0.0 1013.8
+2377341: MT 18.16680 3.426447 1014.095 31.1 5.1 1013.0
+2377342: MT 18.16680 3.426447 1014.095 31.7 4.6 1012.6
+2377343: MT 18.16680 3.426447 1014.095 31.1 5.1 1012.7
+ USAFID templevel
+ <int> <char>
+ 1: 690150 Mid
+ 2: 690150 Mid
+ 3: 690150 Mid
+ 4: 690150 Mid
+ 5: 690150 Mid
+ ---
+2377339: 726798 Low
+2377340: 726798 Low
+2377341: 726798 Low
+2377342: 726798 Low
+2377343: 726798 Low
+
+
summary <- statetemplevel %>%
+ group_by (templevel) %>%
+ summarise (
+ Entries = n (),
+ NA_entries = length (is.na (templevel)),
+ n_distinct (USAFID),
+ n_distinct (STATE),
+ temp_mean = mean (temp, na.rm = TRUE ),
+ wind.sp_mean = mean (wind.sp, na.rm = TRUE ),
+ atm.press_mean = mean (atm.press, na.rm = TRUE ),
+ )
+
+ summary
+
+
# A tibble: 3 × 8
+ templevel Entries NA_entries `n_distinct(USAFID)` `n_distinct(STATE)`
+ <chr> <int> <int> <int> <int>
+1 High 811126 811126 555 12
+2 Low 430794 430794 259 11
+3 Mid 1135423 1135423 781 25
+# ℹ 3 more variables: temp_mean <dbl>, wind.sp_mean <dbl>, atm.press_mean <dbl>
+
+
+
+
+