Every data analysis project begins with one fundamental step: getting your data into R. For new users, this can feel overwhelming because R doesn’t have a single universal import function. Instead, it provides a variety of methods depending on the file format.At first, this seems confusing—you may wonder, “Which function should I use for CSV? What about JSON, Excel, or even a database?” But once you understand the right functions and packages for each file type, importing data becomes smooth and intuitive.This guide walks you through all the major ways to import data into R—from everyday CSV and Excel files to JSON, XML, and even direct database connections. Along the way, we’ll share practical examples, best practices, and quick hacks to make your workflow faster and error-free.By the end, you’ll have a handy reference to avoid endless Googling the next time you face a tricky import problem.
Before diving into imports, it’s best to set up your environment properly.
Your working directory is the “default folder” where R looks for files. Setting it up saves time when dealing with multiple datasets.
getwd() # check current working directory
setwd("path/to/your/folder") # set a new working directory
Now you can use relative paths (like "data.csv"
) instead of typing long absolute paths.
Old objects from previous sessions can cause conflicts. A quick reset helps:
rm(list = ls())
💡 Pro Tip: Start each project with a clean environment. It minimizes errors and keeps your workspace organized.
Text files often use tabs, commas, or semicolons as separators.
df <- read.table("data.txt", header = TRUE, sep = "\t")
Change the sep
argument for different delimiters.
The most common format. R provides built-in wrappers:
df <- read.csv("data.csv") # comma-separated
df <- read.csv2("data.csv") # semicolon-separated
Want to test data quickly? Copy it to your clipboard:
df <- read.table("clipboard", header = TRUE)
Great for ad-hoc analysis without saving files.
Sometimes you’ll deal with specialized formats. R offers excellent packages for these cases.
install.packages("rjson")
library(rjson)
jsonData <- fromJSON(file = "input.json")
jsonDF <- as.data.frame(jsonData)
library(XML)
library(RCurl)
xmlData <- xmlTreeParse("input.xml")
xmlDF <- xmlToDataFrame("input.xml")
htmlData <- readHTMLTable(getURL("https://example.com"))
The readxl
package is fast and simple:
install.packages("readxl")
library(readxl)
df <- read_excel("file.xlsx", sheet = 1)
Use the haven
package:
library(haven)
df_sas <- read_sas("data.sas7bdat")
df_spss <- read_sav("data.sav")
df_stata <- read_dta("data.dta")
library(R.matlab)
matData <- readMat("file.mat")
library(foreign)
octData <- read.octave("file.txt")
For large datasets, it’s often better to connect directly to a database rather than downloading files. The RODBC
package makes this easy:
install.packages("RODBC")
library(RODBC)
con <- odbcConnect("dsn", uid = "username", pwd = "password")
df <- sqlQuery(con, "SELECT * FROM Table1")
odbcClose(con)
This allows real-time data access and avoids duplicate versions.
snake_case
or camelCase
.NA
for consistency.Importing data into R may feel tricky at first, but with the right functions and packages, it becomes second nature. From CSV and Excel to JSON, XML, and direct database queries, R gives you the flexibility to handle almost any dataset.Think of data import as the first step in your analysis pipeline. A clean and efficient import ensures smoother modeling, visualization, and decision-making later. With this guide as your reference, you’ll spend less time searching for solutions and more time analyzing insights.
This article was originally published on Perceptive Analytics.
In United States, our mission is simple — 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 — helping them solve complex data analytics challenges. As a leading excel consultants, we turn raw data into strategic insights that drive better decisions.