Import CSV into RStudio
How to import a CSV file into RStudio with read.csv, readr or the Import Dataset menu, what the import changes about your data, and how to check that nothing was lost.
Importing a CSV file into RStudio converts plain delimited text into an R data frame you can analyze. The original file is not modified. What changes is how R interprets each value: as a number, a date, a piece of text or a missing value. Most import problems come from that interpretation step, not from loading the file. This review covers the main import routes, what each one does to your data, and when to override the defaults.
Which import method suits your purpose?
For most work, use readr::read_csv() or base R's read.csv() in a script rather than clicking through menus. A script makes the import repeatable and documents every parsing decision. The main tradeoff is between convenience and control. The point-and-click Import Dataset dialog is the quickest way to see a file for the first time. Explicit code with stated column types is safer when exact values matter, such as ZIP codes, IDs with leading zeros, or dates in a non-standard format.
Opening the CSV as plain text is the better choice when you only need to inspect the raw contents or confirm the delimiter. It is also the right place to diagnose a file that imports incorrectly. In RStudio, you can open the file in the editor or view the first lines with readLines(). Either way, you see the characters exactly as stored, before any parsing.
The main ways to import a CSV in RStudio
| Method | Where it comes from | Returns | Notes |
| read.csv() | Base R (utils package), no install needed | data.frame | Assumes a header row and comma separator by default; cleans column names |
| readr::read_csv() | readr package (part of the tidyverse) | tibble | Guesses column types from a sample of rows; keeps column names as written |
| data.table::fread() | data.table package | data.table | Detects the separator automatically; built for large files |
| File > Import Dataset | RStudio menu | Depends on the option chosen (base or readr) | Shows a preview and generates import code you can copy into a script |
Basic examples of each code route:
# Base R
sales <- read.csv("data/sales.csv")
# readr
library(readr)
sales <- read_csv("data/sales.csv")
# data.table
library(data.table)
sales <- fread("data/sales.csv")
The file path is resolved relative to the current working directory, which you can check with getwd(). An RStudio Project sets the working directory to the project folder, so relative paths like the ones above work the same way each time. On Windows, write paths with forward slashes, or double each backslash, because a single backslash is an escape character in R strings. file.choose() opens a file picker, but the chosen path is not recorded in your script.
Using the Import Dataset menu
In the Environment pane or the File menu, choose Import Dataset and then the text option, either base or readr. RStudio shows a preview of the parsed data and lets you change the delimiter, header handling, column types and missing-value markers. It also displays the code it will run. Copy that code into your script so the import can be reproduced without repeating the clicks.
What the import actually changes
A CSV file stores only text. Importing it makes several interpretive decisions, and each one can change what a value means even though the source file stays the same:
- Column types: values that look numeric become numbers, so leading zeros in codes such as 02134 are dropped unless the column is read as character.
- Missing values: read.csv() treats "NA" as missing by default, while read_csv() treats both empty cells and "NA" as missing. A blank string and a missing value can therefore come out differently depending on the function.
- Column names: read.csv() converts names into syntactic R names, so "Order Date" becomes "Order.Date". read_csv() keeps the original name, which you then refer to in backticks.
- Text columns: in R 4.0 and later, read.csv() keeps text as character by default instead of converting it to factors. Older code may rely on the earlier factor behavior.
- Dates: read_csv() recognizes ISO-style dates such as 2024-03-15 automatically. Other formats usually stay as text until you parse them explicitly.
Separate the stored data from how it is displayed. Printing a tibble shows only a few rows, shortens wide columns and rounds displayed numbers. The underlying values are unchanged. To check what was actually imported, use str() or glimpse(), or open the data viewer with View(). Do not rely on the console printout alone.
Controlling the import when defaults are wrong
When a column's meaning depends on its exact text, declare its type instead of letting R guess. readr guesses types from an initial sample of rows, so a column whose unusual values appear only later in the file can be guessed wrongly. Specifying the type removes that risk:
library(readr)
customers <- read_csv(
"data/customers.csv",
col_types = cols(
zip = col_character(),
signup_date = col_date(format = "%m/%d/%Y"),
.default = col_guess()
),
na = c("", "NA", "N/A")
)
problems(customers)
The equivalent in base R uses colClasses, for example colClasses = c(zip = "character"). After a readr import, problems() lists any values that could not be parsed into the declared type. Check it whenever you expect clean data.
Delimiters, TSV files and regional formats
Not every file named .csv is separated by commas. Files exported with European regional settings often use semicolons as the separator and commas as the decimal mark. read.csv2() and read_csv2() expect that format. Tab-separated files are read with read.delim() or read_tsv(). For any other separator, use read.table() with the sep argument or read_delim() with the delim argument. fread() usually detects the separator itself, but confirm the result rather than assuming it is correct.
Encoding and special characters
Accented letters or symbols that appear garbled after import usually indicate an encoding mismatch. The file was saved in one encoding and read as another. readr assumes UTF-8 unless told otherwise through locale(encoding = ...). base R accepts a fileEncoding argument. Some spreadsheet programs add a byte order mark at the start of UTF-8 files, which can show up as stray characters in the first column name. In base R, fileEncoding = "UTF-8-BOM" handles this. Which encoding a particular export tool produces depends on that tool and its settings. If you are unsure, check the exporting program's documentation.
Will the result stay usable when shared?
An imported data frame exists only in your R session. To share the result, share the script and the original CSV, or write a cleaned copy with write.csv() or readr::write_csv(). When you write a file back out, the type decisions are flattened into text again. A recipient who reads it with different defaults may get different types, for example losing leading zeros a second time. A script with explicit column types is the most dependable way to ensure someone else gets the same result.
Before relying on an import, a short check confirms the main risks:
- Row and column counts match what you expect from the source file.
- Identifier and code columns are character, not numeric.
- Dates are stored as dates, not as text.
- The number of missing values in each column is plausible.
- Non-ASCII text appears correctly.
- problems() reports nothing unexpected after a readr import.
Gaps and limits of this review
This overview describes documented default behavior of base R, readr and data.table, plus the general workflow of RStudio's Import Dataset dialog. It does not include benchmark measurements, file-size limits or tests against specific files. Defaults and menu labels can change between package and RStudio versions. Confirm the exact behavior of your installation with the built-in help, for example ?read.csv or ?readr::read_csv.