R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Importing data is one of the fundamental tasks in data analysis. In R, there are multiple functions and packages that allow users to import data from various sources and formats. In this tutorial, we'll cover how to import data into R from some common formats.
A CSV (Comma Separated Values) file is one of the most common formats for sharing data.
# Using base R mydata <- read.csv("path_to_file.csv", header = TRUE) # Using readr package (part of the tidyverse) library(readr) mydata <- read_csv("path_to_file.csv")
For importing Excel files (both .xls
and .xlsx
):
# Using the readxl package library(readxl) mydata <- read_excel("path_to_file.xlsx")
Text files can come with various delimiters such as tabs, spaces, or semicolons.
# Tab-delimited file mydata <- read.table("path_to_file.txt", header = TRUE, sep = "\t") # Semicolon-delimited file mydata <- read.table("path_to_file.txt", header = TRUE, sep = ";")
RData files are a binary format that R uses to store one or more objects.
load("path_to_file.RData")
To connect to databases, you often need to use specific R packages based on the database type. For instance, to connect to a MySQL database:
library(DBI) library(RMySQL) # Create a connection con <- dbConnect(RMySQL::MySQL(), dbname = "database_name", host = "host_name", user = "user", password = "password") # Import data mydata <- dbReadTable(con, "table_name") # Close the connection dbDisconnect(con)
You can import data directly from a URL. This is useful for web-based datasets.
# For CSV data available online mydata <- read.csv("http://web_address/path_to_file.csv")
If you use RStudio:
Environment
tab.Import Dataset
dropdown.From Text (readr)...
).Always inspect your data immediately after importing to ensure that it was read correctly. Functions like head()
, str()
, or summary()
can be useful.
When working with large datasets or unfamiliar formats, consider checking if there's a specialized R package for your data format. These specialized packages often provide more efficient or user-friendly tools.
Remember, data importing is the starting point for most analyses, so it's important to be familiar with the tools and techniques that ensure your data is correctly and efficiently loaded into R.
Import data in R script:
# Import data in R script data <- read.table("your_data_file.txt", header = TRUE)
Reading data into R script:
# Reading data into R script data <- read.table("your_data_file.txt", header = TRUE)
Loading datasets in R script:
read.table()
or specialized packages.# Loading datasets in R script data <- read.table("your_data_file.txt", header = TRUE)
R read.table in script:
read.table()
function in R is commonly used to read tabular data from text files into a data frame.# R read.table in script data <- read.table("your_data_file.txt", header = TRUE)
Importing CSV data in R script:
read.csv()
function simplifies this process.# Importing CSV data in R script data <- read.csv("your_csv_file.csv")
Reading Excel files in R script:
readxl
or openxlsx
.# Reading Excel files in R script library(readxl) data <- read_excel("your_excel_file.xlsx")
R readr package in script:
readr
package in R provides efficient functions for reading various data formats in scripts.# R readr package in script library(readr) data <- read_csv("your_data_file.csv")
Importing data from text files in R script:
read.table()
or readLines()
.# Importing data from text files in R script data <- read.table("your_text_file.txt", header = TRUE)
Reading JSON data in R script:
jsonlite
package.# Reading JSON data in R script library(jsonlite) data <- fromJSON("your_json_file.json")
Importing data from databases in R script:
DBI
and database-specific packages.# Importing data from databases in R script library(DBI) con <- dbConnect(RSQLite::SQLite(), dbname = "your_database.db") query <- "SELECT * FROM your_table" data <- dbGetQuery(con, query)