Importing Data in R Script

Data Handling in detail

R offers several functions to import data from various file formats into your working environment. This guide demonstrates how to import data into R using different file formats.

Importing Data in R

To illustrate, we will use a sample dataset in two formats: .csv and .txt. Let’s dive into the methods for importing data.

Reading a CSV (Comma-Separated Values) File

Method 1: Using read.csv()

The read.csv() function is a simple way to import CSV files. It includes the following parameters:

  • file.choose(): Opens a dialog box to select a CSV file.
  • header: Indicates if the first row contains column names. Use TRUE if it does or FALSE otherwise.

Example:

# Import and store the dataset in data1
data1 <- read.csv(file.choose(), header = TRUE)

# Display the data
print(data1)

Output:

Name    Age Department
1 John    25   IT
2 Alice   30   HR
3 Robert  28   Finance

Method 2: Using read.table()

The read.table() function requires you to specify the delimiter using the sep parameter. For CSV files, use sep=",".

Example:

# Import and store the dataset in data2
data2 <- read.table(file.choose(), header = TRUE, sep = ",")

# Display the data
print(data2)

Output:

Name    Age Department
1 John    25   IT
2 Alice   30   HR
3 Robert  28   Finance
Reading a Tab-Delimited (.txt) File

Method 1: Using read.delim()

This function is specifically for tab-delimited files. It also has parameters like:

  • file.choose(): Opens a file selection dialog.
  • header: Indicates whether the first row contains column names.

Example:

# Import and store the dataset in data3
data3 <- read.delim(file.choose(), header = TRUE)

# Display the data
print(data3)

Output:

Product Price Quantity
1  Apples  100       50
2 Bananas   50      120
3 Oranges   75       80

Method 2: Using read.table()

For tab-delimited files, use sep="\t" to specify the delimiter.

Example:

# Import and store the dataset in data4
data4 <- read.table(file.choose(), header = TRUE, sep = "\t")

# Display the data
print(data4)

Output:

Product Price Quantity
1  Apples  100       50
2 Bananas   50      120
3 Oranges   75       80
Using RStudio to Import Data

You can also import data interactively using RStudio. Follow these steps:

  1. In the Environment tab, click Import Dataset.
  2. Choose the file format (CSV, Excel, etc.).
  3. Browse your computer to select the file.
  4. The data will appear in the RStudio Viewer. Type the dataset name in the console to display it.
Reading JSON Files in R

To work with JSON files, install the rjson package. This package allows you to:

  • Load JSON files.
  • Convert JSON data into data frames for analysis.

Install the Package:

install.packages("rjson")

Example JSON File (saved as example.json):

{
  "ID": ["101", "102", "103"],
  "Name": ["Alice", "Bob", "Charlie"],
  "Salary": ["5000", "6000", "5500"],
  "Department": ["IT", "HR", "Finance"]
}

Code to Read JSON:

# Load the rjson library
library(rjson)

# Provide the path to the JSON file
result <- fromJSON(file = "C:\\example.json")

# Print the result
print(result)

Output:

$ID
[1] "101" "102" "103"

$Name
[1] "Alice"   "Bob"     "Charlie"

$Salary
[1] "5000"  "6000"  "5500"

$Department
[1] "IT"      "HR"      "Finance"

Converting JSON to a Data Frame:

# Convert JSON to a data frame
data <- as.data.frame(result)
print(data)

Output:

ID    Name Salary Department
1    101   Alice   5000         IT
2    102     Bob   6000         HR
3    103 Charlie   5500    Finance

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *