4 min read•august 14, 2024
Data manipulation often starts with importing and exporting data. This section covers reading and writing various file formats like , , and databases in R. We'll explore functions and packages that make data transfer between R and other sources seamless.
Understanding these techniques is crucial for effective data analysis. By mastering data import and export, you'll be able to work with diverse datasets, collaborate with others using different tools, and integrate R into your data workflow effortlessly.
[read.csv()](https://www.fiveableKeyTerm:read.csv())
function to read data from a comma-separated values (CSV) file into an R data frame
[read.table()](https://www.fiveableKeyTerm:read.table())
function for reading tabular data from a file into an R data frame
[read_excel()](https://www.fiveableKeyTerm:read_excel())
and read_xls()
functions are available for this purpose[sqldf()](https://www.fiveableKeyTerm:sqldf())
function is used for this purpose[write.csv()](https://www.fiveableKeyTerm:write.csv())
function to write an R data frame to a comma-separated values (CSV) file
[write.table()](https://www.fiveableKeyTerm:write.table())
function for writing tabular data from an R data frame to a file
[write_xlsx()](https://www.fiveableKeyTerm:write_xlsx())
function from the writexl packagesqldf()
function from the sqldf package and appropriate SQL INSERT statements[dbConnect()](https://www.fiveableKeyTerm:dbconnect())
function from the DBI package
[dbGetQuery()](https://www.fiveableKeyTerm:dbGetQuery())
function from the DBI package[dbWriteTable()](https://www.fiveableKeyTerm:dbwritetable())
function from the DBI packagesqldf()
function is used for this purposefileEncoding
argument in read.csv()
and read.table()
functions to specify the encoding of the input filena.strings
argument in data import functions to specify the values to be treated as missing (NA) in RcolClasses
argument in data import functions to specify the desired data types for each columnfread()
from the data.table package or read_csv()
from the readr package for faster and memory-efficient data importtry()
or tryCatch()
, to gracefully handle connection failures