Data Import in R
In this lesson, you will learn how to import data into R from external files. Real-world data is usually stored in files such as CSV, Excel, or text files, and importing this data is the first step in data analysis.
R provides simple and powerful functions to read data from different sources and convert it into usable data structures.
Why Is Data Import Important?
Most real projects do not start with manually created data. Instead, data is collected from files, databases, or other systems.
Learning how to import data allows you to:
- Work with real datasets
- Analyze large amounts of information
- Prepare data for cleaning and visualization
Understanding the Working Directory
The working directory is the folder where R looks for files by default.
Before importing data, it is important to know or set your working directory.
getwd()
You can change the working directory if needed.
setwd("path/to/your/folder")
Importing CSV Files
CSV (Comma-Separated Values) files are the most common format for data storage.
R provides the read.csv() function to load CSV files.
data <- read.csv("students.csv")
head(data)
This reads the file and stores the data in a data frame.
Importing Text Files
Text files store data separated by spaces, tabs, or other characters.
You can use read.table() to import text data.
data <- read.table("data.txt", header = TRUE)
head(data)
Importing Excel Files
Excel files are widely used for storing tabular data.
To read Excel files, you first need to install and load a supporting package.
install.packages("readxl")
library(readxl)
Once installed, you can read Excel files easily.
data <- read_excel("sales_data.xlsx")
head(data)
Importing Data from URLs
R can also import data directly from online sources.
This is useful when working with publicly available datasets.
url <- "https://example.com/data.csv"
data <- read.csv(url)
head(data)
Checking Imported Data
After importing data, it is important to inspect it before analysis.
head()– View first rowstail()– View last rowsstr()– Check structuresummary()– Get statistics
str(data)
summary(data)
Handling Common Import Issues
Sometimes imported data may not look correct due to:
- Missing headers
- Wrong separators
- Incorrect data types
R provides options such as header, sep, and
stringsAsFactors to handle these issues.
📝 Practice Exercises
Exercise 1
Set your working directory and verify it using getwd().
Exercise 2
Import a CSV file and display the first 5 rows.
Exercise 3
Import an Excel file and check its structure.
Exercise 4
Import data from a URL and display summary statistics.
✅ Practice Answers
Answer 1
setwd("path/to/data")
getwd()
Answer 2
data <- read.csv("students.csv")
head(data)
Answer 3
library(readxl)
data <- read_excel("data.xlsx")
str(data)
Answer 4
data <- read.csv("https://example.com/data.csv")
summary(data)
What’s Next?
In the next lesson, you will learn how to clean and prepare data before analysis.
Data cleaning is a critical step in every data science workflow.