Import CSV Files into R and RStudio
How to read CSV files into R and RStudio with base R, readr or data.table, what each approach changes in your data, and how to avoid common import problems on Mac and elsewhere.
Reading a CSV file into R turns plain delimited text into a data frame: a structure with named columns, data types and missing-value markers. That conversion is useful, but it is not neutral. R has to decide where each field ends, which row holds the column names, whether a value is a number, a date or text, and what counts as missing. This review covers the main ways to import CSV files in R and RStudio, what each one changes, and when keeping the data as plain text is the safer choice.
Scope note: no benchmark results, version-specific test runs or measurements were supplied for this article. Statements below describe documented, generally known behavior of the functions. Where behavior depends on your R version, package version or file, that is stated rather than assumed.
Which import method suits your use?
For most people, a bounded recommendation looks like this. Use base R's read.csv() when you want no extra packages and your files are small to moderate and well formed. Use readr's read_csv() when you want predictable type handling, explicit column specifications and a tibble as output. Use data.table's fread() when files are large or the delimiter is uncertain, since it detects the separator automatically and is built for speed. Use RStudio's Import Dataset dialog when you want to preview the file and have the import code written for you.
| Method | Package | Output | Notable defaults | Best fit |
| read.csv() | base R (utils) | data.frame | header = TRUE, sep = ","; strings stay as character since R 4.0.0; column names made syntactically valid | No dependencies, small or clean files |
| read.csv2() | base R (utils) | data.frame | sep = ";", dec = "," | Files from locales that use a comma as the decimal mark |
| read.delim() | base R (utils) | data.frame | sep = "\t" | TSV files without extra packages |
| read_csv() | readr | tibble | Guesses column types from a sample of rows; reports the column specification; keeps original column names | Reproducible scripts, explicit types |
| read_tsv() / read_delim() | readr | tibble | Tab or user-specified delimiter | TSV and other delimiters |
| fread() | data.table | data.table (data.frame optional) | Detects separator and header automatically | Large files, unknown delimiter |
| Import Dataset dialog | RStudio (uses base R or readr) | data.frame or tibble | Preview plus generated code | Exploring an unfamiliar file |
The decisive tradeoff is between convenience and control. Automatic type guessing saves time, but it can silently reinterpret values. If a column holds ZIP codes, account numbers, product codes or IDs with leading zeros, or if a downstream step needs the exact original characters, either declare those columns as text or keep the file as plain text rather than relying on guessed types.
Reading a CSV with base R
read.csv() ships with R, so it works the same way on Mac, Windows and Linux without installing anything. The function itself does not behave differently on a Mac; what usually differs is how you write the file path.
# File in the current working directory
sales <- read.csv("sales.csv")
# Full path on a Mac (forward slashes; ~ means your home folder)
sales <- read.csv("~/Downloads/sales.csv")
# Keep IDs as text and treat empty cells as missing
sales <- read.csv(
"sales.csv",
colClasses = c(customer_id = "character"),
na.strings = c("", "NA")
)
# Inspect the result
str(sales)
head(sales)
If you are unsure where R is looking, getwd() shows the current working directory and setwd() changes it. file.choose() opens a system file picker and returns the selected path, which is handy interactively but makes scripts harder to rerun, because the path is not recorded in the code. On a Mac, holding the Option key in Finder's context menu offers a command to copy a file's full path, which you can paste inside quotes.
Reading a CSV with the readr package
readr is part of the tidyverse and is a common answer to the question of which package to use for reading CSV files in R. Install it once, then load it in each session.
install.packages("readr") # once
library(readr)
sales <- read_csv("sales.csv")
# Declare types explicitly instead of relying on guessing
sales <- read_csv(
"sales.csv",
col_types = cols(
customer_id = col_character(),
order_date = col_date(format = "%Y-%m-%d"),
amount = col_double()
)
)
# Tab-separated file
events <- read_tsv("events.tsv")
# Non-UTF-8 file
old <- read_csv("legacy.csv", locale = locale(encoding = "latin1"))
read_csv() prints the column specification it used, which is a useful check: if a column you expected to be numeric shows up as character, there is probably a stray text value in it. Because type guessing is based on a sample of rows, a column that looks numeric at the top of the file but contains text further down can be misread. Specifying col_types, or raising guess_max, avoids that.
Reading a CSV with data.table
install.packages("data.table") # once
library(data.table)
sales <- fread("sales.csv")
# Return a plain data.frame instead of a data.table
sales <- fread("sales.csv", data.table = FALSE)
# Force a column to stay as text
sales <- fread("sales.csv", colClasses = list(character = "customer_id"))
fread() is designed for large files and detects the delimiter on its own, which helps when you receive files that may be comma, semicolon or tab separated. The output is a data.table, which also behaves as a data frame but uses its own bracket syntax for many operations; set data.table = FALSE if you prefer a standard data frame. No timing comparisons were supplied for this article, so the speed advantage is described here only as the package's documented design goal, not a measured result.
Importing a CSV in RStudio with the menu
RStudio offers a point-and-click route that works the same way on Mac and Windows. Go to File, then Import Dataset, and choose From Text (base) or From Text (readr). The same options are available from the Import Dataset button in the Environment pane. The readr option may prompt you to install readr if it is missing.
- Browse to the file, or paste a path or web address into the file field.
- Check the data preview to confirm columns are split correctly.
- Adjust the delimiter, header row, missing-value strings, encoding or column types as needed.
- Rename the object in the Name field if you want something other than the file name.
- Copy the code shown in the Code Preview box into your script before clicking Import, so the import can be rerun without the dialog.
The dialog's main benefit is the live preview: you can see immediately whether a semicolon-separated file is being split on commas or whether the first row is being treated as data. Its limitation is that clicks are not saved; only the generated code makes the step reproducible.
What the import actually changes
Importing does not modify the CSV file on disk. It creates a new object in memory, and the differences between that object and the original text are where problems usually come from.
- Data types: text such as 007 becomes the number 7 if the column is parsed as numeric, and the leading zeros are gone from the object. Long digit strings such as card or account numbers can lose precision if stored as doubles.
- Column names: read.csv() by default makes names syntactically valid, so a header like Order Date becomes Order.Date. readr and fread keep the original names, which may then need backticks in code. Setting check.names = FALSE in read.csv() keeps names unchanged.
- Missing values: base R treats the string NA as missing by default; readr treats both empty fields and NA as missing. A literal value such as the abbreviation NA for North America can be turned into a missing value unless you change the setting.
- Decimal and thousands marks: a file using commas as decimal marks will not parse correctly as numbers with read.csv(); use read.csv2() or set the locale in readr.
- Encoding: accented or non-Latin characters may appear garbled if the file's encoding does not match what the function assumes. readr assumes UTF-8 unless told otherwise; read.csv() accepts a fileEncoding argument, including "UTF-8-BOM" for files saved with a byte-order mark, which otherwise can leave stray characters at the start of the first column name.
- Dates: base R reads dates as character unless you convert them; readr may parse ISO-formatted dates automatically. Ambiguous formats such as 03/04/2025 need an explicit format.
There is also a difference between what is stored and what is displayed. Printing a data frame or opening it with View() shows rounded numbers and truncated text, while the underlying values may carry more digits or longer strings. Check suspicious values with str() or by extracting the column directly rather than trusting the printed view.
Will the data stay usable after copying or sharing?
Copying a range from a spreadsheet and reading it from the clipboard can be quick for one-off work. On a Mac, base R can read the clipboard through the pbpaste command, and readr provides a clipboard() helper. Spreadsheet copies are typically tab separated, so use a tab-aware reader. This approach is not reproducible, since the data is not saved anywhere, and it captures what the spreadsheet displays, which may already be rounded or reformatted.
# Mac: read a copied spreadsheet range
copied <- read.delim(pipe("pbpaste"))
# readr equivalent
copied <- readr::read_tsv(readr::clipboard())
When you write data back out to share, the conversion runs in reverse and can change things again. write.csv() adds a column of row names unless you set row.names = FALSE. readr's write_csv() writes UTF-8 without row names; some versions of spreadsheet software may misread UTF-8 files without a byte-order mark, and readr's write_excel_csv() adds one for that purpose. Leading zeros you preserved in R can still be stripped when someone opens the CSV in a spreadsheet, because that is the spreadsheet's own interpretation, not a property of the file.
For collaborators, a script containing the import call plus the original CSV is more dependable than a saved workspace or a description of menu clicks. Using an RStudio Project, or relative paths from a project folder, keeps paths working on another person's machine; absolute paths such as a specific Mac user folder will fail elsewhere.
On accessibility: RStudio includes accessibility settings, including screen reader support, in its global options. No scoped testing with a specific screen reader was supplied for this article, so how well data previews or the Import Dataset dialog work with a particular assistive technology is not confirmed here. Code-based imports produce output in the console as plain text, which may be easier to navigate than graphical previews, but that should be verified with the assistive technology you use.
When plain text is the better choice
- You need byte-for-byte fidelity, for example when passing a file to another system or verifying its contents. Read lines with readLines() or keep the file untouched.
- The file is not truly a CSV: it is an Excel workbook (use the readxl package), has multi-line headers, or mixes several tables in one file. Forcing it through a CSV reader will misalign columns.
- Every column is an identifier or code. Reading everything as character, for instance colClasses = "character" in read.csv() or col_types = cols(.default = col_character()) in readr, avoids type conversion entirely.
- You only need to search or count text patterns, where a data frame adds nothing.
Quick troubleshooting
- Cannot open file or no such file: the path is wrong or relative to a different working directory. Check getwd() and list.files().
- Everything lands in one column: the delimiter is not a comma. Try read.csv2(), read_delim() with the right delim, or fread().
- Numbers read as text: a non-numeric value, thousands separator or currency symbol is present in the column. Inspect unique values or use readr's problems() after import.
- Garbled characters: the encoding is mismatched. Set fileEncoding in base R or locale(encoding = ...) in readr.
- Strange characters before the first column name: the file has a byte-order mark. Use fileEncoding = "UTF-8-BOM" with read.csv(), or readr, which handles it.
- Function not found: the package is not installed or not loaded. Run install.packages() once and library() each session.
Bottom line
Any of the main options will import a clean CSV into R or RStudio on a Mac or any other platform. The choice matters most when your data has identifiers with leading zeros, non-English characters, unusual delimiters or ambiguous dates. In those cases, prefer an approach that lets you declare column types and encoding explicitly, keep the import as code in a script, and confirm the result with str() before relying on it. If the data must stay exactly as written, treat it as text rather than letting R interpret it.