Work for Assignment 2

Get help using code

?read_html
## No documentation for 'read_html' in specified packages and libraries:
## you could try '??read_html'
help(package = "dplyr")

vignette("dplyr")        # specific vignette
## starting httpd help server ... done
vignette(package="ggplot2")  # list all vignettes in ggplot2

Question 5: getSymbols() function

library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
?getSymbols

Question 6

getSymbols("MSFT", src="yahoo", from = "2024-10-01", to = "2025-02-01")
## [1] "MSFT"

Question 7

nrow(MSFT)
## [1] 84

Question 8

plot(MSFT$MSFT.Open, main="Microsoft Open Prices (2024-10-01 to 2025-01-31)", col="blue")

Question 10

library(httr)
library(jsonlite)
library(dplyr)
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
## # dplyr from breaking base R's lag() function.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Example: MLB schedule for Opening Day 2025
url <- "https://statsapi.mlb.com/api/v1/schedule?sportId=1&date=2025-04-01"
resp <- GET(url)
stop_for_status(resp)

# Parse JSON
txt <- content(resp, "text", encoding = "UTF-8")
dat <- fromJSON(txt, flatten = TRUE)

# Extract and tidy game info
games <- dat$dates$games[[1]] %>%
  select(gamePk, gameDate, teams.away.team.name, teams.home.team.name)

games
##    gamePk             gameDate teams.away.team.name teams.home.team.name
## 1  778498 2025-04-01T22:40:00Z        Texas Rangers      Cincinnati Reds
## 2  778497 2025-04-01T22:40:00Z        New York Mets        Miami Marlins
## 3  778493 2025-04-01T23:05:00Z   Pittsburgh Pirates       Tampa Bay Rays
## 4  778492 2025-04-01T23:05:00Z Arizona Diamondbacks     New York Yankees
## 5  778491 2025-04-01T23:07:00Z Washington Nationals    Toronto Blue Jays
## 6  778490 2025-04-01T23:40:00Z      Minnesota Twins    Chicago White Sox
## 7  778488 2025-04-01T23:40:00Z   Kansas City Royals    Milwaukee Brewers
## 8  778495 2025-04-01T23:45:00Z   Los Angeles Angels  St. Louis Cardinals
## 9  778489 2025-04-02T00:10:00Z San Francisco Giants       Houston Astros
## 10 778485 2025-04-02T01:40:00Z  Cleveland Guardians     San Diego Padres
## 11 778486 2025-04-02T01:40:00Z       Detroit Tigers     Seattle Mariners
## 12 778494 2025-04-02T02:05:00Z         Chicago Cubs            Athletics
## 13 778487 2025-04-02T02:10:00Z       Atlanta Braves  Los Angeles Dodgers