ww<-read.csv('west_nile_virus_cases.csv')
min(ww$Reported.Positive.Cases)
#| [1] 1Homework 2
Create a new script for this homework. Type your solutions to the problems below in the script.
Download the
west_nile_virus_cases.csvdata file from the CLE, and import it into R using RStudio’s drop-down menu. Make sure to select the From Text (base) option. Feel free to type any name that makes sense to you in the import tool’s Name field. In the import tool’s preview window (at the bottom right), make sure that the data look OK, and then select Import.The data are from the California Department of Public Health (and available, and documented, here). They contain the number of reported cases of West Nile Virus, by week and county, from 2006 through 2024.
Scroll through the data view (it should automatically pop up after the data import) to understand the data structure. You may close the tab when you are done. Also, use
head()andtail()to examine the data structure. Notice from the data view and the data glances that the data appear to be sorted by year, and then week, and then county.The data set omits the row if no cases were reported. For example, if no cases were reported in San Francisco during the 3rd week of 2021, there will be no corresponding row in the data.
Use
min(), a function that finds the smallest value, to confirm thatReported.Positive.Casesis never 0 (otherwise, the minimum value would be 0).TipSolutionUse
range()to find the smallest and largest values ofWeek. You may notice that no cases were reported during the first few weeks of the years. This is presumably because West Nile Virus is a seasonal disease.TipSolutionrange(ww$Week) #| [1] 9 53The
unique()function will return unique values. Use the function to find the unique counties in the data set. Recall that the numbering on the left can help you figure out the position of values in the output, or count how many values there are. Looking at the numbering, confirm that it appears that there are 50 unique counties in the data.TipSolutionunique(ww$County) #| [1] "Alameda" "Butte" "Colusa" "Contra Costa" #| [5] "El Dorado" "Fresno" "Glenn" "Imperial" #| [9] "Kern" "Kings" "Lake" "Los Angeles" #| [13] "Marin" "Merced" "Modoc" "Mono" #| [17] "Napa" "Nevada" "Orange" "Placer" #| [21] "Riverside" "Sacramento" "San Bernardino" "San Diego" #| [25] "San Joaquin" "San Luis Obispo" "Santa Clara" "Shasta" #| [29] "Solano" "Stanislaus" "Sutter" "Tehama" #| [33] "Tulare" "Ventura" "Yolo" "Yuba" #| [37] "Madera" "Mendocino" "Sonoma" "Calaveras" #| [41] "Santa Barbara" "Monterey" "San Francisco" "Amador" #| [45] "Santa Cruz" "Siskiyou" "Inyo" "Humboldt" #| [49] "Tuolumne" "San Mateo"The
length()function will report the number of values. So, we can combinelength()andunique()to properly count the number of unique values in the data, removing the possibility of human error. Specifically, if the data object is namedww(from the object assignment), we can use:length(unique(ww$County)). Confirm that this is in fact 50.TipSolutionlength(unique(ww$County)) #| [1] 50We can sort values using
sort(). Combinesort()andunique()to list the unique counties in alphabetical order. You can follow the pattern from above, replacinglengthwithsort.TipSolutionsort(unique(ww$County)) #| [1] "Alameda" "Amador" "Butte" "Calaveras" #| [5] "Colusa" "Contra Costa" "El Dorado" "Fresno" #| [9] "Glenn" "Humboldt" "Imperial" "Inyo" #| [13] "Kern" "Kings" "Lake" "Los Angeles" #| [17] "Madera" "Marin" "Mendocino" "Merced" #| [21] "Modoc" "Mono" "Monterey" "Napa" #| [25] "Nevada" "Orange" "Placer" "Riverside" #| [29] "Sacramento" "San Bernardino" "San Diego" "San Francisco" #| [33] "San Joaquin" "San Luis Obispo" "San Mateo" "Santa Barbara" #| [37] "Santa Clara" "Santa Cruz" "Shasta" "Siskiyou" #| [41] "Solano" "Sonoma" "Stanislaus" "Sutter" #| [45] "Tehama" "Tulare" "Tuolumne" "Ventura" #| [49] "Yolo" "Yuba"