R Markdown

# Load the package 
library(wbstats) 

# Extract the metadeta 
meta <- wb_cache()
# Show the names of the sublists within the given meta list 
names(meta)
## [1] "countries"     "indicators"    "sources"       "topics"       
## [5] "regions"       "income_levels" "lending_types" "languages"
# Show one of the sublist: topics 
meta$topics
## # A tibble: 21 × 3
##    topic_id topic                           topic_desc                          
##       <dbl> <chr>                           <chr>                               
##  1        1 Agriculture & Rural Development "For the 70 percent of the world's …
##  2        2 Aid Effectiveness               "Aid effectiveness is the impact th…
##  3        3 Economy & Growth                "Economic growth is central to econ…
##  4        4 Education                       "Education is one of the most power…
##  5        5 Energy & Mining                 "The world economy needs ever-incre…
##  6        6 Environment                     "Natural and man-made environmental…
##  7        7 Financial Sector                "An economy's financial markets are…
##  8        8 Health                          "Improving health is central to the…
##  9        9 Infrastructure                  "Infrastructure helps determine the…
## 10       10 Social Protection & Labor       "The supply of labor available in a…
## # ℹ 11 more rows
# Show the top 6 rows for the indicators that contains the characters "gdp" 
gdp_vars <- wb_search("gdp")
head(gdp_vars)
## # A tibble: 6 × 3
##   indicator_id       indicator                                    indicator_desc
##   <chr>              <chr>                                        <chr>         
## 1 5.51.01.10.gdp     Per capita GDP growth                        GDP per capit…
## 2 6.0.GDP_current    GDP (current $)                              GDP is the su…
## 3 6.0.GDP_growth     GDP growth (annual %)                        Annual percen…
## 4 6.0.GDP_usd        GDP (constant 2005 $)                        GDP is the su…
## 5 6.0.GDPpc_constant GDP per capita, PPP (constant 2011 internat… GDP per capit…
## 6 BG.GSR.NFSV.GD.ZS  Trade in services (% of GDP)                 Trade in serv…
# Extract data for the above indicator "6.0.GDP_current" 
wb_data("6.0.GDP_current")
## # A tibble: 270 × 9
##    iso2c iso3c country    date `6.0.GDP_current` unit  obs_status footnote
##    <chr> <chr> <chr>     <dbl>             <dbl> <chr> <chr>      <chr>   
##  1 AR    ARG   Argentina  2000     284203750000  <NA>  <NA>       <NA>    
##  2 AR    ARG   Argentina  2001     268696750000  <NA>  <NA>       <NA>    
##  3 AR    ARG   Argentina  2002      97724513456. <NA>  <NA>       <NA>    
##  4 AR    ARG   Argentina  2003     127586251761. <NA>  <NA>       <NA>    
##  5 AR    ARG   Argentina  2004     181873056797. <NA>  <NA>       <NA>    
##  6 AR    ARG   Argentina  2005     220813784300. <NA>  <NA>       <NA>    
##  7 AR    ARG   Argentina  2006     262666517347. <NA>  <NA>       <NA>    
##  8 AR    ARG   Argentina  2007     329317513143. <NA>  <NA>       <NA>    
##  9 AR    ARG   Argentina  2008     403781994528. <NA>  <NA>       <NA>    
## 10 AR    ARG   Argentina  2009     376627876888. <NA>  <NA>       <NA>    
## # ℹ 260 more rows
## # ℹ 1 more variable: last_updated <date>
# Extract the total population and current US$ GDP for US and Norway 
pop_gdp <- wb_data(indicator = c("SP.POP.TOTL", "NY.GDP.MKTP.CD"), 
                   country = c("US", "NO"), 
                   start_date = 2020, end_date = 2024)

pop_gdp
## # A tibble: 10 × 6
##    iso2c iso3c country        date NY.GDP.MKTP.CD SP.POP.TOTL
##    <chr> <chr> <chr>         <dbl>          <dbl>       <dbl>
##  1 NO    NOR   Norway         2020        3.68e11     5379475
##  2 NO    NOR   Norway         2021        5.03e11     5408320
##  3 NO    NOR   Norway         2022        5.96e11     5457127
##  4 NO    NOR   Norway         2023        4.83e11     5519594
##  5 NO    NOR   Norway         2024        4.84e11     5572272
##  6 US    USA   United States  2020        2.14e13   331577720
##  7 US    USA   United States  2021        2.37e13   332099760
##  8 US    USA   United States  2022        2.60e13   334017321
##  9 US    USA   United States  2023        2.77e13   336806231
## 10 US    USA   United States  2024        2.92e13   340110988
# Load the package 
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(c("NVDA", "TSLA"), source = "yahoo")
## [1] "NVDA" "TSLA"
# Find the last 10 data points for NVDA 
tail(NVDA, 10)
##            NVDA.Open NVDA.High NVDA.Low NVDA.Close NVDA.Volume NVDA.Adjusted
## 2025-09-08    167.55    170.96   167.35     168.31   163769100      168.3005
## 2025-09-09    169.09    170.98   166.74     170.76   157548400      170.7504
## 2025-09-10    176.64    179.29   175.47     177.33   226852000      177.3200
## 2025-09-11    179.68    180.28   176.48     177.17   151159300      177.1700
## 2025-09-12    177.77    178.60   176.45     177.82   124911000      177.8200
## 2025-09-15    175.67    178.85   174.51     177.75   147061600      177.7500
## 2025-09-16    177.00    177.50   174.38     174.88   140737800      174.8800
## 2025-09-17    172.64    173.20   168.41     170.29   211843800      170.2900
## 2025-09-18    173.98    177.10   172.96     176.24   191763300      176.2400
## 2025-09-19    175.77    178.08   175.18     176.67   236658800      176.6700
# Question 6, Option A 
getSymbols("MSFT", src="yahoo",from = "2024-10-01", to = "2025-01-31")
## [1] "MSFT"
tail(MSFT)
##            MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted
## 2025-01-23    442.00    446.75   441.50     446.71    18389300      444.2688
## 2025-01-24    445.16    446.65   441.40     444.06    15549500      441.6333
## 2025-01-27    424.01    435.20   423.50     434.56    35647800      432.1852
## 2025-01-28    434.60    448.38   431.38     447.20    23491700      444.7561
## 2025-01-29    446.69    446.88   440.40     442.33    23581400      439.9127
## 2025-01-30    418.77    422.86   413.16     414.99    54586300      412.7221
# Question 6, Option B
getSymbols("MSFT", src="yahoo",from = "2024-10-01", to = "2025-02-01")
## [1] "MSFT"
tail(MSFT)
##            MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted
## 2025-01-24    445.16    446.65   441.40     444.06    15549500      441.6333
## 2025-01-27    424.01    435.20   423.50     434.56    35647800      432.1852
## 2025-01-28    434.60    448.38   431.38     447.20    23491700      444.7561
## 2025-01-29    446.69    446.88   440.40     442.33    23581400      439.9127
## 2025-01-30    418.77    422.86   413.16     414.99    54586300      412.7221
## 2025-01-31    418.98    420.69   414.91     415.06    34161900      412.7917
# Question 8 
plot(MSFT$MSFT.Open)

# FRED data pull 
getSymbols(c("FPCPITOTLZGCHN", "FPCPITOTLZGEGY", "FPCPITOTLZGUSA"), 
           src = "FRED")
## [1] "FPCPITOTLZGCHN" "FPCPITOTLZGEGY" "FPCPITOTLZGUSA"
tail(FPCPITOTLZGUSA)
##            FPCPITOTLZGUSA
## 2019-01-01       1.812210
## 2020-01-01       1.233584
## 2021-01-01       4.697859
## 2022-01-01       8.002800
## 2023-01-01       4.116338
## 2024-01-01       2.949525
# Use pacman to install/load necessary packages 
pacman:: p_load(httr, jsonlite)