Installing Packages and Importing Data in R
August 01, 2024
R
packagesggplot2
, dplyr
, and tidyr
R
R
R
-specific objects using RDS filesR
PackagesR
Packages and Their ImportanceR
functionality is enhanced by packages—which are like “apps” in the smartphone ecosystem.
In space, no one can hear you scream.
– Alien (1979)
The tidyverse is a collection of R packages designed for data science. All packages share an underlying philosophy and common APIs.
You can install packages in two ways:
R
command line with install.packages()
RStudio
, by navigating to the “Packages” tab and searching for the desired package.After installation, you must load a package to use it:
Note: Some commonly used packages for data analysis are
ggplot2
,dplyr
, andtidyr
.
R
R
R
allows importing data from various file formats like text files, Excel sheets, CSV files, and SQL databases.
Make sure you have the readxl
package installed:
To import a specific sheet:
CSV
files are commonly used and can be easily imported using read.csv()
:
SQL
DatabasesTo import data from a SQL
database, you can use the RODBC
package:
Note: For this course, we will primarily focus on importing data from Excel spreadsheets.
R
R
Once your data analysis is complete, you’ll often need to export the data for further use or reporting. R
provides several ways to export datasets to various formats, including CSV, Excel, and text files.
CSV
FilesOne of the most common way to export data from R
is to save it as a CSV
file using the write.csv()
function.
row.names = FALSE
avoids adding an extra column for row numbers.You can export data to Excel using the writexl
package. First, make sure it’s installed.
Note: The
write_xlsx()
function saves the data into an Excel file, and you can specify the file path.
For exporting data to a text file, you can use the write.table()
function. This is particularly useful when you want to use a delimiter other than commas, such as tabs.
sep
argument specifies the delimiter used in the file (in this case, tabs).RDS
FilesRDS is a format specific to R
that allows you to save R
objects and reload them later.
Note:
RDS
files are useful when you want to save R objects for later use within R itself.
Packages are essential in extending R
’s functionality.
You can install and load packages easily with install.packages()
and library()
functions.
R
supports importing data from multiple sources, including text files, Excel
sheets, CSV
files, and SQL
databases.
You can export datasets to various formats in R, including CSV, Excel, text files, and RDS.
write.csv()
and write_xlsx()
are common functions for CSV and Excel exports.write.table()
allows for more customizable exports, such as tab-delimited files.saveRDS()
and readRDS()
for saving and reloading R-specific objects.Data Mining Lab