DEV Community

Dipti Moryani
Dipti Moryani

Posted on

Loading TXT, CSV, and Other Delimited Files

Data importing into R is a task in itself. There seems to be a different importing function for almost every file format imaginable. While this flexibility is one of R’s greatest strengths, it can also be confusing and, at times, frustrating—especially for beginners or practitioners switching between multiple data sources.
The good news is that once you understand which functions and packages to use for which file types, data import in R becomes both predictable and efficient.
This tutorial serves as a single reference guide for importing the most commonly used data formats into R. So the next time you find yourself searching “How to load XYZ file into R?”—you’ll know exactly where to look.
What this article covers
We will walk through importing:
TXT, CSV, and other delimited files
JSON files
XML and HTML tables
Excel workbooks
SAS, SPSS, STATA datasets
MATLAB and Octave files
Relational databases via ODBC
We’ll also share a quick importing hack that’s extremely useful for fast, ad-hoc analysis.
Let’s dive in.

Preparing Your Workspace Before Importing Data
Before importing any data, it’s important to set up your R workspace properly. A well-prepared environment saves time and avoids unnecessary errors later.
Understanding and Setting the Working Directory
Most projects store all relevant data files in a single folder. You can tell R to treat this folder as its working directory, allowing you to import files using relative paths.
Check your current working directory:
getwd()

If your files are located elsewhere, change the working directory:
setwd("")

Once set, R will automatically look for files in this folder unless told otherwise.

Cleaning the Workspace
Objects from previous sessions can silently interfere with your analysis. It’s often best to start fresh:
rm(list = ls())

This removes all variables, functions, and objects from the current environment.
Tip: Another good practice is to disable saving the workspace when exiting R, ensuring each session starts clean.

Loading TXT, CSV, and Other Delimited Files
Delimited text files are among the most common data formats. These files store values separated by a delimiter such as a tab, comma, or semicolon.
Reading Text Files
Example of a tab-delimited file:
Category V1 V2
A 3 2
B 5 6
B 2 3
A 4 8
A 7 3

Use read.table() to load such files:
df <- read.table("", header = TRUE)

You can specify the delimiter explicitly using the sep argument:
df <- read.table("", sep = "\t")

Reading CSV Files
CSV files use commas (,) or semicolons (;) as separators.
Use read.csv() for comma-separated files
Use read.csv2() for semicolon-separated files
df <- read.csv("")
df <- read.csv2("")

Internally, both functions are wrappers around read.table() with different defaults.
This means the following are equivalent:
read.table("file.csv", sep = ",")
read.csv("file.csv")

Copy-Paste Import: A Quick Hack
For fast, exploratory analysis, you can copy data directly from Excel or a webpage and paste it into R:
df <- read.table("clipboard", header = TRUE)

While not ideal for production workflows, this is incredibly useful for quick experiments or sanity checks.

Importing Data Using Packages
Many complex formats require external packages. Before using them, install and load the package:
install.packages("")
library("")

Reading JSON Files
JSON files are widely used in APIs and web data.
Install and load the rjson package:
install.packages("rjson")
library(rjson)

Import JSON from a local file:
json_data <- fromJSON(file = "input.json")

Or directly from a URL:
json_data <- fromJSON(file = "https://example.com/data.json")

JSON data is imported as a list. Convert it to a data frame if needed:
json_df <- as.data.frame(json_data)

Importing XML Data and HTML Tables
Web-based data often comes in XML or HTML format.
Reading XML Files
Install and load required packages:
library(XML)
library(RCurl)

Read XML from a URL:
xml_data <- xmlTreeParse("https://example.com/data.xml")

Convert XML to a data frame:
xml_df <- xmlToDataFrame("input.xml")

Extracting Tables from HTML Pages
You can extract tabular data from HTML pages using:
html_tables <- readHTMLTable(getURL("https://example.com/page.html"))

This is extremely useful for web scraping structured data.

Reading Excel Workbooks
Several packages support Excel imports, but readxl is the most lightweight and reliable.
install.packages("readxl")
library(readxl)

Read the first sheet:
df <- read_excel("file.xlsx")

Read a specific sheet:
read_excel("file.xlsx", sheet = "Sheet3")
read_excel("file.xlsx", sheet = 3)

Unlike older packages, readxl does not require Java or Perl, making it easier to use across systems.

Importing Data from Statistical Software
SAS, SPSS, and STATA Files
The haven package is the preferred option:
install.packages("haven")
library(haven)

Read files:
read_sas("data.sas7bdat")
read_sav("data.sav")
read_dta("data.dta")

These functions preserve metadata like labels and factors.

MATLAB and Octave Files
For MATLAB .mat files:
install.packages("R.matlab")
library(R.matlab)

data <- readMat("file.mat")

For Octave text files:
library(foreign)
data <- read.octave("file.txt")

Importing Data from Relational Databases (ODBC)
The RODBC package enables database connectivity.
install.packages("RODBC")
library(RODBC)

Connect to a database:
con <- odbcConnect("dsn", uid = "user", pwd = "password")

Fetch tables:
df1 <- sqlFetch(con, "Table1")
df2 <- sqlQuery(con, "SELECT * FROM Table2")

Close the connection:
odbcClose(con)

Tips for Easier Data Import in R
Ensure column names are unique
Avoid spaces and special characters in variable names
Replace blank values with NA
Follow consistent naming conventions
Remove comments from raw data files
Prefer short, meaningful variable names
Keep your code style consistent

End Notes
Loading data into R is only the first step in a much larger analytics journey. Once your data is in R, the real work begins—cleaning, transforming, visualizing, and modeling.
In this article, we covered how to import data from:
Flat files (TXT, CSV)
Web formats (JSON, XML, HTML)
Excel workbooks
Statistical software
Databases via ODBC
As with most things in R, there are multiple ways to accomplish the same task. This guide focuses on the most commonly used and reliable approaches.
If you know of better or faster alternatives, feel free to share them—learning R is always a collaborative process.
Happy importing… and make it easy’R.
At Perceptive Analytics, our mission is “to enable businesses to unlock value in data.” For over 20 years, we’ve partnered with more than 100 clients—from Fortune 500 companies to mid-sized firms—to solve complex data analytics challenges. Our services include access to experienced Power BI freelancers and working with a trusted Snowflake consultant, turning data into strategic insight. We would love to talk to you. Do reach out to us.

Top comments (0)