Accessing data files from R is called reading.
read.csv
and related functions.R does not store data it reads automatically, so you will want to store most data as an object:
object_name <- read.table('file')
If your file ends in the .csv extension, commas probably separate the values in each row. Try the function read.csv
:
read.csv('file_path/file_name.csv')
If your file ends in .tsv (tab-separated values) or if it has another delimiter, try the function read.delim
.
By default, read.delim
assumes tabs separate the values:
read.delim('file_path/file_name.tsv')
The sep
argument can make read.csv
or read.delim
separate values by space, vertical bar, or any other character:
read.delim('file_path/file_name.file_extension', sep = ' ')
read.delim('file_path/file_name.file_extension', sep = '|')
Run ?read.table
to see options for changing the decimal character, skipping non-data rows at the top of the file, and more.
You can read Microsoft Excel files, which end in .xls or .xlsx, using the package readxl.
Install readxl on its own or as part of the package tidyverse:
install.packages('readxl')
# or
install.packages('tidyverse')
Load readxl separately:
library(readxl)
Now use the function read_excel
:
read_excel('file_path/file_name.xls')
# or
read_excel('file_path/file_name.xlsx')
Run ?read_excel
for information about reading specific worksheets and other options.
Working with Stata, SAS or SPSS data? You can read files that end in .dta, .sas7bdat, or .sav. using the package haven.
Install haven directly or as part of the package tidyverse:
install.packages('haven')
# or
install.packages('tidyverse')
Load haven:
library(haven)
Chose a function based on the type of file you have:
read_dta('file_path/file_name.dta')
read_sas('file_path/file_name.sas7bdat')
read_sav('file_path/file_name.sav')
If spacing and the number of characters determine where the columns of your data begin and end, you probably have fixed-width data.
You can use the function read.fwf
. Give the function
read.fwf('file_path/file_name.file_extension', widths = c(col1_width, col2_width, ..., coln_width))
Run ?read.fwf
for details about including column names as well as other options.
R can save data in its own .rdata or .rda file format. RDATA and RDA files store data as R objects.
To load all of the objects in a RDATA or RDA file at once, use the function load
:
load('file_path/file_name.rda')
The load
function imports data objects with their original object names. Data will stay in your environment without assigning new object names.
Having trouble finding your file? Getting errors from your file path?
You can use RStudio's Import Dataset tool to help develop your file-reading code.
The steps above will run code to load your file once. To make it an ongoing part of your code,