library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.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
table1
table2
table3
table4a
table1 %>% mutate(rate = (cases/population) * 10000)
table1 %>% count(year, wt = cases)
ggplot(data = table1 %>% mutate(rate = (cases/population) * 10000) , aes(x = year, y = rate)) +
geom_line(aes(group = country), color = 'gray')+
geom_point(aes(color = country))

# Select year, country and population, and
# turn the long dataframe into a wide one
#spread is a fxn in tidyr that has takes two arguments(key & value)
#key will be the column in the resulting dataframe we create
table1 %>% select(c(year,country,population))
table1 %>% select(c(year,country,population)) %>% spread(key = country, value = population)
# Select year, country and cases, and
# turn the long dataframe into a wide one
#spread is a fxn in tidyr that has takes two arguments(key & value)
#key will be the column in the resulting dataframe we create
table1 %>% select(c(year,country, cases))
table1 %>% mutate(rate = (cases/population) * 10000)
table1 %>% mutate(rate = (cases/population) * 10000) %>% select(c(year,country,rate)) %>% spread(key = country, value = rate)
# Create a wide dataframe
table1.wide = table1 %>% filter(!is.na(cases)) %>% mutate(rate = (cases/population) * 10000) %>% select(c(year,country,rate)) %>% spread(key = country, value = rate)
head(table1.wide)
# Convert wide dataframe to a long one
#The gather() is a fxn in Tidyr that converts a wide dataframe to a long one
head(table1.wide)
table1.wide %>% gather(key = 'country', 'rate', -year)
# Exporting data
write_csv(table1.wide, file = 'table1wide.csv')