R problem opiod <- read.csv(\"https://data.ct.gov/api/views/rybz-nyjw/rows.csv?a
ID: 3755299 • Letter: R
Question
R problem
opiod <- read.csv("https://data.ct.gov/api/views/rybz-nyjw/rows.csv?accessType=DOWNLOAD",stringsAsFactors = FALSE)
Create a function named county. summary that has two arguments: df and county. The opiod data frame will be passed to the argument df , and a county in Connecticut will be passed to the county argument as a character. See below for some examples of county.summary in action. Your function, county.summary , should return a list with the following information: total deaths in the specified county, mean age of death in the specified county, median age of death in the specified county a demographics data frame for county deaths based on race and gender An idea of how the function should work: the county name passed into the function should filter the data frame based on the variable Death.County; compute the necessary information from the filtered data frame; return the results in a list and in the format you see below Include a check at the begining of your function that will give an error to the user if a county is entered that is not in Connecticut county . summary (df opiod, county = "LITCHFIELD") - stotal.deaths [1 187 $mean.age [1] 40.1016 $median.age [1 41 $demographics Female Male 1 Asian, Other Black Hispanic, White White 53 128 county.summary (opiod, "HARTFORD"Explanation / Answer
###Library for filtering data##
library(dplyr)
##For Supressing warnings##
options(warn=-1)
##Read the data##
opiod <- read.csv("https://data.ct.gov/api/views/rybz-nyjw/rows.csv?accessType=DOWNLOAD",stringsAsFactors = FALSE)
###Start the function##
county.summary <- function(df,county)
{
###Filter the data, based on county name##
data_sub<-filter(df, Death.County==county)
###Check if the county name is valid##
if(nrow(data_sub)>0)
{
###Create an empty dataset##
data_subset<-""
###Create Race wise Gender distribution##
data_subset1<-with(data_sub, table(Race, Sex))
###Count number of deaths##
data_subset$tot_death<-nrow(data_sub)
###Calculate Mean and Median##
data_subset$mean_age<-mean(data_sub$Age,na.rm = TRUE)
data_subset$median_age<-median(data_sub$Age,na.rm = TRUE)
###Print the output##
print(data_subset1)
print("Other fields")
data_subset
}
###Print if the county name is invalid##
else
{
print("Error,the county is not located in our database")
}
}
###Call the function##
county.summary(opiod,"HARTFORD")
####Output##
Sex
Race Female Male
0 1 2
Asian Indian 0 1 2
Asian, Other 0 1 3
Black 0 15 67
Hispanic, Black 0 1 3
Hispanic, White 1 24 132
Other 0 1 1
Unknown 0 1 3
White 0 212 551
[1] "Other fields"
[[1]]
[1] ""
$tot_death
[1] 1022
$mean_age
[1] 42.34868
$median_age
[1] 43
county.summary(opiod,"H")
###Output##
[1] "Error,the county is not located in our database"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.