Help me list the names of states from this data set.

So I was supposed to create a function to get a crime name (w/o using readline), print the average rate for that crime and then name the states with more than the average rate. I got the the print the crime and average rate but I cannot list the names of states with more than the average. I put down something but it is only listing the number of the state with TRUE or FALSE.
Here' s my code:

f_high_crime_rate_states <- function(){
x <- readline(prompt = "Enter a crime name: ")
a <- mean(FBI_Data$Murder)
b <- mean(FBI_Data$Rape)
c <- mean(FBI_Data$Robbery)
d <- mean(FBI_Data$Assault)
e <- mean(FBI_Data$PropertyCrime)
f <- mean(FBI_Data$Burglary)
g <- mean(FBI_Data$VehicleTheft)
h <- mean(FBI_Data$LarcenyTheft)
i <- mean(FBI_Data$ViolentCrime)
if(x == "Murder"){
paste("The rate is: ",a,
"The states with more than average include: ", (a < FBI_Data$Murder))
}else if(x == "Violent Crime"){
paste("The rate is: ",i,
"The states with more than average include: ", (i < FBI_Data$ViolentCrime))
}else if(x == "Rape"){
paste("The rate is: ", b,
"The states with more than average include: ", (b < FBI_Data$Rape))
}else if(x == "Robbery"){
paste("The rate is: ", c,
"The states with more than average include: ", (c < FBI_Data$Robbery))
}else if(x == "Assault"){
paste("The rate is: ", d,
"The states with more than average include: " (d < FBI_Data$Assault))
}else if(x == "Property crime"){
paste("The rate is: ", e,
"The states with more than average include: ", (e < FBI_Data$PropertyCrime))
}else if(x == "Burglary"){
paste("The rate is: ", f,
"The states with more than average include: ", (f < FBI_Data$Burglary))
}else if(x == "Vehicle theft"){
paste("The rate is: ", g,
"The states with more than average include: ", (g < FBI_Data$VehicleTheft))
}else{
paste("The rate is: ", h,
"The states with more than average include: ", (h < FBI_Data$LarcenyTheft))}

}
f_high_crime_rate_states()

And here's a picture of the data set:

#as an example table:
FBI_Data <- data.frame(ViolentCrime=c(200,100,150,175,222), Murder=c(2,3,8,5,1), row.names=c("Conneticut", "Maine", "Vermont", "NewYork", "Indiana"))

#extracting the mean of a certain column, in this case Murder (as you already did in your example)
a <- mean(FBI_Data$Murder)

#making a filtered table, containing only those rows with a Murder value above the variable a. The second way is better for your function as it allows you to replace "Murder" with your variable
filteredRows <- subset(FBI_Data, Murder > a)
#OR
filteredRows <- FBI_Data[FBI_Data[ , "Murder"] > a , ]

#giving out the rownames of this filtered table
rownames(filteredRows)

#============================================
#your specific function might look like this:
f_high_crime_rate_states <- function(crimeName){
a <- mean(FBI_Data[,crimeName])
filteredRows <- FBI_Data[FBI_Data[ , crimeName] > a , ]
rownames(filteredRows)
}
f_high_crime_rate_states("Murder")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.