Question 1:

Import data from both statistical package and database management system

Generate and save data in statistical package :

library(haven)
product_data <- data.frame(product_id=c(1,2,3), product_name=c("Headset","Phone","Laptop"), price =c(100,200,"300"))
write_sav(product_data, "product_data.sav")

Import data from Statistical Package:

getwd()
## [1] "/Volumes/Robert/Robert/Master's/R"
st_pac<- read_sav("product_data.sav")
print(st_pac)
## # A tibble: 3 × 3
##   product_id product_name price
##        <dbl> <chr>        <chr>
## 1          1 Headset      100  
## 2          2 Phone        200  
## 3          3 Laptop       300

Data from DBMS

library(RSQLite)
library(DBI)
library(RMySQL)
## 
## Attaching package: 'RMySQL'
## The following object is masked from 'package:RSQLite':
## 
##     isIdCurrent
connect<- dbConnect(RSQLite::SQLite(),"product_database.db")
dbWriteTable(connect,"product_table", product_data, overwrite = TRUE)

Importing data from DBMS:

data_from_db <- dbReadTable(connect,"product_table")
print(data_from_db)
##   product_id product_name price
## 1          1      Headset   100
## 2          2        Phone   200
## 3          3       Laptop   300

Question 2

Merging Datasets by 2 to 3 variables

We are going to merge Data from CO2 Emission and World Population Datasets that you gave us in exercises in R by using Country Vaiable to get Country names, Continents and Population in 2022 and CO2 Emmission in 2019

Select needed column

World_population_data <- read.csv("world_population.csv")
head(World_population_data)
##   Rank CCA3 Country.Territory          Capital Continent X2022.Population
## 1   36  AFG       Afghanistan            Kabul      Asia         41128771
## 2  138  ALB           Albania           Tirana    Europe          2842321
## 3   34  DZA           Algeria          Algiers    Africa         44903225
## 4  213  ASM    American Samoa        Pago Pago   Oceania            44273
## 5  203  AND           Andorra Andorra la Vella    Europe            79824
## 6   42  AGO            Angola           Luanda    Africa         35588987
##   X2020.Population X2015.Population X2010.Population X2000.Population
## 1         38972230         33753499         28189672         19542982
## 2          2866849          2882481          2913399          3182021
## 3         43451666         39543154         35856344         30774621
## 4            46189            51368            54849            58230
## 5            77700            71746            71519            66097
## 6         33428485         28127721         23364185         16394062
##   X1990.Population X1980.Population X1970.Population Area..km..
## 1         10694796         12486631         10752971     652230
## 2          3295066          2941651          2324731      28748
## 3         25518074         18739378         13795915    2381741
## 4            47818            32886            27075        199
## 5            53569            35611            19860        468
## 6         11828638          8330047          6029700    1246700
##   Density..per.km.. Growth.Rate World.Population.Percentage
## 1           63.0587      1.0257                        0.52
## 2           98.8702      0.9957                        0.04
## 3           18.8531      1.0164                        0.56
## 4          222.4774      0.9831                        0.00
## 5          170.5641      1.0100                        0.00
## 6           28.5466      1.0315                        0.45
CO2_emission <- read.csv("CO2_emission.csv")

head(CO2_emission)
##           Country.Name country_code                     Region
## 1                Aruba          ABW  Latin America & Caribbean
## 2          Afghanistan          AFG                 South Asia
## 3               Angola          AGO         Sub-Saharan Africa
## 4              Albania          ALB      Europe & Central Asia
## 5              Andorra          AND      Europe & Central Asia
## 6 United Arab Emirates          ARE Middle East & North Africa
##                           Indicator.Name      X1990      X1991       X1992
## 1 CO2 emissions (metric tons per capita)         NA         NA          NA
## 2 CO2 emissions (metric tons per capita)  0.1917451  0.1676816  0.09595774
## 3 CO2 emissions (metric tons per capita)  0.5536620  0.5445386  0.54355722
## 4 CO2 emissions (metric tons per capita)  1.8195416  1.2428102  0.68369983
## 5 CO2 emissions (metric tons per capita)  7.5218317  7.2353792  6.96307870
## 6 CO2 emissions (metric tons per capita) 30.1951886 31.7784962 29.08092584
##         X1993       X1994       X1995       X1996       X1997       X1998
## 1          NA          NA          NA          NA          NA          NA
## 2  0.08472111  0.07554583  0.06846796  0.06258803  0.05682662  0.05269086
## 3  0.70898423  0.83680440  0.91214149  1.07216847  1.08663697  1.09182531
## 4  0.63830704  0.64535519  0.60543625  0.61236736  0.46692147  0.57215370
## 5  6.72417752  6.54157891  6.73347949  6.99159455  7.30744115  7.63953851
## 6 29.27567777 30.84933296 31.12501806 30.92802588 30.48633262 29.66358052
##         X1999      X2000       X2001       X2002       X2003       X2004
## 1          NA         NA          NA          NA          NA          NA
## 2  0.04015697  0.0365737  0.03378536  0.04557366  0.05151838  0.04165539
## 3  1.10985966  0.9880774  0.94182891  0.89557767  0.92486944  0.93026295
## 4  0.95535931  1.0262131  1.05549588  1.23237878  1.33898498  1.40405869
## 5  7.92319165  7.9522863  7.72154906  7.56623988  7.24241557  7.34426233
## 6 28.88710798 27.0351591 29.43026994 28.50146173 27.96926982 27.03893822
##         X2005       X2006       X2007      X2008      X2009      X2010
## 1          NA          NA          NA         NA         NA         NA
## 2  0.06041878  0.06658329  0.06531235  0.1284166  0.1718624  0.2436140
## 3  0.81353929  0.82184008  0.81175351  0.8886580  0.9394040  0.9761842
## 4  1.33820940  1.33999574  1.39393137  1.3843112  1.4414936  1.5276237
## 5  7.35378001  6.79054277  6.53104692  6.4393039  6.1566875  6.1571978
## 6 25.38238104 22.93510429 21.37028576 22.0114692 19.8323489 19.0397698
##        X2011      X2012      X2013      X2014      X2015      X2016      X2017
## 1         NA         NA         NA         NA         NA         NA         NA
## 2  0.2965062  0.2592953  0.1856237  0.1462356  0.1728967  0.1497893  0.1316946
## 3  0.9855223  0.9506959  1.0362939  1.0997791  1.1350441  1.0318113  0.8133007
## 4  1.6694232  1.5032405  1.5336300  1.6683374  1.6037751  1.5576644  1.7887861
## 5  5.8508861  5.9446542  5.9428004  5.8071277  6.0261818  6.0806003  6.1041339
## 6 18.5094574 19.2078011 20.0556476 20.0516980 21.0776420 21.4806686 20.7690223
##        X2018      X2019    X2019.1
## 1         NA         NA         NA
## 2  0.1632953  0.1598244  0.1598244
## 3  0.7776749  0.7921371  0.7921371
## 4  1.7827389  1.6922483  1.6922483
## 5  6.3629754  6.4812174  6.4812174
## 6 18.3906781 19.3295633 19.3295633
pop_data <- World_population_data[, c(
  "Country.Territory",
  "Continent",
  "X2022.Population"
)]

co2_data <- CO2_emission[, c(
  "Country.Name",
  "X2019"
)]

Merge Data by country

merged_data <- merge(
  pop_data,
  co2_data,
  by.x = "Country.Territory",
  by.y = "Country.Name"
)

head(merged_data)
##   Country.Territory Continent X2022.Population     X2019
## 1       Afghanistan      Asia         41128771 0.1598244
## 2           Albania    Europe          2842321 1.6922483
## 3           Algeria    Africa         44903225 3.9776505
## 4    American Samoa   Oceania            44273        NA
## 5           Andorra    Europe            79824 6.4812174
## 6            Angola    Africa         35588987 0.7921371

Question 3 using groupby, select(), filter(), arrange(), rename(), mutate(), and %>% functions

%>% means pipe operator for Tidyverse package with is used to make the result on the left and pass it into the function on the right.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.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
CO2_emission %>% head()
##           Country.Name country_code                     Region
## 1                Aruba          ABW  Latin America & Caribbean
## 2          Afghanistan          AFG                 South Asia
## 3               Angola          AGO         Sub-Saharan Africa
## 4              Albania          ALB      Europe & Central Asia
## 5              Andorra          AND      Europe & Central Asia
## 6 United Arab Emirates          ARE Middle East & North Africa
##                           Indicator.Name      X1990      X1991       X1992
## 1 CO2 emissions (metric tons per capita)         NA         NA          NA
## 2 CO2 emissions (metric tons per capita)  0.1917451  0.1676816  0.09595774
## 3 CO2 emissions (metric tons per capita)  0.5536620  0.5445386  0.54355722
## 4 CO2 emissions (metric tons per capita)  1.8195416  1.2428102  0.68369983
## 5 CO2 emissions (metric tons per capita)  7.5218317  7.2353792  6.96307870
## 6 CO2 emissions (metric tons per capita) 30.1951886 31.7784962 29.08092584
##         X1993       X1994       X1995       X1996       X1997       X1998
## 1          NA          NA          NA          NA          NA          NA
## 2  0.08472111  0.07554583  0.06846796  0.06258803  0.05682662  0.05269086
## 3  0.70898423  0.83680440  0.91214149  1.07216847  1.08663697  1.09182531
## 4  0.63830704  0.64535519  0.60543625  0.61236736  0.46692147  0.57215370
## 5  6.72417752  6.54157891  6.73347949  6.99159455  7.30744115  7.63953851
## 6 29.27567777 30.84933296 31.12501806 30.92802588 30.48633262 29.66358052
##         X1999      X2000       X2001       X2002       X2003       X2004
## 1          NA         NA          NA          NA          NA          NA
## 2  0.04015697  0.0365737  0.03378536  0.04557366  0.05151838  0.04165539
## 3  1.10985966  0.9880774  0.94182891  0.89557767  0.92486944  0.93026295
## 4  0.95535931  1.0262131  1.05549588  1.23237878  1.33898498  1.40405869
## 5  7.92319165  7.9522863  7.72154906  7.56623988  7.24241557  7.34426233
## 6 28.88710798 27.0351591 29.43026994 28.50146173 27.96926982 27.03893822
##         X2005       X2006       X2007      X2008      X2009      X2010
## 1          NA          NA          NA         NA         NA         NA
## 2  0.06041878  0.06658329  0.06531235  0.1284166  0.1718624  0.2436140
## 3  0.81353929  0.82184008  0.81175351  0.8886580  0.9394040  0.9761842
## 4  1.33820940  1.33999574  1.39393137  1.3843112  1.4414936  1.5276237
## 5  7.35378001  6.79054277  6.53104692  6.4393039  6.1566875  6.1571978
## 6 25.38238104 22.93510429 21.37028576 22.0114692 19.8323489 19.0397698
##        X2011      X2012      X2013      X2014      X2015      X2016      X2017
## 1         NA         NA         NA         NA         NA         NA         NA
## 2  0.2965062  0.2592953  0.1856237  0.1462356  0.1728967  0.1497893  0.1316946
## 3  0.9855223  0.9506959  1.0362939  1.0997791  1.1350441  1.0318113  0.8133007
## 4  1.6694232  1.5032405  1.5336300  1.6683374  1.6037751  1.5576644  1.7887861
## 5  5.8508861  5.9446542  5.9428004  5.8071277  6.0261818  6.0806003  6.1041339
## 6 18.5094574 19.2078011 20.0556476 20.0516980 21.0776420 21.4806686 20.7690223
##        X2018      X2019    X2019.1
## 1         NA         NA         NA
## 2  0.1632953  0.1598244  0.1598244
## 3  0.7776749  0.7921371  0.7921371
## 4  1.7827389  1.6922483  1.6922483
## 5  6.3629754  6.4812174  6.4812174
## 6 18.3906781 19.3295633 19.3295633

Select() Chooses specific columns from a dataset.

Select<- CO2_emission %>%
            select(X2019)

head(Select)
##        X2019
## 1         NA
## 2  0.1598244
## 3  0.7921371
## 4  1.6922483
## 5  6.4812174
## 6 19.3295633

filter() Keeps only rows that match a condition.

filter<- CO2_emission %>%
      filter(X2019 > 10)
head(filter)
##           Country.Name country_code                     Region
## 1 United Arab Emirates          ARE Middle East & North Africa
## 2            Australia          AUS        East Asia & Pacific
## 3              Bahrain          BHR Middle East & North Africa
## 4    Brunei Darussalam          BRN        East Asia & Pacific
## 5               Canada          CAN              North America
## 6           Kazakhstan          KAZ      Europe & Central Asia
##                           Indicator.Name    X1990    X1991    X1992    X1993
## 1 CO2 emissions (metric tons per capita) 30.19519 31.77850 29.08093 29.27568
## 2 CO2 emissions (metric tons per capita) 15.44849 15.31821 15.34153 15.45537
## 3 CO2 emissions (metric tons per capita) 21.65641 20.30359 23.45713 24.37469
## 4 CO2 emissions (metric tons per capita) 12.60079 12.69684 13.10755 13.95180
## 5 CO2 emissions (metric tons per capita) 15.14889 14.74101 15.02823 14.71339
## 6 CO2 emissions (metric tons per capita) 14.51248 14.97534 15.44124 13.38407
##      X1994    X1995     X1996     X1997     X1998     X1999    X2000     X2001
## 1 30.84933 31.12502 30.928026 30.486333 29.663581 28.887108 27.03516 29.430270
## 2 15.69196 16.05688 16.427830 16.625263 17.562931 17.632358 17.72307 17.804564
## 3 24.32723 24.01995 24.453004 24.238268 25.240469 24.271845 23.89371 23.496524
## 4 14.75201 15.48238 15.855820 16.764862 14.163285 13.580043 14.16711 13.851434
## 5 15.06035 15.29060 15.592590 15.943985 16.076512 16.258523 16.75763 16.331569
## 6 12.33574 11.07975  9.846435  8.623745  9.012954  8.215898  8.07263  7.903981
##       X2002    X2003    X2004    X2005    X2006    X2007    X2008    X2009
## 1 28.501462 27.96927 27.03894 25.38238 22.93510 21.37029 22.01147 19.83235
## 2 17.981925 17.72168 18.17473 18.14629 18.14145 18.52110 18.30375 18.22310
## 3 23.424109 23.03816 21.60642 23.26923 23.46563 22.20240 22.21335 20.85944
## 4 13.322683 15.62434 14.18897 13.66704 20.30994 19.04168 20.84772 20.50002
## 5 16.720301 17.20830 16.79427 17.02749 16.59535 17.38057 16.55692 15.50215
## 6  8.820274  9.80212 10.52622 11.17117 12.10471 12.81242 15.34075 13.27362
##      X2010    X2011    X2012    X2013    X2014    X2015    X2016    X2017
## 1 19.03977 18.50946 19.20780 20.05565 20.05170 21.07764 21.48067 20.76902
## 2 17.59007 17.29542 17.02204 16.44232 15.83042 15.86329 15.91466 15.81832
## 3 20.92897 20.35750 20.90093 21.93868 22.66343 22.29102 21.25133 20.42733
## 4 18.44924 18.61885 18.29588 17.82827 17.27765 15.42488 16.60350 17.17391
## 5 15.79214 15.99594 15.73447 15.83846 15.84991 15.64859 15.42060 15.54457
## 6 14.07314 14.82436 14.56638 15.26279 12.10241 10.87226 11.36054 11.89615
##      X2018    X2019  X2019.1
## 1 18.39068 19.32956 19.32956
## 2 15.49353 15.23827 15.23827
## 3 19.63121 20.26610 20.26610
## 4 17.57740 16.13216 16.13216
## 5 15.65058 15.43061 15.43061
## 6 11.85132 11.45694 11.45694

arrange() is used to sort data (ascending or descending).

arrange<- CO2_emission %>%
        arrange(X2019)
head(arrange)
##               Country.Name country_code             Region
## 1         Congo, Dem. Rep.          COD Sub-Saharan Africa
## 2                  Somalia          SOM Sub-Saharan Africa
## 3 Central African Republic          CAF Sub-Saharan Africa
## 4                  Burundi          BDI Sub-Saharan Africa
## 5                   Malawi          MWI Sub-Saharan Africa
## 6                    Niger          NER Sub-Saharan Africa
##                           Indicator.Name      X1990      X1991      X1992
## 1 CO2 emissions (metric tons per capita) 0.09187559 0.04845684 0.03964224
## 2 CO2 emissions (metric tons per capita) 0.10103682 0.09760757 0.09320968
## 3 CO2 emissions (metric tons per capita) 0.05344278 0.04863632 0.05068876
## 4 CO2 emissions (metric tons per capita) 0.03125598 0.03953334 0.03165910
## 5 CO2 emissions (metric tons per capita) 0.05848265 0.06666416 0.06504250
## 6 CO2 emissions (metric tons per capita) 0.07101395 0.06514863 0.05836503
##        X1993      X1994      X1995      X1996      X1997      X1998      X1999
## 1 0.04894909 0.05490278 0.05075014 0.05309042 0.05179433 0.05083616 0.04551432
## 2 0.08748112 0.08545163 0.08008920 0.07419283 0.06930337 0.06314462 0.05845495
## 3 0.05252535 0.05103641 0.04964840 0.05138692 0.05011154 0.06617781 0.06745326
## 4 0.03276962 0.03220905 0.03340547 0.03465284 0.03430179 0.03556668 0.03669940
## 5 0.07414780 0.07593096 0.07618531 0.07383179 0.07501286 0.07107425 0.07278205
## 6 0.06433193 0.05998921 0.05584656 0.06207641 0.06091453 0.06353288 0.05946978
##        X2000      X2001      X2002      X2003      X2004      X2005      X2006
## 1 0.03460294 0.03241890 0.03308491 0.03889115 0.03655627 0.04033885 0.04029832
## 2 0.05522838 0.05551492 0.05999157 0.05909074 0.05725426 0.05551909 0.05388379
## 3 0.06592644 0.06716790 0.06575264 0.05926025 0.05808253 0.05447729 0.05585134
## 4 0.04075956 0.03064878 0.03132403 0.02315766 0.02103289 0.02036699 0.02365977
## 5 0.06368426 0.05948215 0.05805187 0.06249905 0.06259228 0.06019349 0.06012166
## 6 0.05912689 0.05701466 0.05742417 0.06008863 0.06018629 0.05578197 0.05302613
##        X2007      X2008      X2009      X2010      X2011      X2012      X2013
## 1 0.04225567 0.04221072 0.04003294 0.04104464 0.04523995 0.04100216 0.05549420
## 2 0.05595606 0.05439938 0.05205804 0.05230870 0.05090372 0.04954588 0.04975615
## 3 0.05717002 0.03978127 0.03919197 0.03875293 0.04299966 0.04508149 0.02697875
## 4 0.02289428 0.02215084 0.02143454 0.03457972 0.04018572 0.04001734 0.04087921
## 5 0.06221046 0.06993058 0.06936501 0.06121210 0.06215698 0.06170430 0.06187147
## 6 0.05583776 0.05639007 0.06816835 0.08321173 0.08238498 0.10620836 0.10646182
##        X2014      X2015      X2016      X2017      X2018      X2019    X2019.1
## 1 0.06967843 0.04236369 0.03071490 0.03501282 0.03711277 0.03698559 0.03698559
## 2 0.04842229 0.04711099 0.04652594 0.04523905 0.04397589 0.04468071 0.04468071
## 3 0.02912075 0.04228639 0.04627912 0.04786747 0.04928880 0.05057765 0.05057765
## 4 0.03860101 0.03936995 0.04195270 0.04895165 0.06174287 0.06244267 0.06244267
## 5 0.05218069 0.05553796 0.06509640 0.06734505 0.07606149 0.07783668 0.07783668
## 6 0.11174531 0.10549122 0.10149701 0.08841615 0.08688744 0.09223225 0.09223225

mutate() Creates a new column or modifies existing ones.

# add new column colled x2030. Population
World_population_data <- World_population_data %>%
  mutate(X2030.Population = X2022.Population*2.71828^(8*Growth.Rate))

head(World_population_data$X2030.Population)
## [1] 150587920416   8186286161 152619648689    115285821    257768913
## [6] 136493283236

rename() Changes column names.

rename <- CO2_emission %>%
    rename(CO2_2019 = X2019)

head(rename)
##           Country.Name country_code                     Region
## 1                Aruba          ABW  Latin America & Caribbean
## 2          Afghanistan          AFG                 South Asia
## 3               Angola          AGO         Sub-Saharan Africa
## 4              Albania          ALB      Europe & Central Asia
## 5              Andorra          AND      Europe & Central Asia
## 6 United Arab Emirates          ARE Middle East & North Africa
##                           Indicator.Name      X1990      X1991       X1992
## 1 CO2 emissions (metric tons per capita)         NA         NA          NA
## 2 CO2 emissions (metric tons per capita)  0.1917451  0.1676816  0.09595774
## 3 CO2 emissions (metric tons per capita)  0.5536620  0.5445386  0.54355722
## 4 CO2 emissions (metric tons per capita)  1.8195416  1.2428102  0.68369983
## 5 CO2 emissions (metric tons per capita)  7.5218317  7.2353792  6.96307870
## 6 CO2 emissions (metric tons per capita) 30.1951886 31.7784962 29.08092584
##         X1993       X1994       X1995       X1996       X1997       X1998
## 1          NA          NA          NA          NA          NA          NA
## 2  0.08472111  0.07554583  0.06846796  0.06258803  0.05682662  0.05269086
## 3  0.70898423  0.83680440  0.91214149  1.07216847  1.08663697  1.09182531
## 4  0.63830704  0.64535519  0.60543625  0.61236736  0.46692147  0.57215370
## 5  6.72417752  6.54157891  6.73347949  6.99159455  7.30744115  7.63953851
## 6 29.27567777 30.84933296 31.12501806 30.92802588 30.48633262 29.66358052
##         X1999      X2000       X2001       X2002       X2003       X2004
## 1          NA         NA          NA          NA          NA          NA
## 2  0.04015697  0.0365737  0.03378536  0.04557366  0.05151838  0.04165539
## 3  1.10985966  0.9880774  0.94182891  0.89557767  0.92486944  0.93026295
## 4  0.95535931  1.0262131  1.05549588  1.23237878  1.33898498  1.40405869
## 5  7.92319165  7.9522863  7.72154906  7.56623988  7.24241557  7.34426233
## 6 28.88710798 27.0351591 29.43026994 28.50146173 27.96926982 27.03893822
##         X2005       X2006       X2007      X2008      X2009      X2010
## 1          NA          NA          NA         NA         NA         NA
## 2  0.06041878  0.06658329  0.06531235  0.1284166  0.1718624  0.2436140
## 3  0.81353929  0.82184008  0.81175351  0.8886580  0.9394040  0.9761842
## 4  1.33820940  1.33999574  1.39393137  1.3843112  1.4414936  1.5276237
## 5  7.35378001  6.79054277  6.53104692  6.4393039  6.1566875  6.1571978
## 6 25.38238104 22.93510429 21.37028576 22.0114692 19.8323489 19.0397698
##        X2011      X2012      X2013      X2014      X2015      X2016      X2017
## 1         NA         NA         NA         NA         NA         NA         NA
## 2  0.2965062  0.2592953  0.1856237  0.1462356  0.1728967  0.1497893  0.1316946
## 3  0.9855223  0.9506959  1.0362939  1.0997791  1.1350441  1.0318113  0.8133007
## 4  1.6694232  1.5032405  1.5336300  1.6683374  1.6037751  1.5576644  1.7887861
## 5  5.8508861  5.9446542  5.9428004  5.8071277  6.0261818  6.0806003  6.1041339
## 6 18.5094574 19.2078011 20.0556476 20.0516980 21.0776420 21.4806686 20.7690223
##        X2018   CO2_2019    X2019.1
## 1         NA         NA         NA
## 2  0.1632953  0.1598244  0.1598244
## 3  0.7776749  0.7921371  0.7921371
## 4  1.7827389  1.6922483  1.6922483
## 5  6.3629754  6.4812174  6.4812174
## 6 18.3906781 19.3295633 19.3295633

How to use trace() and recover()

trace() is used to temporarily modify a function so you can see what happens when it runs.

my_function <- function(x) {
  y <- x * 2
  return(y)
}

trace(my_function, tracer = quote(print("Function is running")))
## [1] "my_function"

recover() is used for debugging errors interactively.

f <- function(x) {
  y <- log(x)
  z <- y + 10
  return(z)
}

# f("a")

options(error = recover)

f(3)
## [1] 11.09861

Question 5:

Make functions that calculate summary statistics and apply it to a variable to show that it works

summary <- function(x) {

  # Sort the vector
  x <- sort(x)

  n <- length(x)

  # Minimum
  minimum <- x[1]

  # Maximum
  maximum <- x[n]

  # Mean
  total <- 0

  for(i in x) {
    total <- total + i
  }

  mean_value <- total / n

  # Median
  if(n %% 2 == 1) {

    median_value <- x[(n + 1) / 2]

  } else {

    median_value <- (x[n/2] + x[(n/2) + 1]) / 2
  }

  # First Quartile (Q1)
  q1_position <- (n + 1) / 4
  q1 <- x[round(q1_position)]

  # Third Quartile (Q3)
  q3_position <- 3 * (n + 1) / 4
  q3 <- x[round(q3_position)]

  # Display results
  cat("Minimum :", minimum, "\n")
  cat("1st Quartile :", q1, "\n")
  cat("Median :", median_value, "\n")
  cat("Mean :", mean_value, "\n")
  cat("3rd Quartile :", q3, "\n")
  cat("Maximum :", maximum, "\n")
}

summary(c(1,2,3,4,8,12,29,5,6))
## Minimum : 1 
## 1st Quartile : 2 
## Median : 5 
## Mean : 7.777778 
## 3rd Quartile : 12 
## Maximum : 29

Make a function to calculate two sample t test, then apply it to a function

two_sample_ttest <- function(x, y) {

  result <- t.test(x, y)

  return(result)
  
group1 <- c(2, 5, 3, 7, 3, 78, 9)
group2 <- c(5, 9, 5, 8, 3, 5, 1)

two_sample_ttest(group1, group2)
}

Question 6: How to use sapply() ,vapply(),lapply(), map(), and mapply()

lapply() applies a function to each element and returns a list.

x <- list(a = 1:5, b = 6:10)

lapply(x, mean)
## $a
## [1] 3
## 
## $b
## [1] 8

sapply() tries to return a vector or matrix instead of a list.

x <- list(a = 1:5, b = 6:10)

sapply(x, mean)
## a b 
## 3 8

vapply() like sapply() but you must specify the output type, making it safer.

x <- list(a = 1:5, b = 6:10)

vapply(x, mean, numeric(1))
## a b 
## 3 8

mapply() applies a function to multiple vectors at the same time.

mapply(sum, c(1,2,3), c(4,5,6))
## [1] 5 7 9

map() from purrr package Works like lapply() but from the tidyverse style

library(purrr)

x <- list(a = 1:5, b = 6:10)

map(x, mean)
## $a
## [1] 3
## 
## $b
## [1] 8