{include=FALSE}
knitr::opts_chunk$set(error = TRUE)
install.packages("tidyverse")
library(tidyverse)
candidate_level <- pres_elections %>%
filter(party_simplified != "OTHER") %>%
group_by(year, candidate, totalvotes, party_simplified candidatevotes) %>%
summarize(all_states_total = sum(candidatevotes))
## Error in parse(text = input): <text>:6:58: unexpected symbol
## 5: filter(party_simplified != "OTHER") %>%
## 6: group_by(year, candidate, totalvotes, party_simplified candidatevotes
## ^
setwd(“/Users/susan/Library/CloudStorage/Dropbox/Teaching/Political Science/POL280_researchDesign/2026_Spring”)
setwd("/Users/susan/Library/CloudStorage/Dropbox/Teaching/Political Science/POL280_researchDesign/2026_Spring")
## Error in setwd("/Users/susan/Library/CloudStorage/Dropbox/Teaching/Political Science/POL280_researchDesign/2026_Spring"): cannot change working directory
pres_elections <- read.csv(“1976-2020-president.csv”)
5 + 10
## [1] 15
fifteen <- 5+10 fifteen <- c(10,18,35,48,60)
ones <- (1,1,1,1,1)
fifteen + ones
View(pres_elections) # This command will allow you to see the data in a spreadsheet format.
View(pres_elections)
## Error: object 'pres_elections' not found
The unit of observation is “state_fips” since they are ranked in order 1-60 using this unit.
install.packages(“tidyverse”) library(tidyverse)
candidate_level <- pres_elections %>% filter(party_simplified != “OTHER”) %>% group_by(year, candidate, totalvotes, party_simplified candidatevotes) %>% summarize(all_states_total = sum(candidatevotes))
install.packages("tidyverse")
## Error in contrib.url(repos, "source"): trying to use CRAN without setting a mirror
library(tidyverse)
## Warning: package 'readr' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.5.1
## ✔ ggplot2 4.0.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
candidate_level <- pres_elections %>%
filter(party_simplified != "OTHER") %>%
group_by(year, candidate, totalvotes, party_simplified, candidatevotes) %>%
summarize(all_states_total = sum(candidatevotes))
## Error: object 'pres_elections' not found
I received an error message in return. # (b) Is this an error or a warning? Did your code run? The error message said that there is an unexpected symbol in: ” filter(party_simplified != “OTHER”) %>% group_by(year, candidate, totalvotes, party_simplified candidatevotes” No, my code did not run. # (c) What is the content of the bug? What is the location/position? The bug is probably due to the open parantheses without a closed parantheses in the second line of the error message. # (d) What do you think the solution might be? (Do your best! We will talk about it!) The solution might be adding a closed parantheses at the end of the line. Upon running the code a bunch of times with no luck, I have realized the issue is not a closed parantheses but the lack of a comma.
install.packages("tidyverse")
library(tidyverse)
candidate_level <- pres_elections %>%
filter(party_simplified != "OTHER") %>%
group_by(year, candidate, totalvotes, party_simplified candidatevotes) %>%
summarize(all_states_total = sum(candidatevotes)
## Error in parse(text = input): <text>:6:58: unexpected symbol
## 5: filter(party_simplified != "OTHER") %>%
## 6: group_by(year, candidate, totalvotes, party_simplified candidatevotes
## ^
install.packages("tidyverse")
library(tidyverse)
candidate_level <- pres_elections %>%
filter(party_simplified != "OTHER") %>%
group_by(year, candidate, totalvotes, partysimplified candidatevotes) %>%
summarize(all_states_total = sum(candidatevotes))
## Error in parse(text = input): <text>:6:57: unexpected symbol
## 5: filter(party_simplified != "OTHER") %>%
## 6: group_by(year, candidate, totalvotes, partysimplified candidatevotes
## ^
install.packages("tidyverse")
library("tidyverse"")
candidate_level <- pres_elections %>%
filter(party_simplified != "OTHER") %>%
group_by(year, candidate, totalvotes, party_simplified candidatevotes) %>%
summarize(all_states_total = sum(candidatevotes))
## Error in parse(text = input): <text>:2:20: unexpected string constant
## 4: candidate_level <- pres_elections %>%
## 5: filter(party_simplified != "
## ^
install.packages("tidyverse")
library("tidyverse"")
candidate_level <- pres_elections %>%
filter(party_simplified(!= "OTHER")) %>%
group_by(year, candidate, totalvotes, party_simplified candidatevotes) %>%
summarize(all_states_total = sum(candidatevotes))
## Error in parse(text = input): <text>:2:20: unexpected string constant
## 4: candidate_level <- pres_elections %>%
## 5: filter(party_simplified(!= "
## ^
install.packages("tidyverse")
## Error in contrib.url(repos, "source"): trying to use CRAN without setting a mirror
library(tidyverse)
candidate_level <- pres_elections %>%
filter(party_simplified != "OTHER") %>%
group_by(year, candidate, totalvotes, party_simplified, candidatevotes) %>%
summarize(all_states_total = sum(candidatevotes))
## Error: object 'pres_elections' not found
I think if this were between an error and warning this would be a warning since R states where the downloaded packages are but it also states that it has grouped out certain units and gives you a way to override. I would describe this as a suggestion.
View(candidate_level)
View(candidate_level)
## Error: object 'candidate_level' not found
We would need to add all values assocaited with each name in each year. I’m not exactly sure how that data frame would look, possibly adding candidate votes for columns 1-51, as an example for 1976 Jimmy Carter. We did not achieve our goal, as we were not provided any additional numbers.
new_df <- pres_elections %>% filter(party_simplified %in% c(“DEMOCRAT”, “REPUBLICAN”)) %>% mutate(candidate = na_if(candidate, ““)) %>% filter(!is.na(candidate)) %>% filter(writein == FALSE) %>% group_by(year, party_simplified, candidate) %>% summarize(all_states_total = sum(candidatevotes), national_vote_total = sum(totalvotes)) %>% pivot_wider( id_cols = c(year, national_vote_total), names_from = party_simplified, values_from = c(candidate, all_states_total), names_glue =”{party_simplified}_{.value}” )
new_df <- pres_elections %>%
filter(party_simplified %in% c("DEMOCRAT", "REPUBLICAN")) %>%
mutate(candidate = na_if(candidate, "")) %>%
filter(!is.na(candidate)) %>%
filter(writein == FALSE) %>%
group_by(year, party_simplified, candidate) %>%
summarize(all_states_total = sum(candidatevotes),
national_vote_total = sum(totalvotes)) %>%
pivot_wider(
id_cols = c(year, national_vote_total),
names_from = party_simplified,
values_from = c(candidate, all_states_total),
names_glue = "{party_simplified}_{.value}"
)
## Error: object 'pres_elections' not found
The unit of obsevation has changed to year and party. # (4) Let’s say that you wanted to calculate the proportion of the popular vote share won by each candidate in 2016 and 2020. In future classes, we will learn how to do this with the names of variables. But let’s do it by hand today. Use view() to open the data frame, then find the relevant vote totals. Calculate the percentage for each candidate below. For each line of code, please use narrative debugging. In other words, please use a # and write the purpose of each line of code next to the code itself. Include your answers below.
Calculating Hillary’s 2016 proportion of votes
65853514/128838342
## [1] 0.5111329
51.1%
Calculating Trump’s 2016 proportion of votes
62984828/128838342
## [1] 0.4888671
48.89%
Calculating Biden’s 2020 proportion of votes
81283501/155507476
## [1] 0.5226983
52.27%
Calculating Trump’s 2020 proportion of votes
74223975/155507476
## [1] 0.4773017
47.7%
2016 total votes: 128838342 2020 total votes: 155507476
Hillary 2016: 65853514 Trump 2016: 62984828 Biden 2020: 81283501 Trump 2020: 74223975 # Mini-R HW: Please complete Problem 4.3 and 4.4 (Sumner, Chapter 4, p. 51) and turn in this whole script on Canvas.
knitr::opts_chunk$set(error = TRUE)
5 + x
## Error: object 'x' not found
Will your code produce an output in the console? How do you know? Because an error message is the computer’s way of saying that it doesn’t understand what I’m asking it to do, the code will not produce an output in the console. I also did not define what the value of x is, so there is no way for the computer to produce an output. This is different from a warning message in which the computer would produce an output but would warn that it may not be what I’m looking for.
x = 2
4 + x
## [1] 6
Once we give a value for x, the code will produce an output.
sd(c(47,29,90,1@))
## Error in parse(text = input): <text>:1:17: unexpected ')'
## 1: sd(c(47,29,90,1@)
## ^
sd(c(47,29,90,1@)
## Error in parse(text = input): <text>:1:17: unexpected ')'
## 1: sd(c(47,29,90,1@)
## ^
sd(c(47,29,90,1@
## Error in parse(text = input): <text>:2:0: unexpected end of input
## 1: sd(c(47,29,90,1@
## ^
sdc(47,29,90,1)
## Error in sdc(47, 29, 90, 1): could not find function "sdc"
sd(c(47,29,90,1@))
## Error in parse(text = input): <text>:1:17: unexpected ')'
## 1: sd(c(47,29,90,1@)
## ^
sd(c(47,29,90,1@))
## Error in parse(text = input): <text>:1:17: unexpected ')'
## 1: sd(c(47,29,90,1@)
## ^
sd(c47,29,90,1@))
## Error in parse(text = input): <text>:1:16: unexpected ')'
## 1: sd(c47,29,90,1@)
## ^
sd(c(47,29,90,1))
## [1] 37.3218
Upon retrying the code without the ‘@’ symbol next to the fourth value, I’ve realized the error is the @ symbol instead of the closed parantheses.
With the @ symbol included, the computer does not calculate a standard deviation. It only returns with the error message.
The way to fix the error would be to take out the ‘@’ symbol and retry.