[1] 4
September 22, 2024
+
addition-
subtraction*
multiplication/
division^
exponentiation (also **
)mean()
function to calculate the average of a set of numbersc()
to create a vector of numbersCreate a vector of numbers and take the mean…
mean()
calculates the mean of a set of numbersmedian()
calculates the median of a set of numberssd()
calculates the standard deviation of a set of numberssum()
calculates the sum of a set of numberslength()
calculates the length of a vectormax()
and min()
calculate the maximum and minimum values of a vectorround()
rounds a number to a specified number of decimal placessqrt()
calculates the square root of a numberlog()
calculates the natural logarithm of a numberexp()
calculates the exponential of a numberabs()
calculates the absolute value of a numberread_csv()
is a functionggplot()
is a functionreadr
is a package that contains the read_csv()
functionggplot2
is a package that contains the ggplot()
functioninstall.packages()
to install packageslibrary()
to load packagesggplot2
for data visualizationreadr
for reading datadplyr
for data manipulationtidyr
for data tidyinglibrary(ggplot2)
library(tidyverse)
<-
<-
, and the data or value you wish to assign to the object is on the right05:00
Let’s open up the Getting Started module on Posit Cloud…
And work through the examples there.
05:00
# Load the packages
library(vdemlite)
library(ggplot2)
# Use vdemlite to extract democracy scores for France and INdia
dem_data <- fetchdem(indicators = "v2x_polyarchy",
countries = c("FRA", "IND"))
# And now we can plot the data
ggplot(dem_data, aes(y = v2x_polyarchy, x = year, color=country_name)) +
geom_line() +
theme_minimal() +
xlab("Year") +
ylab("Electoral Democracy Index") +
ggtitle("Electoral Democracy, 1970-2022") +
geom_hline(yintercept = .5, linetype = "dashed", color = "grey") +
scale_color_manual(name="Country", values=c("#E69F00", "#56B4E9")) +
ylim(0, 1)
05:00
library(tidyverse)
library(unvotes)
library(lubridate)
library(scales)
library(pacman)
unvotes <- un_votes %>%
inner_join(un_roll_calls, by = "rcid") %>%
inner_join(un_roll_call_issues, by = "rcid")
unvotes %>%
# then filter out to only include the countries we want
filter(country %in% c("South Africa", "United States", "France")) %>%
# then make sure R understands the the year variable is a data
mutate(year = year(date)) %>%
# Then group the data by country and year
group_by(country, year, issue) %>%
# then take the average Yes votes for each country_year
summarize(percent_yes = mean(vote == "yes")) %>%
# then make a nice plot
ggplot(mapping = aes(x = year, y = percent_yes, color = country)) +
geom_point(alpha = 0.4) +
geom_smooth(method = "loess", se = FALSE) +
facet_wrap(~issue) +
scale_y_continuous(labels = percent) +
scale_color_manual( values=c("#E69F00", "#56B4E9", "#009E73")) +
labs(
title = "Percentage of 'Yes' votes in the UN General Assembly",
subtitle = "1946 to 2019",
y = "% Yes",
x = "Year",
color = "Country"
)
05:00