Last active
January 7, 2022 14:30
-
-
Save AntoineSoetewey/966d01bb2439ba47b1cc23ad22bb06b8 to your computer and use it in GitHub Desktop.
Data manipulation in R to plot active covid19 cases and Apple stock price. See comment in https://statsandr.com/blog/data-manipulation-in-r/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install.packages("quantmod") | |
library(quantmod) | |
# devtools::install_github("RamiKrispin/coronavirus", force = TRUE) | |
library(coronavirus) | |
# coronavirus dataset | |
`%>%` <- magrittr::`%>%` | |
df <- coronavirus %>% | |
# dplyr::filter(date == max(date)) %>% | |
dplyr::filter(country == "Indonesia") %>% | |
dplyr::group_by(country, type) %>% | |
dplyr::summarise(total = sum(cases)) %>% | |
tidyr::pivot_wider( | |
names_from = type, | |
values_from = total | |
) %>% | |
# dplyr::mutate(unrecovered = confirmed - ifelse(is.na(recovered), 0, recovered) - ifelse(is.na(death), 0, death)) %>% | |
dplyr::mutate(unrecovered = confirmed - ifelse(is.na(death), 0, death)) %>% | |
dplyr::arrange(-confirmed) %>% | |
dplyr::ungroup() %>% | |
dplyr::mutate(country = dplyr::if_else(country == "United Arab Emirates", "UAE", country)) %>% | |
dplyr::mutate(country = dplyr::if_else(country == "Mainland China", "China", country)) %>% | |
dplyr::mutate(country = dplyr::if_else(country == "North Macedonia", "N.Macedonia", country)) %>% | |
dplyr::mutate(country = trimws(country)) %>% | |
dplyr::mutate(country = factor(country, levels = country)) | |
df_daily <- coronavirus %>% | |
dplyr::filter(country == "Indonesia") %>% | |
dplyr::group_by(date, type) %>% | |
dplyr::summarise(total = sum(cases, na.rm = TRUE)) %>% | |
tidyr::pivot_wider( | |
names_from = type, | |
values_from = total | |
) %>% | |
dplyr::arrange(date) %>% | |
dplyr::ungroup() %>% | |
# dplyr::mutate(active = confirmed - death - recovered) %>% | |
dplyr::mutate(active = confirmed - death) %>% | |
dplyr::mutate( | |
confirmed_cum = cumsum(confirmed), | |
death_cum = cumsum(death), | |
# recovered_cum = cumsum(recovered), | |
active_cum = cumsum(active) | |
) | |
View(df_daily) | |
# stock dataset | |
getSymbols("AAPL", src = "yahoo") | |
View(AAPL) | |
str(AAPL) | |
AAPL <- as.data.frame(AAPL) | |
AAPL$date <- as.Date(rownames(AAPL)) | |
AAPL <- subset(AAPL, date >= min(df_daily$date)) | |
# create plot | |
plot( | |
x = AAPL$date, y = AAPL$AAPL.Close, ylim = c(0, max(df_daily$active)), | |
type = "l", ylab = NA, xlab = "Date", main = "Apple stock price and active Covid19 cases" | |
) | |
lines(x = df_daily$date, y = df_daily$active, col = "red") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment