Accessing data files from R is called reading.
read.csvand 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
If your file ends in .tsv (tab-separated values) or if it has another delimiter, try the function
read.delim assumes tabs separate the values:
sep argument can make
read.delim separate values by space, vertical bar, or any other character:
read.delim('file_path/file_name.file_extension', sep = ' ') # space-delimited read.delim('file_path/file_name.file_extension', sep = '|') # vertical-bar-delimited
?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:
Now use the function
read_excel('file_path/file_name.xls') # or read_excel('file_path/file_name.xlsx')
?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')
Chose a function based on the type of file you have:
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))
?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 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,