Comma separated value files (CSVs) can be imported using read.csv
, which wraps read.table
, but uses sep = ","
to set the delimiter to a comma.
# get the file path of a CSV included in R's utils package
csv_path <- system.file("misc", "exDIF.csv", package = "utils")
# path will vary based on installation location
csv_path
## [1] "/Library/Frameworks/R.framework/Resources/library/utils/misc/exDIF.csv"
df <- read.csv(csv_path)
df
## Var1 Var2
## 1 2.70 A
## 2 3.14 B
## 3 10.00 A
## 4 -7.00 A
A user friendly option, file.choose
, allows to browse through the directories:
df <- read.csv(file.choose())
read.table
, read.csv
defaults to header = TRUE
, and uses the first row as column names.factor
class by default unless either as.is = TRUE
or stringsAsFactors = FALSE
.read.csv2
variant defaults to sep = ";"
and dec = ","
for use on data from countries where the comma is used as a decimal point and the semicolon as a field separator.The readr
package's read_csv
function offers much faster performance, a progress bar for large files, and more popular default options than standard read.csv
, including stringsAsFactors = FALSE
.
library(readr)
df <- read_csv(csv_path)
df
## # A tibble: 4 x 2
## Var1 Var2
## <dbl> <chr>
## 1 2.70 A
## 2 3.14 B
## 3 10.00 A
## 4 -7.00 A