I'm trying to create a data frame containing the number of shootings by state and race by using group_by and summarise, however I keep getting an "object not found" error referring to the column names I've included, even though they exist.
I've already tried restarting my session, restarting R, searching for answers by Googling, looking at related videos on YouTube including tutorials on group_by and summarise, and searching here on Stack Overflow and can't find anything that helps.
The version of RStudio I'm using is: Version 1.1.463
The code I've written that runs successfully is:
temp1 <- cleansed_data[, c("state", "race", "for_count")]
View(temp1)
The code that generates an error is:
temp2 <- temp1 %>% select(state, race, for_count) %>% group_by(state, race) %>% summarise(num_shootings = sum(for_count))
The error I see is:
Error in summarise(num_shootings = sum(for_count)) :
object 'for_count' not found
I'm expecting to get a 3 column data frame with columns state, race and num_shootings, with each row containing the sum of the for_count values for each combination of state and race.
But I just get the "object not found" error.
Additional info that my be useful is:
The output of:
dput(head(temp1))
is:
structure(list(state = c("IL", "PA", "FL", "IL", "CA", "PA"), race = c("Black", "White", "White", "Latino", "Unknown", "White"), for_count = c(1, 1, 1, 1, 1, 1)), row.names = c(NA, 6L), class = "data.frame")
The libraries I have loaded are:
tidyverse, operators, dplyr, ggplot2, knitr
I'm trying to create an RMarkdown HTML file.
The full RMarkdown file that this problem code is in is here:
https://github.com/foxnic/US-Mass-Shootings-Analysis/blob/master/Shootings_html_version.Rmd
...under:
## State & Race
dput
and then use the code which you have tried (temp1 %>% select(state, race, for_count) %>% group_by(state, race) %>% summarise(num_shootings = sum(for_count))
) it doesn't give me any error. – Ronak Shah