R Craft: Read CSV, RDA, and other Supported Data Files with R

You downloaded data files, or maybe you compiled them yourself. How will you get those data into R?

R software logo

R offers built-in functions that let you access either delimited (where a certain character separates values) or fixed-width (where each column uses a certain number of characters) files. Like other popular statistical packages, R even supports its own data file format. Let’s explore some frequently-used functions.

Read delimited files

Delimited files, such as CSV and TSV, are popular for their straightforward ability to work in a wide variety of software packages.

Read CSV files

If your file ends in the .csv extension, commas probably separate the values in each row. R offers the read.csv function for this situation.

# Comma-separated values
dat <- read.csv('file_path/file_name.csv')

In most cases, you can point the function directly to the file you want to open without setting more options. Assigning the data to an object, as shown here, will let you access it later in your R session.

Read TSV and other delimited files

If your file ends in .tsv (tab-separated values) or if it has another delimiter, try the function read.delim.

# Tab-separated values
dat <- read.delim('file_path/file_name.tsv')

By default, read.delim assumes tabs separate the values. You can tell the function to separate values by space, vertical bar, or any other character using the sep argument.

# Space-separated values
dat <- read.delim('file_path/file_name.extension', sep = " ")

# Vertical-bar-separated values
dat <- read.delim('file_path/file_name.extension', sep = "|")

For more customization, try the function read.table. Use Help to explore options such as changing the decimal indicator or skipping non-data rows at the top of the file.

# Documentation for read.table, read.csv, read.delim, etc.
?read.table

Read fixed-width files

If spacing and the number of characters determine where the columns of your data begin and end, you probably have fixed-width data. Use read.fwf by telling the function both where to find the file and, using the widths argument, how many characters wide each column is.

dat <- read.fwf('file_path/file_name.extension', widths = c(col1_width, col2_width, ..., coln_width))

Load RDATA and RDA files

R can save data in its own .rdata or .rda file format. These files each can hold one or more objects, and all objects within a RDA file load into R together.

load('file_path/file_name.rda')
# or
load('file_path/file_name.rdata')

Note that you won’t assign these data. The load function imports data objects with their original object names.

Read other file types

Don’t see your file type on this page?

For unsupported file types like XLSX, DTA, SAS7BDAT, and SAV; check out our post R Craft: Read Excel, Stata, and other Unsupported Data Files with R.

Or open your favorite internet search engine and search r how to open [file_extension] files. Someone probably faced this challenge before, leaving helpful suggestions.

What’s next?

With your data in R, you’re on your way to understanding and interpreting the information. You can learn more about exploring data and getting data ready for analysis in our recorded workshop R Basics: Prepare Data for Modeling and Analysis.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s