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

More Related Content

What's hot (20)

R Programming: Introduction To R Packages
R Programming: Introduction To R PackagesR Programming: Introduction To R Packages
R Programming: Introduction To R Packages
Rsquared Academy
 
Python basic
Python basicPython basic
Python basic
Saifuddin Kaijar
 
3 Data Structure in R
3 Data Structure in R3 Data Structure in R
3 Data Structure in R
Dr Nisha Arora
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
Girish Khanzode
 
Getting Started with R
Getting Started with RGetting Started with R
Getting Started with R
Sankhya_Analytics
 
R programming slides
R  programming slidesR  programming slides
R programming slides
Pankaj Saini
 
An Introduction to Data Mining with R
An Introduction to Data Mining with RAn Introduction to Data Mining with R
An Introduction to Data Mining with R
Yanchang Zhao
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
NareshKarela1
 
Exploratory data analysis in R - Data Science Club
Exploratory data analysis in R - Data Science ClubExploratory data analysis in R - Data Science Club
Exploratory data analysis in R - Data Science Club
Martin Bago
 
Data Analysis in Python
Data Analysis in PythonData Analysis in Python
Data Analysis in Python
Richard Herrell
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
izahn
 
Data visualization with R
Data visualization with RData visualization with R
Data visualization with R
Biswajeet Dasmajumdar
 
R programming
R programmingR programming
R programming
Shantanu Patil
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
Piyush rai
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
Gaurav Aggarwal
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
ShareThis
 
R data types
R   data typesR   data types
R data types
Learnbay Datascience
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
Kazuki Yoshida
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
krishna singh
 
R Programming: Introduction To R Packages
R Programming: Introduction To R PackagesR Programming: Introduction To R Packages
R Programming: Introduction To R Packages
Rsquared Academy
 
R programming slides
R  programming slidesR  programming slides
R programming slides
Pankaj Saini
 
An Introduction to Data Mining with R
An Introduction to Data Mining with RAn Introduction to Data Mining with R
An Introduction to Data Mining with R
Yanchang Zhao
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
NareshKarela1
 
Exploratory data analysis in R - Data Science Club
Exploratory data analysis in R - Data Science ClubExploratory data analysis in R - Data Science Club
Exploratory data analysis in R - Data Science Club
Martin Bago
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
izahn
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
Piyush rai
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
ShareThis
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
krishna singh
 

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

R Programming: Export/Output Data In R
R Programming: Export/Output Data In RR Programming: Export/Output Data In R
R Programming: Export/Output Data In R
Rsquared Academy
 
Unit-5 BDS.pptx on basics of data science
Unit-5 BDS.pptx on basics of data scienceUnit-5 BDS.pptx on basics of data science
Unit-5 BDS.pptx on basics of data science
SyedFahad39584
 
Unit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptxUnit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptx
SreeLaya9
 
Data analystics with R module 3 cseds vtu
Data analystics with R module 3 cseds vtuData analystics with R module 3 cseds vtu
Data analystics with R module 3 cseds vtu
LalithauLali
 
R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011
Mandi Walls
 
Unit 3
Unit 3Unit 3
Unit 3
Piyush Rochwani
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
Guy Lebanon
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
Nimrita Koul
 
Introduction To Programming In R for data analyst
Introduction To Programming In R for data analystIntroduction To Programming In R for data analyst
Introduction To Programming In R for data analyst
ssuser26ff68
 
Introduction to R for Learning Analytics Researchers
Introduction to R for Learning Analytics ResearchersIntroduction to R for Learning Analytics Researchers
Introduction to R for Learning Analytics Researchers
Vitomir Kovanovic
 
A short tutorial on r
A short tutorial on rA short tutorial on r
A short tutorial on r
Ashraf Uddin
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
Malla Reddy University
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
Chia-Chi Chang
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
062MayankSinghal
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
vpletap
 
Basics of R-Programming with example.ppt
Basics of R-Programming with example.pptBasics of R-Programming with example.ppt
Basics of R-Programming with example.ppt
geethar79
 
Basocs of statistics with R-Programming.ppt
Basocs of statistics with R-Programming.pptBasocs of statistics with R-Programming.ppt
Basocs of statistics with R-Programming.ppt
geethar79
 
R-Programming.ppt it is based on R programming language
R-Programming.ppt it is based on R programming languageR-Programming.ppt it is based on R programming language
R-Programming.ppt it is based on R programming language
Zoha681526
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
ranapoonam1
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
KabilaArun
 
R Programming: Export/Output Data In R
R Programming: Export/Output Data In RR Programming: Export/Output Data In R
R Programming: Export/Output Data In R
Rsquared Academy
 
Unit-5 BDS.pptx on basics of data science
Unit-5 BDS.pptx on basics of data scienceUnit-5 BDS.pptx on basics of data science
Unit-5 BDS.pptx on basics of data science
SyedFahad39584
 
Unit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptxUnit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptx
SreeLaya9
 
Data analystics with R module 3 cseds vtu
Data analystics with R module 3 cseds vtuData analystics with R module 3 cseds vtu
Data analystics with R module 3 cseds vtu
LalithauLali
 
R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011
Mandi Walls
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
Guy Lebanon
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
Nimrita Koul
 
Introduction To Programming In R for data analyst
Introduction To Programming In R for data analystIntroduction To Programming In R for data analyst
Introduction To Programming In R for data analyst
ssuser26ff68
 
Introduction to R for Learning Analytics Researchers
Introduction to R for Learning Analytics ResearchersIntroduction to R for Learning Analytics Researchers
Introduction to R for Learning Analytics Researchers
Vitomir Kovanovic
 
A short tutorial on r
A short tutorial on rA short tutorial on r
A short tutorial on r
Ashraf Uddin
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
Chia-Chi Chang
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
vpletap
 
Basics of R-Programming with example.ppt
Basics of R-Programming with example.pptBasics of R-Programming with example.ppt
Basics of R-Programming with example.ppt
geethar79
 
Basocs of statistics with R-Programming.ppt
Basocs of statistics with R-Programming.pptBasocs of statistics with R-Programming.ppt
Basocs of statistics with R-Programming.ppt
geethar79
 
R-Programming.ppt it is based on R programming language
R-Programming.ppt it is based on R programming languageR-Programming.ppt it is based on R programming language
R-Programming.ppt it is based on R programming language
Zoha681526
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
ranapoonam1
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
KabilaArun
 
Ad

More from Rsquared Academy (20)

Handling Date & Time in R
Handling Date & Time in RHandling Date & Time in R
Handling Date & Time in R
Rsquared Academy
 
Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in R
Rsquared Academy
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using RPractical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
Rsquared Academy
 
Joining Data with dplyr
Joining Data with dplyrJoining Data with dplyr
Joining Data with dplyr
Rsquared Academy
 
Explore Data using dplyr
Explore Data using dplyrExplore Data using dplyr
Explore Data using dplyr
Rsquared Academy
 
Data Wrangling with dplyr
Data Wrangling with dplyrData Wrangling with dplyr
Data Wrangling with dplyr
Rsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with Pipes
Rsquared Academy
 
Introduction to tibbles
Introduction to tibblesIntroduction to tibbles
Introduction to tibbles
Rsquared Academy
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRead 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 RRead/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 RVariables & Data Types in R
Variables & Data Types in R
Rsquared Academy
 
How to install & update R packages?
How to install & update R packages?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?How to get help in R?
How to get help in R?
Rsquared Academy
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
Rsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
Rsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
Rsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
Rsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data Types
Rsquared Academy
 
Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in R
Rsquared Academy
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using RPractical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
Rsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with Pipes
Rsquared Academy
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRead 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 RRead/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 RVariables & Data Types in R
Variables & Data Types in R
Rsquared Academy
 
How to install & update R packages?
How to install & update R packages?How to install & update R packages?
How to install & update R packages?
Rsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
Rsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
Rsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
Rsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data Types
Rsquared Academy
 
Ad

Recently uploaded (20)

531a07261283c4efb4cbae5fb8. Tele2 Sverige AB v post-och telestyrelsen, C-203:...
531a07261283c4efb4cbae5fb8. Tele2 Sverige AB v post-och telestyrelsen, C-203:...531a07261283c4efb4cbae5fb8. Tele2 Sverige AB v post-och telestyrelsen, C-203:...
531a07261283c4efb4cbae5fb8. Tele2 Sverige AB v post-och telestyrelsen, C-203:...
spratistha569
 
How to Choose the Right Online Proofing Software
How to Choose the Right Online Proofing SoftwareHow to Choose the Right Online Proofing Software
How to Choose the Right Online Proofing Software
skalatskayaek
 
llm lecture 4 stanford blah blah blah blah
llm lecture 4 stanford blah blah blah blahllm lecture 4 stanford blah blah blah blah
llm lecture 4 stanford blah blah blah blah
saud140081
 
time_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptxtime_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptx
stefanopinto1113
 
Tableau Finland User Group June 2025.pdf
Tableau Finland User Group June 2025.pdfTableau Finland User Group June 2025.pdf
Tableau Finland User Group June 2025.pdf
elinavihriala
 
Data Analytics and visualization-PowerBi
Data Analytics and visualization-PowerBiData Analytics and visualization-PowerBi
Data Analytics and visualization-PowerBi
Krishnapriya975316
 
一比一原版(USC毕业证)南加利福尼亚大学毕业证如何办理
一比一原版(USC毕业证)南加利福尼亚大学毕业证如何办理一比一原版(USC毕业证)南加利福尼亚大学毕业证如何办理
一比一原版(USC毕业证)南加利福尼亚大学毕业证如何办理
Taqyea
 
Human body make Structure analysis the part of the human
Human body make Structure analysis the part of the humanHuman body make Structure analysis the part of the human
Human body make Structure analysis the part of the human
ankit392215
 
EPC UNIT-V forengineeringstudentsin.pptx
EPC UNIT-V forengineeringstudentsin.pptxEPC UNIT-V forengineeringstudentsin.pptx
EPC UNIT-V forengineeringstudentsin.pptx
ExtremerZ
 
Math arihant handbook.pdf all formula is here
Math arihant handbook.pdf all formula is hereMath arihant handbook.pdf all formula is here
Math arihant handbook.pdf all formula is here
rdarshankumar84
 
Content Moderation Services_ Leading the Future of Online Safety.docx
Content Moderation Services_ Leading the Future of Online Safety.docxContent Moderation Services_ Leading the Future of Online Safety.docx
Content Moderation Services_ Leading the Future of Online Safety.docx
sofiawilliams5966
 
Artificial-Intelligence-in-Autonomous-Vehicles (1)-1.pptx
Artificial-Intelligence-in-Autonomous-Vehicles (1)-1.pptxArtificial-Intelligence-in-Autonomous-Vehicles (1)-1.pptx
Artificial-Intelligence-in-Autonomous-Vehicles (1)-1.pptx
AbhijitPal87
 
9.-Composite-Dr.-B.-Nalini.pptxfdrtyuioklj
9.-Composite-Dr.-B.-Nalini.pptxfdrtyuioklj9.-Composite-Dr.-B.-Nalini.pptxfdrtyuioklj
9.-Composite-Dr.-B.-Nalini.pptxfdrtyuioklj
aishwaryavdcw
 
Али махмуд to The teacm of ghsbh to fortune .pptx
Али махмуд to The teacm of ghsbh to fortune .pptxАли махмуд to The teacm of ghsbh to fortune .pptx
Али махмуд to The teacm of ghsbh to fortune .pptx
palr19411
 
15 Benefits of Data Analytics in Business Growth.pdf
15 Benefits of Data Analytics in Business Growth.pdf15 Benefits of Data Analytics in Business Growth.pdf
15 Benefits of Data Analytics in Business Growth.pdf
AffinityCore
 
PSUG 7 - 2025-06-03 - David Bianco on Splunk SURGe
PSUG 7 - 2025-06-03 - David Bianco on Splunk SURGePSUG 7 - 2025-06-03 - David Bianco on Splunk SURGe
PSUG 7 - 2025-06-03 - David Bianco on Splunk SURGe
Tomas Moser
 
Comprehensive Roadmap of AI, ML, DS, DA & DSA.pdf
Comprehensive Roadmap of AI, ML, DS, DA & DSA.pdfComprehensive Roadmap of AI, ML, DS, DA & DSA.pdf
Comprehensive Roadmap of AI, ML, DS, DA & DSA.pdf
epsilonice
 
Blue Dark Professional Geometric Business Project Presentation .pdf
Blue Dark Professional Geometric Business Project Presentation .pdfBlue Dark Professional Geometric Business Project Presentation .pdf
Blue Dark Professional Geometric Business Project Presentation .pdf
mohammadhaidarayoobi
 
Chronic constipation presentaion final.ppt
Chronic constipation presentaion final.pptChronic constipation presentaion final.ppt
Chronic constipation presentaion final.ppt
DrShashank7
 
"Machine Learning in Agriculture: 12 Production-Grade Models", Danil Polyakov
"Machine Learning in Agriculture: 12 Production-Grade Models", Danil Polyakov"Machine Learning in Agriculture: 12 Production-Grade Models", Danil Polyakov
"Machine Learning in Agriculture: 12 Production-Grade Models", Danil Polyakov
Fwdays
 
531a07261283c4efb4cbae5fb8. Tele2 Sverige AB v post-och telestyrelsen, C-203:...
531a07261283c4efb4cbae5fb8. Tele2 Sverige AB v post-och telestyrelsen, C-203:...531a07261283c4efb4cbae5fb8. Tele2 Sverige AB v post-och telestyrelsen, C-203:...
531a07261283c4efb4cbae5fb8. Tele2 Sverige AB v post-och telestyrelsen, C-203:...
spratistha569
 
How to Choose the Right Online Proofing Software
How to Choose the Right Online Proofing SoftwareHow to Choose the Right Online Proofing Software
How to Choose the Right Online Proofing Software
skalatskayaek
 
llm lecture 4 stanford blah blah blah blah
llm lecture 4 stanford blah blah blah blahllm lecture 4 stanford blah blah blah blah
llm lecture 4 stanford blah blah blah blah
saud140081
 
time_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptxtime_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptx
stefanopinto1113
 
Tableau Finland User Group June 2025.pdf
Tableau Finland User Group June 2025.pdfTableau Finland User Group June 2025.pdf
Tableau Finland User Group June 2025.pdf
elinavihriala
 
Data Analytics and visualization-PowerBi
Data Analytics and visualization-PowerBiData Analytics and visualization-PowerBi
Data Analytics and visualization-PowerBi
Krishnapriya975316
 
一比一原版(USC毕业证)南加利福尼亚大学毕业证如何办理
一比一原版(USC毕业证)南加利福尼亚大学毕业证如何办理一比一原版(USC毕业证)南加利福尼亚大学毕业证如何办理
一比一原版(USC毕业证)南加利福尼亚大学毕业证如何办理
Taqyea
 
Human body make Structure analysis the part of the human
Human body make Structure analysis the part of the humanHuman body make Structure analysis the part of the human
Human body make Structure analysis the part of the human
ankit392215
 
EPC UNIT-V forengineeringstudentsin.pptx
EPC UNIT-V forengineeringstudentsin.pptxEPC UNIT-V forengineeringstudentsin.pptx
EPC UNIT-V forengineeringstudentsin.pptx
ExtremerZ
 
Math arihant handbook.pdf all formula is here
Math arihant handbook.pdf all formula is hereMath arihant handbook.pdf all formula is here
Math arihant handbook.pdf all formula is here
rdarshankumar84
 
Content Moderation Services_ Leading the Future of Online Safety.docx
Content Moderation Services_ Leading the Future of Online Safety.docxContent Moderation Services_ Leading the Future of Online Safety.docx
Content Moderation Services_ Leading the Future of Online Safety.docx
sofiawilliams5966
 
Artificial-Intelligence-in-Autonomous-Vehicles (1)-1.pptx
Artificial-Intelligence-in-Autonomous-Vehicles (1)-1.pptxArtificial-Intelligence-in-Autonomous-Vehicles (1)-1.pptx
Artificial-Intelligence-in-Autonomous-Vehicles (1)-1.pptx
AbhijitPal87
 
9.-Composite-Dr.-B.-Nalini.pptxfdrtyuioklj
9.-Composite-Dr.-B.-Nalini.pptxfdrtyuioklj9.-Composite-Dr.-B.-Nalini.pptxfdrtyuioklj
9.-Composite-Dr.-B.-Nalini.pptxfdrtyuioklj
aishwaryavdcw
 
Али махмуд to The teacm of ghsbh to fortune .pptx
Али махмуд to The teacm of ghsbh to fortune .pptxАли махмуд to The teacm of ghsbh to fortune .pptx
Али махмуд to The teacm of ghsbh to fortune .pptx
palr19411
 
15 Benefits of Data Analytics in Business Growth.pdf
15 Benefits of Data Analytics in Business Growth.pdf15 Benefits of Data Analytics in Business Growth.pdf
15 Benefits of Data Analytics in Business Growth.pdf
AffinityCore
 
PSUG 7 - 2025-06-03 - David Bianco on Splunk SURGe
PSUG 7 - 2025-06-03 - David Bianco on Splunk SURGePSUG 7 - 2025-06-03 - David Bianco on Splunk SURGe
PSUG 7 - 2025-06-03 - David Bianco on Splunk SURGe
Tomas Moser
 
Comprehensive Roadmap of AI, ML, DS, DA & DSA.pdf
Comprehensive Roadmap of AI, ML, DS, DA & DSA.pdfComprehensive Roadmap of AI, ML, DS, DA & DSA.pdf
Comprehensive Roadmap of AI, ML, DS, DA & DSA.pdf
epsilonice
 
Blue Dark Professional Geometric Business Project Presentation .pdf
Blue Dark Professional Geometric Business Project Presentation .pdfBlue Dark Professional Geometric Business Project Presentation .pdf
Blue Dark Professional Geometric Business Project Presentation .pdf
mohammadhaidarayoobi
 
Chronic constipation presentaion final.ppt
Chronic constipation presentaion final.pptChronic constipation presentaion final.ppt
Chronic constipation presentaion final.ppt
DrShashank7
 
"Machine Learning in Agriculture: 12 Production-Grade Models", Danil Polyakov
"Machine Learning in Agriculture: 12 Production-Grade Models", Danil Polyakov"Machine Learning in Agriculture: 12 Production-Grade Models", Danil Polyakov
"Machine Learning in Agriculture: 12 Production-Grade Models", Danil Polyakov
Fwdays
 

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