SlideShare a Scribd company logo
r-squared
Slide 1 www.r-squared.in/rprogramming
R Programming
Learn the fundamentals of data analysis with R.
r-squared
Slide 2
Course Modules
www.r-squared.in/rprogramming
✓ Introduction
✓ Elementary Programming
✓ Working With Data
✓ Selection Statements
✓ Loops
✓ Functions
✓ Debugging
✓ Unit Testing
r-squared
Slide 3
Working With Data
www.r-squared.in/rprogramming
✓ Data Types
✓ Data Structures
✓ Data Creation
✓ Data Info
✓ Data Subsetting
✓ Comparing R Objects
✓ Importing Data
✓ Exporting Data
✓ Data Transformation
✓ Numeric Functions
✓ String Functions
✓ Mathematical Functions
r-squared
Slide 4
Importing Data In R
www.r-squared.in/rprogramming
Objectives
In this module, we will learn to:
● Read data from the console
● Read data from files
● Import data from
○ Text/Excel/CSV files
○ Stata/SAS/SPSS files
● Load .Rdata files
● Source R scripts
r-squared
Slide 5
Read Data From Console
www.r-squared.in/rprogramming
In this section, we will learn to read data from the console interactively and store them as
R objects using the following functions:
✓ scan
✓ readline
r-squared
Slide 6
scan() (1/4)
www.r-squared.in/rprogramming
Description:
scan() allows user to input data from console or from a file and stores the input in a
vector or list.
Syntax:
x <- scan() # stores input as vector
x <- scan("", what = integer()) # stores input as integer
x <- scan("", what = list()) # stores input as list
Returns:
A vector or list of the input data.
Documentation
help(scan)
r-squared
Slide 7
scan() (2/4)
www.r-squared.in/rprogramming
Examples
> # example 1
> x <- scan()
1: 1
2: 2
3: 3
4:
Read 3 items
# to end input, do not enter anything.
> x
[1] 1 2 3
> typeof(x)
[1] "double"
# if numbers are entered, they will be stored as double. In the next example, we will learn
how to store numbers as integers.
r-squared
Slide 8
scan() (3/4)
www.r-squared.in/rprogramming
Examples
> # example 2
> x <- scan("", what = integer())
1: 1
2: 2
3: 3
4:
Read 3 items
# mention the data type in the what argument to store the data in the preferred mode.
> x
[1] 1 2 3
> typeof(x)
[1] "integer"
r-squared
Slide 9
scan() (4/4)
www.r-squared.in/rprogramming
Examples
> # example 3
> x <- scan("", what = list(name = "", age = 0))
1: Jovial 28
2: Manual 27
3: Funnel 25
4: Tunnel 29
5:
Read 4 records
# suppose we want the user to enter multiple attributes and store the input in a list. Use
list in the what argument with the names for the attributes.
> x
$name
[1] "Jovial" "Manual" "Funnel" "Tunnel"
$age
[1] 28 27 25 29
r-squared
Slide 10
readline() (1/3)
www.r-squared.in/rprogramming
Description:
readline() prompts the user for an input and stores the input as a character vector.
Syntax:
readline(prompt = "")
Returns:
A character vector of the input data.
Documentation
help(readline)
r-squared
Slide 11
readline() (2/3)
www.r-squared.in/rprogramming
Examples
> # example 1
> x <- readline(prompt = "Enter your name: ")
Enter your name: Jovial
> x
[1] "Jovial"
> class(x)
[1] "character"
# input is stored as character type. It has to be converted to other data types as necessary.
In the next example, we will input a number and then store it as an integer.
r-squared
Slide 12
readline() (3/3)
www.r-squared.in/rprogramming
Examples
> # example 2
> x <- readline(prompt = "Enter your age: ")
Enter your age: 28
> x
[1] "28"
> class(x)
[1] "character"
> x <- as.integer(x)
> x
[1] 28
r-squared
Slide 13
Read Data From Files
www.r-squared.in/rprogramming
In this section, we will learn to read data from files using the following functions:
✓ scan
✓ readLines
r-squared
Slide 14
scan() (1/5)
www.r-squared.in/rprogramming
Description:
scan() allows user to input data from console or from a file and stores the input in a vector
or list.
Syntax:
scan(file = "", what = double(), nmax = -1L, n = -1L, sep = "",
quote = if (identical(sep, "n")) "" else "'"", dec = ".",
skip = 0L, nlines = 0L, na.strings = "NA", flush = FALSE,
fill = FALSE, strip.white = FALSE, quiet = FALSE, blank.lines.skip = TRUE,
multi.line = TRUE, comment.char = "", allowEscapes = FALSE,
fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)
Returns:
A vector or list of the input data.
Documentation
help(scan)
r-squared
Slide 15
scan() (2/5)
www.r-squared.in/rprogramming
Arguments:
file: name of the file from which the data must be read.
what: mode in which data must be stored.
nmax: maximum number of data values or lines to be read from a file.
n: maximum number of data values to be read.
sep: delimiter
skip: number of lines to be skipped before reading reading data from a file.
nlines: maximum number of lines to be read from a file.
quiet: how many items have been read.
blank.lines.skip: if blank lines must be skipped.
multi.line: whether all lines must appear in one line or multi-line.
r-squared
Slide 16
scan() (3/5)
www.r-squared.in/rprogramming
Examples
> # example 1
> scan("words.txt", what = character(), skip = 2, nlines = 2,
+ quiet = TRUE)
[1] "Morbi" "consequat" "commodo" "orci" "ut" "volutpat."
[7] "Sed" "accumsan" "eleifend" "egestas." "Nullam" "ac"
[13] "posuere" "eros." "Donec" "rutrum" "gravida" "felis,"
[19] "quis" "fermentum" "orci." "Pellentesque" "purus" "lacus,"
[25] "tincidunt" "eget" "enim" "ut," "facilisis" "rutrum"
[31] "odio."
# read two lines from the file “words.txt” as type character after skipping the first two
lines and do not print the number of lines read on the console.
r-squared
Slide 17
scan() (4/5)
www.r-squared.in/rprogramming
Examples
> # example 2
> scan("words.txt", what = list("", ""), skip = 2, nlines = 2, sep = " ",
+ quiet = TRUE)
[[1]]
[1] "Morbi" "commodo" "ut" "Sed" "eleifend" "Nullam" "posuere"
[8] "Donec" "gravida" "quis" "orci." "purus" "tincidunt" "enim"
[15] "facilisis" "odio."
[[2]]
[1] "consequat" "orci" "volutpat." "accumsan" "egestas." "ac"
[7] "eros." "rutrum" "felis," "fermentum" "Pellentesque" "lacus,"
[13] "eget" "ut," "rutrum" ""
# read two lines from the file “words.txt” as a list, after skipping the first two lines and
do not print the number of lines read on the console.
r-squared
Slide 18
scan() (5/5)
www.r-squared.in/rprogramming
Examples
> # example 3
> scan("words.txt", what = list("", "", ""), skip = 2, nlines = 3, sep = " ",
+ quiet = TRUE)
[[1]]
[1] "Morbi" "orci" "Sed" "egestas." "posuere" "Donec" "felis,"
[8] "orci." "lacus," "enim" "rutrum" "Donec" "tincidunt" "eu,"
[15] "tortor." "turpis" "bibendum"
[[2]]
[1] "consequat" "ut" "accumsan" "Nullam" "eros." "rutrum"
[7] "quis" "Pellentesque" "tincidunt" "ut," "odio." "mi"
[13] "a" "euismod" "In" "vel" "posuere."
[[3]]
[1] "commodo" "volutpat." "eleifend" "ac" "" "gravida"
[7] "fermentum" "purus" "eget" "facilisis" "" "urna,"
[13] "sollicitudin" "non" "dignissim" "lorem" ""
# read three lines from the file “words.txt” as a list, after skipping the first two lines
and do not print the number of lines read on the console.
r-squared
Slide 19
readLines() (1/3)
www.r-squared.in/rprogramming
Description:
readLines() allows user to input data from console or from a file and stores the input in a
vector or list.
Syntax:
readLines(file_name)
Returns:
A vector of the input data.
Documentation
help(readLines)
r-squared
Slide 20
readLines() (2/3)
www.r-squared.in/rprogramming
Examples
> # example 1
> readLines("words.txt")
[1] "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sodales nulla quis interdum
dictum. "
[2] "Maecenas molestie suscipit libero lobortis ornare. Nam quam magna, tincidunt id
vulputate nec, elementum ac lorem. "
[3] "Morbi consequat commodo orci ut volutpat. Sed accumsan eleifend egestas. Nullam ac
posuere eros. "
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
[15] "Vivamus pulvinar consectetur tellus, quis mollis libero lobortis at. "
[16] "Quisque tincidunt purus fermentum augue auctor ultricies."
[17] ""
# reads all the lines from the file
r-squared
Slide 21
readLines() (3/3)
www.r-squared.in/rprogramming
Examples
> # example 2
> readLines("words.txt", n = 5)
[1] "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sodales nulla quis interdum
dictum. "
[2] "Maecenas molestie suscipit libero lobortis ornare. Nam quam magna, tincidunt id
vulputate nec, elementum ac lorem. "
[3] "Morbi consequat commodo orci ut volutpat. Sed accumsan eleifend egestas. Nullam ac
posuere eros. "
[4] "Donec rutrum gravida felis, quis fermentum orci. Pellentesque purus lacus, tincidunt
eget enim ut, facilisis rutrum odio. "
[5] "Donec mi urna, tincidunt a sollicitudin eu, euismod non tortor. In dignissim turpis vel
lorem bibendum posuere. "
# reads the first 5 lines from the file.
r-squared
Slide 22
Import Data Files
www.r-squared.in/rprogramming
In this section, we will learn to import the following data files:
✓ Text file
✓ Excel/CSV file
✓ Stata file
✓ SAS file
✓ SPSS file
r-squared
Slide 23
Importing Text File
www.r-squared.in/rprogramming
Description:
read.table() reads a file in table format and creates a data frame from it.
Syntax:
read.table(file_name, header, sep)
Returns:
A data frame.
Documentation
help(read.table)
r-squared
Slide 24
read.table()
www.r-squared.in/rprogramming
Examples
> # example 1
> # read data from a semicolon delimited file and retain the column names
> text_data <- read.table("data.txt", header = TRUE, sep = ";")
> # example 2
> # read data from a comma delimited file and retain the column names
> text_data1 <- read.table("data1.txt", header = TRUE, sep = ",")
> # example 3
> # read data from a tab delimited file and retain the column names
> text_data2 <- read.table("data2.txt", header = TRUE, sep = "t")
r-squared
Slide 25
read.csv()
www.r-squared.in/rprogramming
Description:
read.csv() reads a CSV file in table format and creates a data frame from it.
Syntax:
read.csv(file, header = TRUE, sep = ",", quote = """, dec = ".",
fill = TRUE, comment.char = "", ...)
Returns:
A data frame.
Documentation
help(read.csv)
r-squared
Slide 26
read.csv()
www.r-squared.in/rprogramming
Examples
> # example 1
> # read data from a CSV file and retain the column names
> data_csv <- read.csv("data.csv", header = TRUE)
> # example 2
> # read data from a CSV file without the column names
> data_csv <- read.csv("data.csv", header = FALSE)
> # example 3
> # read data from a CSV file and retain the column names and add blank fields
> # when rows are of unequal length
> data_csv <- read.csv("data.csv", header = TRUE, fill = TRUE)
r-squared
Slide 27
read.xls()
www.r-squared.in/rprogramming
Description:
read.xls() reads an excel file in table format and creates a data frame from it. You need
to install the gdata package in order to use the read.xls() function.
Syntax:
read.xls(file, sheet)
Returns:
A data frame.
Documentation:
library(gdata)
help(read.xls)
r-squared
Slide 28
read.xls()
www.r-squared.in/rprogramming
Examples
> # example 1
> # read data from a excel file
> data_xls <- read.xls("data.csv")
> # example 2
> # read data from a particular sheet in a excel file
> data_xls <- read.xls("data.csv", sheet = 1)
r-squared
Slide 29
Stata File
www.r-squared.in/rprogramming
Description
read.dta() reads a Stata binary file into a data frame.
Package
Install the foreign package to import stata files.
Syntax
read.csv(file, convert.dates = TRUE, convert.factors = TRUE, missing.type
= FALSE, convert.underscore = FALSE, warn.missing.labels = TRUE)
Returns
A data frame.
Documentation
help(read.dta)
r-squared
Slide 30
read.dta()
www.r-squared.in/rprogramming
Examples
> # example 1
> install.packages("foreign")
> library(foreign)
> data_stata <- read.dta("auto.dta")
r-squared
Slide 31
SPSS File
www.r-squared.in/rprogramming
Description
read.spss() reads a SPSS file into a data frame.
Package
Install the foreign package to import stata files.
Syntax
read.spss(file, use.value.labels = TRUE, to.data.frame = FALSE, max.value.labels =
Inf, trim.factor.names = FALSE, trim_values = TRUE, reencode = NA, use.missings =
to.data.frame)
Returns
A data frame.
Documentation
help(read.spss)
r-squared
Slide 32
read.spss()
www.r-squared.in/rprogramming
Examples
> # example 1
> install.packages("foreign")
> library(foreign)
> data_spss <- read.spss("binary.sav")
r-squared
Slide 33
SAS File
www.r-squared.in/rprogramming
Description
read.sas7bdat() reads SAS files in the sas7bdat data format into a dataframe.
Package
Install the sas7bdat package to import stata files.
Syntax:
read.sas7bdat(file, debug=FALSE)
Returns:
A data frame.
Documentation
help(read.sas7bdat)
r-squared
Slide 34
read.sas7bdat()
www.r-squared.in/rprogramming
Examples
> # example 1
> install.packages("sas7bdat")
> library(sas7bdat)
> data_sas <- read.sas7bdat("crime.sas7bdat")
r-squared
Slide 35
load()
www.r-squared.in/rprogramming
Description
load() reloads saved datasets and workspaces. Datasets and workspaces have the
extension .RData
Syntax:
load(file)
Returns:
R object or workspace.
Documentation
help(load)
Example
> load("x.RData")
r-squared
Slide 36
source()
www.r-squared.in/rprogramming
Description
source() reads R codes from a file and makes those codes available in the current
session. R scripts have the extension .R
Syntax:
source(file_name, file_path)
Returns
Codes from a R file.
Documentation
help(source)
Example
> source("functions.R")
r-squared
Slide 37
Next Steps...
www.r-squared.in/rprogramming
In the next unit, we will learn to export data from R:
● Output data to the console
● Output data to files
● Export data into text/CSV files
● Save R objects
r-squared
Slide 38
Connect With Us
www.r-squared.in/rprogramming
Visit r-squared for tutorials
on:
● R Programming
● Business Analytics
● Data Visualization
● Web Applications
● Package Development
● Git & GitHub
Ad

Recommended

Data Management in R
Data Management in R
Sankhya_Analytics
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
Malla Reddy University
 
Data tidying with tidyr meetup
Data tidying with tidyr meetup
Matthew Samelson
 
Basic Concept of Database
Basic Concept of Database
Marlon Jamera
 
Introduction to database & sql
Introduction to database & sql
zahid6
 
R data-import, data-export
R data-import, data-export
FAO
 
Elementary data organisation
Elementary data organisation
Muzamil Hussain
 
Query optimization in SQL
Query optimization in SQL
Abdul Rehman
 
SQL commands
SQL commands
GirdharRatne
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
krishna singh
 
Introduction to pandas
Introduction to pandas
Piyush rai
 
Introduction to R Programming
Introduction to R Programming
izahn
 
Relational algebra.pptx
Relational algebra.pptx
RUpaliLohar
 
DBMS Practical File
DBMS Practical File
Dushmanta Nath
 
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ARADHYAYANA
 
Normalization in DBMS
Normalization in DBMS
Hitesh Mohapatra
 
Database security
Database security
Software Engineering
 
Red black trees
Red black trees
mumairsadiq
 
Dbms lab questions
Dbms lab questions
Parthipan Parthi
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
single linked list
single linked list
Sathasivam Rangasamy
 
5 the relational algebra and calculus
5 the relational algebra and calculus
Kumar
 
SQL Basics
SQL Basics
Hammad Rasheed
 
Dbms relational model
Dbms relational model
Chirag vasava
 
Algorithmic Notations
Algorithmic Notations
Muhammad Muzammal
 
MYSQL.ppt
MYSQL.ppt
webhostingguy
 
Database Triggers
Database Triggers
Aliya Saldanha
 
Entity Relationship Diagrams
Entity Relationship Diagrams
sadique_ghitm
 
R Programming: Export/Output Data In R
R Programming: Export/Output Data In R
Rsquared Academy
 
RDataMining slides-r-programming
RDataMining slides-r-programming
Yanchang Zhao
 

More Related Content

What's hot (20)

SQL commands
SQL commands
GirdharRatne
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
krishna singh
 
Introduction to pandas
Introduction to pandas
Piyush rai
 
Introduction to R Programming
Introduction to R Programming
izahn
 
Relational algebra.pptx
Relational algebra.pptx
RUpaliLohar
 
DBMS Practical File
DBMS Practical File
Dushmanta Nath
 
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ARADHYAYANA
 
Normalization in DBMS
Normalization in DBMS
Hitesh Mohapatra
 
Database security
Database security
Software Engineering
 
Red black trees
Red black trees
mumairsadiq
 
Dbms lab questions
Dbms lab questions
Parthipan Parthi
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
single linked list
single linked list
Sathasivam Rangasamy
 
5 the relational algebra and calculus
5 the relational algebra and calculus
Kumar
 
SQL Basics
SQL Basics
Hammad Rasheed
 
Dbms relational model
Dbms relational model
Chirag vasava
 
Algorithmic Notations
Algorithmic Notations
Muhammad Muzammal
 
MYSQL.ppt
MYSQL.ppt
webhostingguy
 
Database Triggers
Database Triggers
Aliya Saldanha
 
Entity Relationship Diagrams
Entity Relationship Diagrams
sadique_ghitm
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
krishna singh
 
Introduction to pandas
Introduction to pandas
Piyush rai
 
Introduction to R Programming
Introduction to R Programming
izahn
 
Relational algebra.pptx
Relational algebra.pptx
RUpaliLohar
 
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ARADHYAYANA
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
5 the relational algebra and calculus
5 the relational algebra and calculus
Kumar
 
Dbms relational model
Dbms relational model
Chirag vasava
 
Entity Relationship Diagrams
Entity Relationship Diagrams
sadique_ghitm
 

Similar to R Programming: Importing Data In R (20)

R Programming: Export/Output Data In R
R Programming: Export/Output Data In R
Rsquared Academy
 
RDataMining slides-r-programming
RDataMining slides-r-programming
Yanchang Zhao
 
r,rstats,r language,r packages
r,rstats,r language,r packages
Ajay Ohri
 
R basics
R basics
Sagun Baijal
 
R Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB Academy
rajkamaltibacademy
 
R1-Intro (2udsjhfkjdshfkjsdkfhsdkfsfsffs
R1-Intro (2udsjhfkjdshfkjsdkfhsdkfsfsffs
sabari Giri
 
Lecture1_R Programming Introduction1.ppt
Lecture1_R Programming Introduction1.ppt
premak23
 
R_Language_study_forstudents_R_Material.ppt
R_Language_study_forstudents_R_Material.ppt
Suresh Babu
 
Brief introduction to R Lecturenotes1_R .ppt
Brief introduction to R Lecturenotes1_R .ppt
geethar79
 
Lecture1_R.ppt
Lecture1_R.ppt
ArchishaKhandareSS20
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
anshikagoel52
 
Lecture1 r
Lecture1 r
Sandeep242951
 
Lecture1_R.ppt
Lecture1_R.ppt
vikassingh569137
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
Nico Ludwig
 
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
The Statistical and Applied Mathematical Sciences Institute
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
Yashpatel821746
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Yashpatel821746
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
Yashpatel821746
 
Lecture1_R.pdf
Lecture1_R.pdf
BusyBird2
 
Basics of R programming for analytics [Autosaved] (1).pdf
Basics of R programming for analytics [Autosaved] (1).pdf
suanshu15
 
R Programming: Export/Output Data In R
R Programming: Export/Output Data In R
Rsquared Academy
 
RDataMining slides-r-programming
RDataMining slides-r-programming
Yanchang Zhao
 
r,rstats,r language,r packages
r,rstats,r language,r packages
Ajay Ohri
 
R Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB Academy
rajkamaltibacademy
 
R1-Intro (2udsjhfkjdshfkjsdkfhsdkfsfsffs
R1-Intro (2udsjhfkjdshfkjsdkfhsdkfsfsffs
sabari Giri
 
Lecture1_R Programming Introduction1.ppt
Lecture1_R Programming Introduction1.ppt
premak23
 
R_Language_study_forstudents_R_Material.ppt
R_Language_study_forstudents_R_Material.ppt
Suresh Babu
 
Brief introduction to R Lecturenotes1_R .ppt
Brief introduction to R Lecturenotes1_R .ppt
geethar79
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
anshikagoel52
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
Nico Ludwig
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
Yashpatel821746
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Yashpatel821746
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
Yashpatel821746
 
Lecture1_R.pdf
Lecture1_R.pdf
BusyBird2
 
Basics of R programming for analytics [Autosaved] (1).pdf
Basics of R programming for analytics [Autosaved] (1).pdf
suanshu15
 
Ad

More from Rsquared Academy (20)

Handling Date & Time in R
Handling Date & Time in R
Rsquared Academy
 
Market Basket Analysis in R
Market Basket Analysis in R
Rsquared Academy
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
Rsquared Academy
 
Joining Data with dplyr
Joining Data with dplyr
Rsquared Academy
 
Explore Data using dplyr
Explore Data using dplyr
Rsquared Academy
 
Data Wrangling with dplyr
Data Wrangling with dplyr
Rsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with Pipes
Rsquared Academy
 
Introduction to tibbles
Introduction to tibbles
Rsquared Academy
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into R
Rsquared Academy
 
Read/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into R
Rsquared Academy
 
Variables & Data Types in R
Variables & Data Types in R
Rsquared Academy
 
How to install & update R packages?
How to install & update R packages?
Rsquared Academy
 
How to get help in R?
How to get help in R?
Rsquared Academy
 
Introduction to R
Introduction to R
Rsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
Rsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
Rsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
R Programming: Introduction to Matrices
R Programming: Introduction to Matrices
Rsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
R Programming: Variables & Data Types
R Programming: Variables & Data Types
Rsquared Academy
 
Market Basket Analysis in R
Market Basket Analysis in R
Rsquared Academy
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
Rsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with Pipes
Rsquared Academy
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into R
Rsquared Academy
 
Read/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into R
Rsquared Academy
 
Variables & Data Types in R
Variables & Data Types in R
Rsquared Academy
 
How to install & update R packages?
How to install & update R packages?
Rsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
Rsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
Rsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
R Programming: Introduction to Matrices
R Programming: Introduction to Matrices
Rsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
R Programming: Variables & Data Types
R Programming: Variables & Data Types
Rsquared Academy
 
Ad

Recently uploaded (20)

最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
Taqyea
 
最新版意大利米兰大学毕业证(UNIMI毕业证书)原版定制
最新版意大利米兰大学毕业证(UNIMI毕业证书)原版定制
taqyea
 
ppt somu_Jarvis_AI_Assistant_presen.pptx
ppt somu_Jarvis_AI_Assistant_presen.pptx
MohammedumarFarhan
 
Flextronics Employee Safety Data-Project-2.pptx
Flextronics Employee Safety Data-Project-2.pptx
kilarihemadri
 
RESEARCH-FINAL-GROUP-3, about the final .pptx
RESEARCH-FINAL-GROUP-3, about the final .pptx
gwapokoha1
 
Starbucks in the Indian market through its joint venture.
Starbucks in the Indian market through its joint venture.
sales480687
 
Prescriptive Process Monitoring Under Uncertainty and Resource Constraints: A...
Prescriptive Process Monitoring Under Uncertainty and Resource Constraints: A...
Mahmoud Shoush
 
Informatics Market Insights AI Workforce.pdf
Informatics Market Insights AI Workforce.pdf
karizaroxx
 
Model Evaluation & Visualisation part of a series of intro modules for data ...
Model Evaluation & Visualisation part of a series of intro modules for data ...
brandonlee626749
 
PPT2 W1L2.pptx.........................................
PPT2 W1L2.pptx.........................................
palicteronalyn26
 
美国毕业证范本中华盛顿大学学位证书CWU学生卡购买
美国毕业证范本中华盛顿大学学位证书CWU学生卡购买
Taqyea
 
Crafting-Research-Recommendations Grade 12.pptx
Crafting-Research-Recommendations Grade 12.pptx
DaryllWhere
 
Artigo - Playing to Win.planejamento docx
Artigo - Playing to Win.planejamento docx
KellyXavier15
 
Indigo_Airlines_Strategy_Presentation.pptx
Indigo_Airlines_Strategy_Presentation.pptx
mukeshpurohit991
 
All the DataOps, all the paradigms .
All the DataOps, all the paradigms .
Lars Albertsson
 
Residential Zone 4 for industrial village
Residential Zone 4 for industrial village
MdYasinArafat13
 
英国毕业证范本利物浦约翰摩尔斯大学成绩单底纹防伪LJMU学生证办理学历认证
英国毕业证范本利物浦约翰摩尔斯大学成绩单底纹防伪LJMU学生证办理学历认证
taqyed
 
@Reset-Password.pptx presentakh;kenvtion
@Reset-Password.pptx presentakh;kenvtion
MarkLariosa1
 
最新版美国威斯康星大学河城分校毕业证(UWRF毕业证书)原版定制
最新版美国威斯康星大学河城分校毕业证(UWRF毕业证书)原版定制
taqyea
 
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
animaroy81
 
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
Taqyea
 
最新版意大利米兰大学毕业证(UNIMI毕业证书)原版定制
最新版意大利米兰大学毕业证(UNIMI毕业证书)原版定制
taqyea
 
ppt somu_Jarvis_AI_Assistant_presen.pptx
ppt somu_Jarvis_AI_Assistant_presen.pptx
MohammedumarFarhan
 
Flextronics Employee Safety Data-Project-2.pptx
Flextronics Employee Safety Data-Project-2.pptx
kilarihemadri
 
RESEARCH-FINAL-GROUP-3, about the final .pptx
RESEARCH-FINAL-GROUP-3, about the final .pptx
gwapokoha1
 
Starbucks in the Indian market through its joint venture.
Starbucks in the Indian market through its joint venture.
sales480687
 
Prescriptive Process Monitoring Under Uncertainty and Resource Constraints: A...
Prescriptive Process Monitoring Under Uncertainty and Resource Constraints: A...
Mahmoud Shoush
 
Informatics Market Insights AI Workforce.pdf
Informatics Market Insights AI Workforce.pdf
karizaroxx
 
Model Evaluation & Visualisation part of a series of intro modules for data ...
Model Evaluation & Visualisation part of a series of intro modules for data ...
brandonlee626749
 
PPT2 W1L2.pptx.........................................
PPT2 W1L2.pptx.........................................
palicteronalyn26
 
美国毕业证范本中华盛顿大学学位证书CWU学生卡购买
美国毕业证范本中华盛顿大学学位证书CWU学生卡购买
Taqyea
 
Crafting-Research-Recommendations Grade 12.pptx
Crafting-Research-Recommendations Grade 12.pptx
DaryllWhere
 
Artigo - Playing to Win.planejamento docx
Artigo - Playing to Win.planejamento docx
KellyXavier15
 
Indigo_Airlines_Strategy_Presentation.pptx
Indigo_Airlines_Strategy_Presentation.pptx
mukeshpurohit991
 
All the DataOps, all the paradigms .
All the DataOps, all the paradigms .
Lars Albertsson
 
Residential Zone 4 for industrial village
Residential Zone 4 for industrial village
MdYasinArafat13
 
英国毕业证范本利物浦约翰摩尔斯大学成绩单底纹防伪LJMU学生证办理学历认证
英国毕业证范本利物浦约翰摩尔斯大学成绩单底纹防伪LJMU学生证办理学历认证
taqyed
 
@Reset-Password.pptx presentakh;kenvtion
@Reset-Password.pptx presentakh;kenvtion
MarkLariosa1
 
最新版美国威斯康星大学河城分校毕业证(UWRF毕业证书)原版定制
最新版美国威斯康星大学河城分校毕业证(UWRF毕业证书)原版定制
taqyea
 
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
animaroy81
 

R Programming: Importing Data In R

  • 1. r-squared Slide 1 www.r-squared.in/rprogramming R Programming Learn the fundamentals of data analysis with R.
  • 2. r-squared Slide 2 Course Modules www.r-squared.in/rprogramming ✓ Introduction ✓ Elementary Programming ✓ Working With Data ✓ Selection Statements ✓ Loops ✓ Functions ✓ Debugging ✓ Unit Testing
  • 3. r-squared Slide 3 Working With Data www.r-squared.in/rprogramming ✓ Data Types ✓ Data Structures ✓ Data Creation ✓ Data Info ✓ Data Subsetting ✓ Comparing R Objects ✓ Importing Data ✓ Exporting Data ✓ Data Transformation ✓ Numeric Functions ✓ String Functions ✓ Mathematical Functions
  • 4. r-squared Slide 4 Importing Data In R www.r-squared.in/rprogramming Objectives In this module, we will learn to: ● Read data from the console ● Read data from files ● Import data from ○ Text/Excel/CSV files ○ Stata/SAS/SPSS files ● Load .Rdata files ● Source R scripts
  • 5. r-squared Slide 5 Read Data From Console www.r-squared.in/rprogramming In this section, we will learn to read data from the console interactively and store them as R objects using the following functions: ✓ scan ✓ readline
  • 6. r-squared Slide 6 scan() (1/4) www.r-squared.in/rprogramming Description: scan() allows user to input data from console or from a file and stores the input in a vector or list. Syntax: x <- scan() # stores input as vector x <- scan("", what = integer()) # stores input as integer x <- scan("", what = list()) # stores input as list Returns: A vector or list of the input data. Documentation help(scan)
  • 7. r-squared Slide 7 scan() (2/4) www.r-squared.in/rprogramming Examples > # example 1 > x <- scan() 1: 1 2: 2 3: 3 4: Read 3 items # to end input, do not enter anything. > x [1] 1 2 3 > typeof(x) [1] "double" # if numbers are entered, they will be stored as double. In the next example, we will learn how to store numbers as integers.
  • 8. r-squared Slide 8 scan() (3/4) www.r-squared.in/rprogramming Examples > # example 2 > x <- scan("", what = integer()) 1: 1 2: 2 3: 3 4: Read 3 items # mention the data type in the what argument to store the data in the preferred mode. > x [1] 1 2 3 > typeof(x) [1] "integer"
  • 9. r-squared Slide 9 scan() (4/4) www.r-squared.in/rprogramming Examples > # example 3 > x <- scan("", what = list(name = "", age = 0)) 1: Jovial 28 2: Manual 27 3: Funnel 25 4: Tunnel 29 5: Read 4 records # suppose we want the user to enter multiple attributes and store the input in a list. Use list in the what argument with the names for the attributes. > x $name [1] "Jovial" "Manual" "Funnel" "Tunnel" $age [1] 28 27 25 29
  • 10. r-squared Slide 10 readline() (1/3) www.r-squared.in/rprogramming Description: readline() prompts the user for an input and stores the input as a character vector. Syntax: readline(prompt = "") Returns: A character vector of the input data. Documentation help(readline)
  • 11. r-squared Slide 11 readline() (2/3) www.r-squared.in/rprogramming Examples > # example 1 > x <- readline(prompt = "Enter your name: ") Enter your name: Jovial > x [1] "Jovial" > class(x) [1] "character" # input is stored as character type. It has to be converted to other data types as necessary. In the next example, we will input a number and then store it as an integer.
  • 12. r-squared Slide 12 readline() (3/3) www.r-squared.in/rprogramming Examples > # example 2 > x <- readline(prompt = "Enter your age: ") Enter your age: 28 > x [1] "28" > class(x) [1] "character" > x <- as.integer(x) > x [1] 28
  • 13. r-squared Slide 13 Read Data From Files www.r-squared.in/rprogramming In this section, we will learn to read data from files using the following functions: ✓ scan ✓ readLines
  • 14. r-squared Slide 14 scan() (1/5) www.r-squared.in/rprogramming Description: scan() allows user to input data from console or from a file and stores the input in a vector or list. Syntax: scan(file = "", what = double(), nmax = -1L, n = -1L, sep = "", quote = if (identical(sep, "n")) "" else "'"", dec = ".", skip = 0L, nlines = 0L, na.strings = "NA", flush = FALSE, fill = FALSE, strip.white = FALSE, quiet = FALSE, blank.lines.skip = TRUE, multi.line = TRUE, comment.char = "", allowEscapes = FALSE, fileEncoding = "", encoding = "unknown", text, skipNul = FALSE) Returns: A vector or list of the input data. Documentation help(scan)
  • 15. r-squared Slide 15 scan() (2/5) www.r-squared.in/rprogramming Arguments: file: name of the file from which the data must be read. what: mode in which data must be stored. nmax: maximum number of data values or lines to be read from a file. n: maximum number of data values to be read. sep: delimiter skip: number of lines to be skipped before reading reading data from a file. nlines: maximum number of lines to be read from a file. quiet: how many items have been read. blank.lines.skip: if blank lines must be skipped. multi.line: whether all lines must appear in one line or multi-line.
  • 16. r-squared Slide 16 scan() (3/5) www.r-squared.in/rprogramming Examples > # example 1 > scan("words.txt", what = character(), skip = 2, nlines = 2, + quiet = TRUE) [1] "Morbi" "consequat" "commodo" "orci" "ut" "volutpat." [7] "Sed" "accumsan" "eleifend" "egestas." "Nullam" "ac" [13] "posuere" "eros." "Donec" "rutrum" "gravida" "felis," [19] "quis" "fermentum" "orci." "Pellentesque" "purus" "lacus," [25] "tincidunt" "eget" "enim" "ut," "facilisis" "rutrum" [31] "odio." # read two lines from the file “words.txt” as type character after skipping the first two lines and do not print the number of lines read on the console.
  • 17. r-squared Slide 17 scan() (4/5) www.r-squared.in/rprogramming Examples > # example 2 > scan("words.txt", what = list("", ""), skip = 2, nlines = 2, sep = " ", + quiet = TRUE) [[1]] [1] "Morbi" "commodo" "ut" "Sed" "eleifend" "Nullam" "posuere" [8] "Donec" "gravida" "quis" "orci." "purus" "tincidunt" "enim" [15] "facilisis" "odio." [[2]] [1] "consequat" "orci" "volutpat." "accumsan" "egestas." "ac" [7] "eros." "rutrum" "felis," "fermentum" "Pellentesque" "lacus," [13] "eget" "ut," "rutrum" "" # read two lines from the file “words.txt” as a list, after skipping the first two lines and do not print the number of lines read on the console.
  • 18. r-squared Slide 18 scan() (5/5) www.r-squared.in/rprogramming Examples > # example 3 > scan("words.txt", what = list("", "", ""), skip = 2, nlines = 3, sep = " ", + quiet = TRUE) [[1]] [1] "Morbi" "orci" "Sed" "egestas." "posuere" "Donec" "felis," [8] "orci." "lacus," "enim" "rutrum" "Donec" "tincidunt" "eu," [15] "tortor." "turpis" "bibendum" [[2]] [1] "consequat" "ut" "accumsan" "Nullam" "eros." "rutrum" [7] "quis" "Pellentesque" "tincidunt" "ut," "odio." "mi" [13] "a" "euismod" "In" "vel" "posuere." [[3]] [1] "commodo" "volutpat." "eleifend" "ac" "" "gravida" [7] "fermentum" "purus" "eget" "facilisis" "" "urna," [13] "sollicitudin" "non" "dignissim" "lorem" "" # read three lines from the file “words.txt” as a list, after skipping the first two lines and do not print the number of lines read on the console.
  • 19. r-squared Slide 19 readLines() (1/3) www.r-squared.in/rprogramming Description: readLines() allows user to input data from console or from a file and stores the input in a vector or list. Syntax: readLines(file_name) Returns: A vector of the input data. Documentation help(readLines)
  • 20. r-squared Slide 20 readLines() (2/3) www.r-squared.in/rprogramming Examples > # example 1 > readLines("words.txt") [1] "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sodales nulla quis interdum dictum. " [2] "Maecenas molestie suscipit libero lobortis ornare. Nam quam magna, tincidunt id vulputate nec, elementum ac lorem. " [3] "Morbi consequat commodo orci ut volutpat. Sed accumsan eleifend egestas. Nullam ac posuere eros. " . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . [15] "Vivamus pulvinar consectetur tellus, quis mollis libero lobortis at. " [16] "Quisque tincidunt purus fermentum augue auctor ultricies." [17] "" # reads all the lines from the file
  • 21. r-squared Slide 21 readLines() (3/3) www.r-squared.in/rprogramming Examples > # example 2 > readLines("words.txt", n = 5) [1] "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sodales nulla quis interdum dictum. " [2] "Maecenas molestie suscipit libero lobortis ornare. Nam quam magna, tincidunt id vulputate nec, elementum ac lorem. " [3] "Morbi consequat commodo orci ut volutpat. Sed accumsan eleifend egestas. Nullam ac posuere eros. " [4] "Donec rutrum gravida felis, quis fermentum orci. Pellentesque purus lacus, tincidunt eget enim ut, facilisis rutrum odio. " [5] "Donec mi urna, tincidunt a sollicitudin eu, euismod non tortor. In dignissim turpis vel lorem bibendum posuere. " # reads the first 5 lines from the file.
  • 22. r-squared Slide 22 Import Data Files www.r-squared.in/rprogramming In this section, we will learn to import the following data files: ✓ Text file ✓ Excel/CSV file ✓ Stata file ✓ SAS file ✓ SPSS file
  • 23. r-squared Slide 23 Importing Text File www.r-squared.in/rprogramming Description: read.table() reads a file in table format and creates a data frame from it. Syntax: read.table(file_name, header, sep) Returns: A data frame. Documentation help(read.table)
  • 24. r-squared Slide 24 read.table() www.r-squared.in/rprogramming Examples > # example 1 > # read data from a semicolon delimited file and retain the column names > text_data <- read.table("data.txt", header = TRUE, sep = ";") > # example 2 > # read data from a comma delimited file and retain the column names > text_data1 <- read.table("data1.txt", header = TRUE, sep = ",") > # example 3 > # read data from a tab delimited file and retain the column names > text_data2 <- read.table("data2.txt", header = TRUE, sep = "t")
  • 25. r-squared Slide 25 read.csv() www.r-squared.in/rprogramming Description: read.csv() reads a CSV file in table format and creates a data frame from it. Syntax: read.csv(file, header = TRUE, sep = ",", quote = """, dec = ".", fill = TRUE, comment.char = "", ...) Returns: A data frame. Documentation help(read.csv)
  • 26. r-squared Slide 26 read.csv() www.r-squared.in/rprogramming Examples > # example 1 > # read data from a CSV file and retain the column names > data_csv <- read.csv("data.csv", header = TRUE) > # example 2 > # read data from a CSV file without the column names > data_csv <- read.csv("data.csv", header = FALSE) > # example 3 > # read data from a CSV file and retain the column names and add blank fields > # when rows are of unequal length > data_csv <- read.csv("data.csv", header = TRUE, fill = TRUE)
  • 27. r-squared Slide 27 read.xls() www.r-squared.in/rprogramming Description: read.xls() reads an excel file in table format and creates a data frame from it. You need to install the gdata package in order to use the read.xls() function. Syntax: read.xls(file, sheet) Returns: A data frame. Documentation: library(gdata) help(read.xls)
  • 28. r-squared Slide 28 read.xls() www.r-squared.in/rprogramming Examples > # example 1 > # read data from a excel file > data_xls <- read.xls("data.csv") > # example 2 > # read data from a particular sheet in a excel file > data_xls <- read.xls("data.csv", sheet = 1)
  • 29. r-squared Slide 29 Stata File www.r-squared.in/rprogramming Description read.dta() reads a Stata binary file into a data frame. Package Install the foreign package to import stata files. Syntax read.csv(file, convert.dates = TRUE, convert.factors = TRUE, missing.type = FALSE, convert.underscore = FALSE, warn.missing.labels = TRUE) Returns A data frame. Documentation help(read.dta)
  • 30. r-squared Slide 30 read.dta() www.r-squared.in/rprogramming Examples > # example 1 > install.packages("foreign") > library(foreign) > data_stata <- read.dta("auto.dta")
  • 31. r-squared Slide 31 SPSS File www.r-squared.in/rprogramming Description read.spss() reads a SPSS file into a data frame. Package Install the foreign package to import stata files. Syntax read.spss(file, use.value.labels = TRUE, to.data.frame = FALSE, max.value.labels = Inf, trim.factor.names = FALSE, trim_values = TRUE, reencode = NA, use.missings = to.data.frame) Returns A data frame. Documentation help(read.spss)
  • 32. r-squared Slide 32 read.spss() www.r-squared.in/rprogramming Examples > # example 1 > install.packages("foreign") > library(foreign) > data_spss <- read.spss("binary.sav")
  • 33. r-squared Slide 33 SAS File www.r-squared.in/rprogramming Description read.sas7bdat() reads SAS files in the sas7bdat data format into a dataframe. Package Install the sas7bdat package to import stata files. Syntax: read.sas7bdat(file, debug=FALSE) Returns: A data frame. Documentation help(read.sas7bdat)
  • 34. r-squared Slide 34 read.sas7bdat() www.r-squared.in/rprogramming Examples > # example 1 > install.packages("sas7bdat") > library(sas7bdat) > data_sas <- read.sas7bdat("crime.sas7bdat")
  • 35. r-squared Slide 35 load() www.r-squared.in/rprogramming Description load() reloads saved datasets and workspaces. Datasets and workspaces have the extension .RData Syntax: load(file) Returns: R object or workspace. Documentation help(load) Example > load("x.RData")
  • 36. r-squared Slide 36 source() www.r-squared.in/rprogramming Description source() reads R codes from a file and makes those codes available in the current session. R scripts have the extension .R Syntax: source(file_name, file_path) Returns Codes from a R file. Documentation help(source) Example > source("functions.R")
  • 37. r-squared Slide 37 Next Steps... www.r-squared.in/rprogramming In the next unit, we will learn to export data from R: ● Output data to the console ● Output data to files ● Export data into text/CSV files ● Save R objects
  • 38. r-squared Slide 38 Connect With Us www.r-squared.in/rprogramming Visit r-squared for tutorials on: ● R Programming ● Business Analytics ● Data Visualization ● Web Applications ● Package Development ● Git & GitHub