code_to_joy
code_to_joy

Reputation: 623

Why do I get an "Object not found" error when using group_by and summarise() in r?

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

Upvotes: 5

Views: 17664

Answers (2)

code_to_joy
code_to_joy

Reputation: 623

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! :)

Upvotes: 3

Vishal Katti
Vishal Katti

Reputation: 652

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

Upvotes: 0

Related Questions