# 1. Read the dataset and check the first seven rows
bigmac <- read.csv("big-mac-raw-index_Formatted.csv")
head(bigmac, 7)
##       date iso_a3 currency_code      name local_price   dollar_ex dollar_price
## 1 4/1/2000    ARG           ARS Argentina        2.50   1.0000000     2.500000
## 2 4/1/2000    AUS           AUD Australia        2.59   1.6800000     1.541667
## 3 4/1/2000    BRA           BRL    Brazil        2.95   1.7900000     1.648045
## 4 4/1/2000    GBR           GBP   Britain        1.90   0.6329114     3.002000
## 5 4/1/2000    CAN           CAD    Canada        2.85   1.4700000     1.938776
## 6 4/1/2000    CHL           CLP     Chile     1260.00 514.0000000     2.451362
## 7 4/1/2000    CHN           CNY     China        9.90   8.2800000     1.195652
##        USD      EUR      GBP      JPY     CNY Year_Formatted
## 1  0.11607  0.05007 -0.16722 -0.09864 1.09091          Y2000
## 2 -0.31176 -0.35246 -0.48645 -0.44416 0.28939          Y2000
## 3 -0.26427 -0.30778 -0.45102 -0.40581 0.37836          Y2000
## 4  0.34018  0.26092  0.00000  0.08235 1.51076          Y2000
## 5 -0.13448 -0.18566 -0.35417 -0.30099 0.62152          Y2000
## 6  0.09436  0.02964 -0.18342 -0.11618 1.05023          Y2000
## 7 -0.46623 -0.49779 -0.60171 -0.56891 0.00000          Y2000
# 2. Calculate summary statistics for dollar_price by year
summary_stats <- aggregate(dollar_price ~ Year_Formatted, data = bigmac, 
                           FUN = function(x) c(mean = mean(x), min = min(x), max = max(x)))
summary_stats
##    Year_Formatted dollar_price.mean dollar_price.min dollar_price.max
## 1           Y2000         2.0602601        1.1894737        3.5802469
## 2           Y2001         1.8804949        1.1729622        3.6416185
## 3           Y2002         2.0436859        0.7987220        4.0887850
## 4           Y2003         2.1450423        1.1956522        4.5985401
## 5           Y2004         2.2289945        0.6399659        5.1831628
## 6           Y2005         2.4874191        1.2686675        6.0628673
## 7           Y2006         2.5771867        1.3020672        7.0465234
## 8           Y2007         2.8222464        1.4149184        6.8800633
## 9           Y2008         3.4333239        1.6985794        7.8753335
## 10          Y2009         3.0888727        1.7160183        6.1451012
## 11          Y2010         3.3414435        1.8276762        7.2043226
## 12          Y2011         3.9203287        1.8918919        8.3117075
## 13          Y2012         3.6769645        1.5846168        7.9168268
## 14          Y2013         3.8757716        1.5005002        9.0810660
## 15          Y2014         3.8392883        1.5360983        7.7954348
## 16          Y2015         3.4238243        0.6700508        7.5436662
## 17          Y2016         3.2962137        0.6643227        6.5916236
## 18          Y2017         3.5258859        1.4645711        6.7416896
## 19          Y2018         3.5882339        1.6367752        6.7648436
## 20          Y2019         3.4619033        1.6520957        6.6215046
## 21          Y2020         3.5662031        1.8593492        6.9057105
## 22          Y2021         3.6818914        1.6818182        7.2906735
## 23          Y2022         3.7627851        1.7437918        6.9813651
## 24          Y2023         4.2489444        1.8428548        7.7291342
## 25          Y2024         4.4048045        2.2817158        8.1670213
# 3. Create a dataframe bigmac_USA filtering for USA and selecting year and local_price
bigmac_USA <- bigmac[bigmac$iso_a3 == "USA", c("Year_Formatted", "local_price")]
head(bigmac_USA, 5)
##     Year_Formatted local_price
## 28           Y2000        2.24
## 56           Y2001        2.24
## 88           Y2002        2.35
## 120          Y2003        2.46
## 159          Y2004        2.47
# 4. Rename local_price to US_price in bigmac_USA
colnames(bigmac_USA)[2] <- "US_price"
# 5. Merge bigmac and bigmac_USA using year
merged_bigmac <- merge(bigmac, bigmac_USA, by = "Year_Formatted")
head(merged_bigmac, 5)
##   Year_Formatted     date iso_a3 currency_code      name local_price dollar_ex
## 1          Y2000 4/1/2000    ARG           ARS Argentina        2.50 1.0000000
## 2          Y2000 4/1/2000    AUS           AUD Australia        2.59 1.6800000
## 3          Y2000 4/1/2000    BRA           BRL    Brazil        2.95 1.7900000
## 4          Y2000 4/1/2000    GBR           GBP   Britain        1.90 0.6329114
## 5          Y2000 4/1/2000    CAN           CAD    Canada        2.85 1.4700000
##   dollar_price      USD      EUR      GBP      JPY     CNY US_price
## 1     2.500000  0.11607  0.05007 -0.16722 -0.09864 1.09091     2.24
## 2     1.541667 -0.31176 -0.35246 -0.48645 -0.44416 0.28939     2.24
## 3     1.648045 -0.26427 -0.30778 -0.45102 -0.40581 0.37836     2.24
## 4     3.002000  0.34018  0.26092  0.00000  0.08235 1.51076     2.24
## 5     1.938776 -0.13448 -0.18566 -0.35417 -0.30099 0.62152     2.24
# 6. Sort the merged data by country name
sorted_bigmac <- merged_bigmac[order(merged_bigmac$name), ]
# 7. Create a dummy variable undervalued (1 if local_price < US_price)
merged_bigmac$undervalued <- ifelse(merged_bigmac$local_price < merged_bigmac$US_price, 1, 0)
# 8. Present a frequency table for undervalued
table(merged_bigmac$undervalued)
## 
##    0    1 
## 3086  268
# 9. Custom question: Find the country with the highest local_price in each year
highest_price <- aggregate(local_price ~ Year_Formatted, data = bigmac, 
                           FUN = function(x) bigmac$name[which.max(x)])
highest_price
##    Year_Formatted    local_price
## 1           Y2000      Indonesia
## 2           Y2001      Indonesia
## 3           Y2002         Brazil
## 4           Y2003      Australia
## 5           Y2004 Czech Republic
## 6           Y2005       Malaysia
## 7           Y2006         Israel
## 8           Y2007       Malaysia
## 9           Y2008       Malaysia
## 10          Y2009       Malaysia
## 11          Y2010      Argentina
## 12          Y2011         Mexico
## 13          Y2012         Brazil
## 14          Y2013         Mexico
## 15          Y2014         Turkey
## 16          Y2015         Mexico
## 17          Y2016         Mexico
## 18          Y2017         Mexico
## 19          Y2018       Malaysia
## 20          Y2019       Thailand
## 21          Y2020       Thailand
## 22          Y2021         Poland
## 23          Y2022   South Africa
## 24          Y2023   South Africa
## 25          Y2024   South Africa

#I struggled with this assignment more than past assignments and labs. It seemed there was a lot more room for errors to appear.It took me about 3x longer than past assignments. I will admit I had to use chatgpt to help me get past the errors. I tried my best to solve them and I was able to sort out a few, but there were some that I could not figure out for the life of me. I think my coding ability is growing stronger, especially after the challenges I faced with this assignment. I also included some comments in my code blocks to make it more organized.