Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use software R to solve Consider the built-in USArrests data frame. (a) D

ID: 3754408 • Letter: P

Question

Please use software R to solve

Consider the built-in USArrests data frame. (a) Determine the number of rows and columns for this data frame. (b) Calculate the median of each column of this data frame. (c) Find the average per capita murder rate (Murder) in regions where the percent age of the population living in urban areas (UrbanPop) exceeds 77%. Compare this with the average per capita murder rate where urban area population is less than 50%. (d) Construct a new data frame consisting of a random sample of 12 of the records of the USArrests data frame, where the records have been sampled without replacement.

Explanation / Answer

###Library functions to use filter operation##
library(dplyr)

### Import data###
data("USArrests")

### a) Get number of rows and columns####
dim(USArrests)

###Get number of columns##
ncol(USArrests)

##Get number of rows##
nrow(USArrests)

### b) Calculate median of every column##

apply(USArrests,2,median)


### c) Check average per capita murder rate##
### Filter population with urban percentage population > 77 ##

high_urban<-filter(USArrests,UrbanPop>77)

###Calculate the mean Murder rate for these##

high_urban_murder_rate<-mean(high_urban$Murder)

### Filter population with urban percentage population < 50 ##

low_urban<-filter(USArrests,UrbanPop<50)

###Calculate the mean Murder rate for these##

low_urban_murder_rate<-mean(low_urban$Murder)

#### High urban population's murder rate is 8.5 and bit better than low urban population's##
##murder rate which is 8.25##

###Without replacement, select 12 records and make a new dataframe##

USArrests1<-USArrests[sample(nrow(USArrests), 12, replace = FALSE),]