2
votes

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
2
What packages did you loaded to the enviroment?fran496
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")code_to_joy
The packages I've loaded are: tidyverse, operators, dplyr, ggplot2, knitrcode_to_joy
If I take your 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
Thanks for trying Ronak. Could my issue be related to the combination of packages I have loaded (tidyverse, operators, dplyr, ggplot2, knitr)? Or the fact that I'm trying to create an RMarkdown HTML document?code_to_joy

2 Answers

2
votes

From @mouli3c3 on twitter:

I know what caused the problem. Cant explain clearly why though. library(operators) is some how masking/changing the original behaviour of %>%. Adding library(magrittr) below librarary(operators) solved the problem. Let me know if it works.

It worked! :)

0
votes

Load only tidyverse and knitr. The other packages dplyr, ggplot2 and the %>% operator gets loaded automatically by tidyverse.