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
In R, there are various functions available for importing data from different types of files. Here's a general guideline on how to import data from several common file formats:
To read data from a CSV (Comma Separated Values) file, you can use the read.csv()
function.
# Import data from a CSV file mydata <- read.csv("path_to_file.csv", header = TRUE)
header = TRUE
indicates that the first row of the CSV file contains column names.For reading Excel files (both .xls
and .xlsx
), the readxl
package is quite handy.
install.packages("readxl") library(readxl) # Import data from an Excel file mydata <- read_excel("path_to_file.xlsx")
To read data from a text file, use the read.table()
function. It's a general-purpose reader function from which read.csv()
is derived. So, while read.csv()
expects commas as field separators, read.table()
is more general and can handle other delimiters.
# Import data from a text file with tab as the delimiter mydata <- read.table("path_to_file.txt", header = TRUE, sep = "\t")
The haven
package provides functions to read datasets from software like SPSS, SAS, and Stata.
install.packages("haven") library(haven) # Import data from an SPSS file mydata_spss <- read_spss("path_to_file.sav") # Import data from a SAS file mydata_sas <- read_sas("path_to_file.sas7bdat") # Import data from a Stata file mydata_stata <- read_dta("path_to_file.dta")
RData
(or .rda
) is a file format for storing one or more R objects.
load("path_to_file.RData")
When you load an RData file, it restores the saved objects into your R environment, so you don't need to assign it to a new variable.
If you are using RStudio, you can use its GUI:
Environment
tab.Import Dataset
dropdown button.From Text (readr)...
).It's crucial to understand your data and the format it's in, so you can choose the appropriate import function in R. Each function comes with its own set of arguments to handle nuances in the data, so refer to the relevant documentation (?function_name
) to make the most of these functions.
Import data from file in R:
# Import data from file in R data <- read.table("your_data_file.txt", header = TRUE)
Reading data into R from a file:
read.table()
or read.csv()
depending on the file format.# Reading data into R from a file data <- read.table("your_data_file.txt", header = TRUE)
Loading data into R with read.csv:
read.csv()
function is specifically designed for importing data from CSV files into R.# Loading data into R with read.csv data <- read.csv("your_data_file.csv")
Importing Excel data into R:
readxl
or openxlsx
.# Importing Excel data into R library(readxl) data <- read_excel("your_excel_file.xlsx")
R readr package for data import:
readr
package in R provides efficient functions for reading various data formats.# R readr package for data import library(readr) data <- read_csv("your_data_file.csv")
Reading data from text files in R:
read.table()
or readLines()
.# Reading data from text files in R data <- read.table("your_text_file.txt", header = TRUE)
Importing data from CSV files in R:
read.csv()
for this purpose.# Importing data from CSV files in R data <- read.csv("your_csv_file.csv")
Reading Excel files in R:
readxl
or openxlsx
.# Reading Excel files in R library(readxl) data <- read_excel("your_excel_file.xlsx")