install.packages(
  c("rmarkdown","haven",
    "DBI", "RMariaDB", "jsonlite"),
  repos = "https://cloud.r-project.org"
)
## package 'rmarkdown' successfully unpacked and MD5 sums checked
## package 'haven' successfully unpacked and MD5 sums checked
## package 'DBI' successfully unpacked and MD5 sums checked
## package 'RMariaDB' successfully unpacked and MD5 sums checked
## package 'jsonlite' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\MIFOTRA\AppData\Local\Temp\Rtmpa2SjaG\downloaded_packages

R Markdown

Import data from Statistical packages and from Database Management Systems.

R provides functions and packages that allow you to load data, analyze it, and create visualizations.

1. Importing data from statistical packages

Statistical software often saves data in specific file formats.

First, install and load the haven package.

library(haven)

file_path <- "D:\\Document\\BIG-DATA\\R\\Assignment\\data\\"

Importing Stata data

data <- read_dta(file.path(file_path, "survey.dta"))
View(data)
head(data)
## # A tibble: 6 × 8
##   student_id name                age gender program     year marks passed
##        <dbl> <chr>             <dbl> <chr>  <chr>      <dbl> <dbl>  <dbl>
## 1       1001 Aline Mukamana       20 Female IT             2  78.5      1
## 2       1002 Jean Uwimana         22 Male   Statistics     3  64        1
## 3       1003 Eric Niyonzima       19 Male   Business       1  88        1
## 4       1004 Claudine Ingabire    21 Female IT             2  91.5      1
## 5       1005 Patrick Habimana     23 Male   Economics      4  72        1
## 6       1006 Diane Uwamahoro      20 Female Statistics     2  84        1
summary(data)
##    student_id          name         age             gender        program  
##  Min.   :1001   Length   :10   Min.   :19.0   Length   :10   Length   :10  
##  1st Qu.:1003   N.unique :10   1st Qu.:20.0   N.unique : 2   N.unique : 4  
##  Median :1006   N.blank  : 0   Median :21.0   N.blank  : 0   N.blank  : 0  
##  Mean   :1006   Min.nchar:12   Mean   :21.1   Min.nchar: 4   Min.nchar: 2  
##  3rd Qu.:1008   Max.nchar:19   3rd Qu.:22.0   Max.nchar: 6   Max.nchar:10  
##  Max.   :1010                  Max.   :24.0                                
##       year         marks           passed   
##  Min.   :1.0   Min.   :59.50   Min.   :0.0  
##  1st Qu.:2.0   1st Qu.:69.75   1st Qu.:1.0  
##  Median :2.0   Median :77.25   Median :1.0  
##  Mean   :2.4   Mean   :77.75   Mean   :0.9  
##  3rd Qu.:3.0   3rd Qu.:87.00   3rd Qu.:1.0  
##  Max.   :4.0   Max.   :95.00   Max.   :1.0

Importing a CSV file

CSV files are common in statistical analysis.

data <- read.csv(file.path(file_path, "students.csv"))

View(data)

Get Top 6 rows

head(data)
##   student_id              name age gender    program year marks passed
## 1       1001    Aline Mukamana  20 Female         IT    2  78.5   True
## 2       1002      Jean Uwimana  22   Male Statistics    3  64.0   True
## 3       1003    Eric Niyonzima  19   Male   Business    1  88.0   True
## 4       1004 Claudine Ingabire  21 Female         IT    2  91.5   True
## 5       1005  Patrick Habimana  23   Male  Economics    4  72.0   True
## 6       1006   Diane Uwamahoro  20 Female Statistics    2  84.0   True

Get CSV Summary

summary(data)
##    student_id          name         age             gender        program  
##  Min.   :1001   Length   :10   Min.   :19.0   Length   :10   Length   :10  
##  1st Qu.:1003   N.unique :10   1st Qu.:20.0   N.unique : 2   N.unique : 4  
##  Median :1006   N.blank  : 0   Median :21.0   N.blank  : 0   N.blank  : 0  
##  Mean   :1006   Min.nchar:12   Mean   :21.1   Min.nchar: 4   Min.nchar: 2  
##  3rd Qu.:1008   Max.nchar:19   3rd Qu.:22.0   Max.nchar: 6   Max.nchar:10  
##  Max.   :1010                  Max.   :24.0                                
##       year         marks             passed  
##  Min.   :1.0   Min.   :59.50   Length   :10  
##  1st Qu.:2.0   1st Qu.:69.75   N.unique : 2  
##  Median :2.0   Median :77.25   N.blank  : 0  
##  Mean   :2.4   Mean   :77.75   Min.nchar: 4  
##  3rd Qu.:3.0   3rd Qu.:87.00   Max.nchar: 5  
##  Max.   :4.0   Max.   :95.00

2. Importing data from a Database Management System (DBMS)

A DBMS stores structured data in tables. R can connect to a database, execute SQL queries, and retrieve the results into a data frame. Common database systems include: - MySQL - PostgreSQL - Microsoft SQL Server - Oracle Database - SQLite

Importing data from MySQL

Install the package:

library(DBI)
library(RMariaDB)

con <- dbConnect(
  RMariaDB::MariaDB(),
  dbname = "PayrollDB",
  host = "localhost",
  port = 3306,
  user = "root",
  password = "Password123!"
)

employees <- dbGetQuery(
  con,
      "SELECT e.EmployeeID, d.DepartmentName, p.PositionName, e.EmploymentDate,   e.EmploymentDate, e.Status
     FROM payrolldb.employees e
    JOIN payrolldb.positions p on p.PositionID=e.PositionID
    JOIN payrolldb.departments d on d.DepartmentID=e.DepartmentID
;"
)

View(employees)

dbDisconnect(con)
head(employees)
##   EmployeeID DepartmentName  PositionName EmploymentDate EmploymentDate Status
## 1          1             IT Finance offer     2026-10-21     2026-10-21 Active
## 2          3             CS Finance offer     2026-10-21     2026-10-21 Active

3. Import data from a public API

library(jsonlite)

users <- fromJSON(
  "https://jsonplaceholder.typicode.com/users"
)

View(users)
head(users)
##   id                 name         username                     email
## 1  1        Leanne Graham             Bret         Sincere@april.biz
## 2  2         Ervin Howell        Antonette         Shanna@melissa.tv
## 3  3     Clementine Bauch         Samantha        Nathan@yesenia.net
## 4  4     Patricia Lebsack         Karianne Julianne.OConner@kory.org
## 5  5     Chelsey Dietrich           Kamren  Lucio_Hettinger@annie.ca
## 6  6 Mrs. Dennis Schulist Leopoldo_Corkery   Karley_Dach@jasper.info
##      address.street address.suite  address.city address.zipcode address.geo.lat
## 1       Kulas Light      Apt. 556   Gwenborough      92998-3874        -37.3159
## 2     Victor Plains     Suite 879   Wisokyburgh      90566-7771        -43.9509
## 3 Douglas Extension     Suite 847 McKenziehaven      59590-4157        -68.6102
## 4       Hoeger Mall      Apt. 692   South Elvis      53919-4257         29.4572
## 5      Skiles Walks     Suite 351    Roscoeview           33263        -31.8129
## 6 Norberto Crossing      Apt. 950 South Christy      23505-1337        -71.4197
##   address.geo.lng                 phone       website       company.name
## 1         81.1496 1-770-736-8031 x56442 hildegard.org    Romaguera-Crona
## 2        -34.4618   010-692-6593 x09125 anastasia.net       Deckow-Crist
## 3        -47.0653        1-463-123-4447   ramiro.info Romaguera-Jacobson
## 4       -164.2990     493-170-9623 x156      kale.biz      Robel-Corkery
## 5         62.5342         (254)954-1289  demarco.info        Keebler LLC
## 6         71.7478  1-477-935-8478 x6430       ola.org  Considine-Lockman
##                        company.catchPhrase                           company.bs
## 1   Multi-layered client-server neural-net          harness real-time e-markets
## 2           Proactive didactic contingency     synergize scalable supply-chains
## 3        Face to face bifurcated interface      e-enable strategic applications
## 4 Multi-tiered zero tolerance productivity transition cutting-edge web services
## 5     User-centric fault-tolerant solution     revolutionize end-to-end systems
## 6       Synchronised bottom-line interface     e-enable innovative applications