Sys.setenv(
  DB_USER = "root",
  DB_PASSWORD = "Password123!",
  DB_HOST = "localhost"
)

1 Introduction

R is a programming language and statistical computing environment used for data manipulation, statistical analysis, and visualization. One of the first steps in data analysis is importing data from different sources into R.

This assignment demonstrates how to import data from statistical software, flat files, relational database management systems (DBMS), and public APIs.

The objectives are to:

  1. Import Stata data files (.dta).
  2. Import CSV files (.csv).
  3. Import SPSS data files (.sav).
  4. Import SAS data files (.sas7bdat).
  5. Connect to a MySQL database and retrieve records using SQL.
  6. Import JSON data from a public REST API.
  7. Inspect imported datasets using R functions.

2 Preparation and Required Packages

2.1 Required packages

R packages extend the functionality of the R environment. The following packages are required for this assignment.

Package

haven: Imports data from Stata, SPSS, and SAS. DBI: Provides a standard interface for connecting to databases. RMariaDB: Connects R to MySQL-compatible and MariaDB database servers. jsonlite | Converts JSON data into R objects. rmarkdown | Generates reports in HTML and PDF formats.

Install the packages once by running the following code in the R console. Package installation is not required every time the report is generated.

install.packages(
  c("rmarkdown", "haven", "DBI", "RMariaDB", "jsonlite"),
  repos = "https://cloud.r-project.org"
)
## Installing packages into 'C:/Users/MIFOTRA/AppData/Local/R/win-library/4.6'
## (as 'lib' is unspecified)
## package 'rmarkdown' successfully unpacked and MD5 sums checked
## package 'haven' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'haven'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\MIFOTRA\AppData\Local\R\win-library\4.6\00LOCK\haven\libs\x64\haven.dll
## to C:\Users\MIFOTRA\AppData\Local\R\win-library\4.6\haven\libs\x64\haven.dll:
## Permission denied
## Warning: restored 'haven'
## package 'DBI' successfully unpacked and MD5 sums checked
## package 'RMariaDB' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'RMariaDB'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\MIFOTRA\AppData\Local\R\win-library\4.6\00LOCK\RMariaDB\libs\x64\RMariaDB.dll
## to
## C:\Users\MIFOTRA\AppData\Local\R\win-library\4.6\RMariaDB\libs\x64\RMariaDB.dll:
## Permission denied
## Warning: restored 'RMariaDB'
## package 'jsonlite' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'jsonlite'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\MIFOTRA\AppData\Local\R\win-library\4.6\00LOCK\jsonlite\libs\x64\jsonlite.dll
## to
## C:\Users\MIFOTRA\AppData\Local\R\win-library\4.6\jsonlite\libs\x64\jsonlite.dll:
## Permission denied
## Warning: restored 'jsonlite'
## 
## The downloaded binary packages are in
##  C:\Users\MIFOTRA\AppData\Local\Temp\RtmpYBR5Kb\downloaded_packages

2.2 Load the required packages

The following setup chunk loads the packages needed for the assignment and configures the report.

2.3 Define the data directory

The datasets used in this assignment are stored in a local directory on the computer.

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

# Check whether the directory exists
dir.exists(file_path)
## [1] TRUE
# List the files available in the directory
list.files(file_path)
## [1] "student_scores.sas7bdat" "students.csv"           
## [3] "survey.dta"

The file_path variable stores the directory containing the local datasets. The dir.exists() function checks whether the directory is available, while list.files() displays its contents.

The following files are expected:

  • survey.dta
  • students.csv
  • student_scores.sas7bdat

The SPSS example in Section 4 is downloaded from a public GitHub URL.

3 Importing Data from Stata

3.1 Introduction to Stata files

Stata is statistical software commonly used in economics, social sciences, public policy, and research. It stores datasets in files with the .dta extension.

The haven package provides the read_dta() function for importing Stata datasets into R.

3.2 Import the Stata dataset

stata_data <- read_dta(
  file.path(file_path, "survey.dta")
)
head(stata_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

3.3 Examine the imported data

# Display the first six rows
head(stata_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
# Display the structure and variable types
str(stata_data)
## tibble [10 × 8] (S3: tbl_df/tbl/data.frame)
##  $ student_id: num [1:10] 1001 1002 1003 1004 1005 ...
##   ..- attr(*, "format.stata")= chr "%12.0g"
##  $ name      : chr [1:10] "Aline Mukamana" "Jean Uwimana" "Eric Niyonzima" "Claudine Ingabire" ...
##   ..- attr(*, "format.stata")= chr "%19s"
##  $ age       : num [1:10] 20 22 19 21 23 20 24 19 22 21
##   ..- attr(*, "format.stata")= chr "%12.0g"
##  $ gender    : chr [1:10] "Female" "Male" "Male" "Female" ...
##   ..- attr(*, "format.stata")= chr "%6s"
##  $ program   : chr [1:10] "IT" "Statistics" "Business" "IT" ...
##   ..- attr(*, "format.stata")= chr "%10s"
##  $ year      : num [1:10] 2 3 1 2 4 2 4 1 3 2
##   ..- attr(*, "format.stata")= chr "%12.0g"
##  $ marks     : num [1:10] 78.5 64 88 91.5 72 84 59.5 76 69 95
##   ..- attr(*, "format.stata")= chr "%10.0g"
##  $ passed    : num [1:10] 1 1 1 1 1 1 0 1 1 1
##   ..- attr(*, "format.stata")= chr "%8.0g"
# Display the number of rows and columns
dim(stata_data)
## [1] 10  8
# Display the column names
names(stata_data)
## [1] "student_id" "name"       "age"        "gender"     "program"   
## [6] "year"       "marks"      "passed"

3.4 Explanation

  • read_dta() reads the Stata file and returns an R data frame or tibble.
  • head() displays the first six rows by default.
  • str() displays the dataset’s structure, including variable names and data types.
  • dim() returns the number of rows and columns.
  • names() returns the variable names.

Result: The Stata dataset is available in R for further processing and statistical analysis.

4 Importing Data from CSV

4.1 Introduction to CSV files

CSV stands for Comma-Separated Values. A CSV file stores tabular data as text, with records typically separated by line breaks and fields separated by commas.

CSV is widely used because it is supported by spreadsheet applications, databases, statistical software, and programming languages.

R provides the built-in read.csv() function, so an additional package is not required.

4.2 Import the CSV dataset

csv_data <- read.csv(
  file.path(file_path, "students.csv"),
  header = TRUE,
  stringsAsFactors = FALSE
)

4.3 Examine the imported data

# Display the first six rows
head(csv_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
# Examine variable types and structure
str(csv_data)
## 'data.frame':    10 obs. of  8 variables:
##  $ student_id: int  1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
##  $ name      : chr  "Aline Mukamana" "Jean Uwimana" "Eric Niyonzima" "Claudine Ingabire" ...
##  $ age       : int  20 22 19 21 23 20 24 19 22 21
##  $ gender    : chr  "Female" "Male" "Male" "Female" ...
##  $ program   : chr  "IT" "Statistics" "Business" "IT" ...
##  $ year      : int  2 3 1 2 4 2 4 1 3 2
##  $ marks     : num  78.5 64 88 91.5 72 84 59.5 76 69 95
##  $ passed    : chr  "True" "True" "True" "True" ...
# Count rows and columns
dim(csv_data)
## [1] 10  8
# Display column names
names(csv_data)
## [1] "student_id" "name"       "age"        "gender"     "program"   
## [6] "year"       "marks"      "passed"

4.4 Explanation

  • read.csv() reads the CSV file into R.
  • header = TRUE indicates that the first row contains column names.
  • stringsAsFactors = FALSE prevents character columns from automatically being converted into factors in R versions where this setting applies.
  • head(csv_data) displays the first six records.

Result: The CSV file is imported into an R object named csv_data.

5 Importing Data from SPSS

5.1 Introduction to SPSS files

SPSS (Statistical Package for the Social Sciences) is statistical software used for survey analysis, social research, and other forms of quantitative analysis.

SPSS datasets commonly use the .sav extension. These files may contain variable labels, value labels, and other metadata.

The haven package provides the read_sav() function for reading SPSS files.

5.2 Import an SPSS dataset from GitHub

The following example uses the Iris dataset provided in the haven package’s public GitHub repository.

sav_url <- paste0(
  "https://raw.githubusercontent.com/tidyverse/haven/",
  "HEAD/inst/examples/iris.sav"
)

iris_spss <- read_sav(sav_url)

head(iris_spss)
## # A tibble: 6 × 5
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
##          <dbl>       <dbl>        <dbl>       <dbl> <dbl+lbl> 
## 1          5.1         3.5          1.4         0.2 1 [setosa]
## 2          4.9         3            1.4         0.2 1 [setosa]
## 3          4.7         3.2          1.3         0.2 1 [setosa]
## 4          4.6         3.1          1.5         0.2 1 [setosa]
## 5          5           3.6          1.4         0.2 1 [setosa]
## 6          5.4         3.9          1.7         0.4 1 [setosa]

5.3 Examine the imported data

# Display the first six rows
head(iris_spss)
## # A tibble: 6 × 5
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
##          <dbl>       <dbl>        <dbl>       <dbl> <dbl+lbl> 
## 1          5.1         3.5          1.4         0.2 1 [setosa]
## 2          4.9         3            1.4         0.2 1 [setosa]
## 3          4.7         3.2          1.3         0.2 1 [setosa]
## 4          4.6         3.1          1.5         0.2 1 [setosa]
## 5          5           3.6          1.4         0.2 1 [setosa]
## 6          5.4         3.9          1.7         0.4 1 [setosa]
# Examine the structure
str(iris_spss)
## tibble [150 × 5] (S3: tbl_df/tbl/data.frame)
##  $ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##   ..- attr(*, "format.spss")= chr "F8.2"
##  $ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##   ..- attr(*, "format.spss")= chr "F8.2"
##  $ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##   ..- attr(*, "format.spss")= chr "F8.2"
##  $ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##   ..- attr(*, "format.spss")= chr "F8.2"
##  $ Species     : dbl+lbl [1:150] 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
##    ..@ format.spss: chr "F8.0"
##    ..@ labels     : Named num [1:3] 1 2 3
##    .. ..- attr(*, "names")= chr [1:3] "setosa" "versicolor" "virginica"
# Count rows and columns
dim(iris_spss)
## [1] 150   5
# Display variable names
names(iris_spss)
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"

5.4 Explanation

  • sav_url stores the URL of the SPSS file.
  • read_sav() downloads and reads the file into R.
  • head() displays the first six observations.
  • str() displays the variable types and structure.

Result: The SPSS dataset is imported into an R object named iris_spss.

Note: An internet connection is required to retrieve the file from GitHub.

6 Importing Data from SAS

6.1 Introduction to SAS files

SAS (Statistical Analysis System) is software used for statistical analysis, data management, and business intelligence.

SAS datasets can be stored in files with the .sas7bdat extension. The haven package provides the read_sas() function to import these files.

6.2 Import the SAS dataset

sas_data <- read_sas(
  file.path(file_path, "student_scores.sas7bdat")
)

head(sas_data)
## # A tibble: 6 × 7
##   student_id gender course_code course_name             semester score grade
##   <chr>      <chr>  <chr>       <chr>                   <chr>    <dbl> <chr>
## 1 STU0001    F      CSC201      Data Structures         2025-S2   77.4 B    
## 2 STU0001    F      ECO110      Principles of Economics 2025-S1   72.5 B    
## 3 STU0001    F      ENG120      Academic Writing        2025-S1   85.7 A    
## 4 STU0001    F      MTH102      Calculus I              2025-S2   72.8 B    
## 5 STU0002    F      BIO130      General Biology         2025-S2   58.5 D    
## 6 STU0002    F      ECO110      Principles of Economics 2025-S1   45.2 F

6.3 Examine the imported data

# Display the first six rows
head(sas_data)
## # A tibble: 6 × 7
##   student_id gender course_code course_name             semester score grade
##   <chr>      <chr>  <chr>       <chr>                   <chr>    <dbl> <chr>
## 1 STU0001    F      CSC201      Data Structures         2025-S2   77.4 B    
## 2 STU0001    F      ECO110      Principles of Economics 2025-S1   72.5 B    
## 3 STU0001    F      ENG120      Academic Writing        2025-S1   85.7 A    
## 4 STU0001    F      MTH102      Calculus I              2025-S2   72.8 B    
## 5 STU0002    F      BIO130      General Biology         2025-S2   58.5 D    
## 6 STU0002    F      ECO110      Principles of Economics 2025-S1   45.2 F
# Examine variable types
str(sas_data)
## tibble [800 × 7] (S3: tbl_df/tbl/data.frame)
##  $ student_id : chr [1:800] "STU0001" "STU0001" "STU0001" "STU0001" ...
##   ..- attr(*, "label")= chr "Student ID"
##  $ gender     : chr [1:800] "F" "F" "F" "F" ...
##   ..- attr(*, "label")= chr "Gender"
##  $ course_code: chr [1:800] "CSC201" "ECO110" "ENG120" "MTH102" ...
##   ..- attr(*, "label")= chr "Course code"
##  $ course_name: chr [1:800] "Data Structures" "Principles of Economics" "Academic Writing" "Calculus I" ...
##   ..- attr(*, "label")= chr "Course name"
##  $ semester   : chr [1:800] "2025-S2" "2025-S1" "2025-S1" "2025-S2" ...
##   ..- attr(*, "label")= chr "Semester"
##  $ score      : num [1:800] 77.4 72.5 85.7 72.8 58.5 45.2 71.5 41.4 53.4 71.5 ...
##   ..- attr(*, "label")= chr "Final score (0-100)"
##  $ grade      : chr [1:800] "B" "B" "A" "B" ...
##   ..- attr(*, "label")= chr "Letter grade"
# Count rows and columns
dim(sas_data)
## [1] 800   7
# Display variable names
names(sas_data)
## [1] "student_id"  "gender"      "course_code" "course_name" "semester"   
## [6] "score"       "grade"

6.4 Explanation

  • read_sas() reads the SAS dataset into R.
  • file.path() constructs the file path using the data directory and filename.
  • head() displays the first six observations.
  • str() provides information about the dataset’s variables and structure.

Result: The SAS dataset is imported into an R object named sas_data.

7 Importing Data from a Database Management System

7.1 Introduction to DBMS

A Database Management System (DBMS) is software used to store, organize, retrieve, and manage data.

Common database management systems include:

  • MySQL
  • MariaDB
  • PostgreSQL
  • Microsoft SQL Server
  • Oracle Database
  • SQLite

Unlike CSV and statistical data files, a database usually requires a connection before data can be retrieved.

R can connect to databases, execute SQL statements, and return query results as data frames.

7.2 Required packages

The following packages are used for database connectivity:

  • DBI: Defines a common interface for database operations.
  • RMariaDB: Provides a database driver for MySQL-compatible and MariaDB servers.
library(DBI)
library(RMariaDB)

7.3 Connect to the MySQL database

The following example assumes that a MySQL-compatible database named PayrollDB is running on the local computer.

For security, database credentials should be stored in environment variables rather than written directly into an assignment or report.

db_host <- Sys.getenv("DB_HOST", unset = "localhost")
db_name <- "PayrollDB"
db_user <- Sys.getenv("DB_USER")
db_password <- Sys.getenv("DB_PASSWORD")

if (db_user == "" || db_password == "") {
  stop(
    "Set the DB_USER and DB_PASSWORD environment variables ",
    "before running this section."
  )
}

con <- dbConnect(
  RMariaDB::MariaDB(),
  dbname = db_name,
  host = db_host,
  port = 3306,
  user = db_user,
  password = db_password
)

dbIsValid(con)
## [1] TRUE

The dbConnect() function establishes the connection to the database.

The dbIsValid() function checks whether the connection is valid.

Before running this section, configure the DB_USER, DB_PASSWORD, and, if necessary, DB_HOST environment variables on your computer.

7.4 Retrieve employee information using SQL

The following query retrieves employee information together

employees <- tryCatch(
  {
    dbGetQuery(
      con,
      "
      SELECT
          e.EmployeeID,
          d.DepartmentName,
          p.PositionName,
          e.EmploymentDate,
          e.Status
      FROM Employees AS e
      INNER JOIN Positions AS p
          ON p.PositionID = e.PositionID
      INNER JOIN Departments AS d
          ON d.DepartmentID = e.DepartmentID
      "
    )
  },
  finally = {
    dbDisconnect(con)
  }
)

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

This example assumes that the Employees, Positions, and Departments tables are available in the PayrollDB database and have the specified columns.

7.5 Examine the retrieved data

# Display the first six records
head(employees)
##   EmployeeID DepartmentName  PositionName EmploymentDate Status
## 1          1             IT Finance offer     2026-10-21 Active
## 2          3             CS Finance offer     2026-10-21 Active
# Examine the data structure
str(employees)
## 'data.frame':    2 obs. of  5 variables:
##  $ EmployeeID    : int  1 3
##  $ DepartmentName: chr  "IT" "CS"
##  $ PositionName  : chr  "Finance offer" "Finance offer"
##  $ EmploymentDate: Date, format: "2026-10-21" "2026-10-21"
##  $ Status        : chr  "Active" "Active"
# Count rows and columns
dim(employees)
## [1] 2 5
# Display column names
names(employees)
## [1] "EmployeeID"     "DepartmentName" "PositionName"   "EmploymentDate"
## [5] "Status"

8 Importing Data from a Public REST API

8.1 Introduction to APIs

An Application Programming Interface (API) allows one software application to communicate with another.

A REST API commonly exposes resources through HTTP endpoints. Data is often returned in JSON (JavaScript Object Notation) format.

JSON is a lightweight data-interchange format that represents objects, arrays, strings, numbers, Boolean values, and null values.

The jsonlite package provides functions for converting JSON responses into R objects.

8.2 Retrieve data from a public API

This example uses JSONPlaceholder, a public demonstration API that provides sample user records.

library(jsonlite)

api_url <- "https://jsonplaceholder.typicode.com/users"

users <- fromJSON(api_url)

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

8.3 Examine the API data

# Display the first six 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
# Examine the structure
str(users)
## 'data.frame':    10 obs. of  8 variables:
##  $ id      : int  1 2 3 4 5 6 7 8 9 10
##  $ name    : chr  "Leanne Graham" "Ervin Howell" "Clementine Bauch" "Patricia Lebsack" ...
##  $ username: chr  "Bret" "Antonette" "Samantha" "Karianne" ...
##  $ email   : chr  "Sincere@april.biz" "Shanna@melissa.tv" "Nathan@yesenia.net" "Julianne.OConner@kory.org" ...
##  $ address :'data.frame':    10 obs. of  5 variables:
##   ..$ street : chr  "Kulas Light" "Victor Plains" "Douglas Extension" "Hoeger Mall" ...
##   ..$ suite  : chr  "Apt. 556" "Suite 879" "Suite 847" "Apt. 692" ...
##   ..$ city   : chr  "Gwenborough" "Wisokyburgh" "McKenziehaven" "South Elvis" ...
##   ..$ zipcode: chr  "92998-3874" "90566-7771" "59590-4157" "53919-4257" ...
##   ..$ geo    :'data.frame':  10 obs. of  2 variables:
##   .. ..$ lat: chr  "-37.3159" "-43.9509" "-68.6102" "29.4572" ...
##   .. ..$ lng: chr  "81.1496" "-34.4618" "-47.0653" "-164.2990" ...
##  $ phone   : chr  "1-770-736-8031 x56442" "010-692-6593 x09125" "1-463-123-4447" "493-170-9623 x156" ...
##  $ website : chr  "hildegard.org" "anastasia.net" "ramiro.info" "kale.biz" ...
##  $ company :'data.frame':    10 obs. of  3 variables:
##   ..$ name       : chr  "Romaguera-Crona" "Deckow-Crist" "Romaguera-Jacobson" "Robel-Corkery" ...
##   ..$ catchPhrase: chr  "Multi-layered client-server neural-net" "Proactive didactic contingency" "Face to face bifurcated interface" "Multi-tiered zero tolerance productivity" ...
##   ..$ bs         : chr  "harness real-time e-markets" "synergize scalable supply-chains" "e-enable strategic applications" "transition cutting-edge web services" ...
# Count the records and columns
dim(users)
## [1] 10  8
# Display column names
names(users)
## [1] "id"       "name"     "username" "email"    "address"  "phone"    "website" 
## [8] "company"

9 Conclusion

This assignment demonstrates how R imports data from different statistical packages, local files, relational databases, and public APIs.

The haven package supports Stata, SPSS, and SAS formats, while base R provides functions for importing CSV files. The DBI and RMariaDB packages enable database connectivity and SQL-based retrieval. The jsonlite package supports importing JSON data from REST APIs.

Understanding these methods is essential for data preparation, statistical analysis, business intelligence, and data analytics. Selecting the appropriate import method depends on the source of the data, its structure, the required metadata, and the way the data will be used in subsequent analysis.