SlideShare una empresa de Scribd logo
1 de 38
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

Más contenido relacionado

La actualidad más candente

Data visualization using R
Data visualization using RData visualization using R
Data visualization using RUmmiya Mohammedi
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to MatricesRsquared Academy
 
Import Data using R
Import Data using R Import Data using R
Import Data using R Rupak Roy
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to VectorsRsquared Academy
 
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With REdureka!
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 
R programming slides
R  programming slidesR  programming slides
R programming slidesPankaj Saini
 
Python Pandas
Python PandasPython Pandas
Python PandasSunil OS
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data framekrishna singh
 
Introduction to R for data science
Introduction to R for data scienceIntroduction to R for data science
Introduction to R for data scienceLong Nguyen
 
Exploratory data analysis with Python
Exploratory data analysis with PythonExploratory data analysis with Python
Exploratory data analysis with PythonDavis David
 

La actualidad más candente (20)

Data visualization using R
Data visualization using RData visualization using R
Data visualization using R
 
R decision tree
R   decision treeR   decision tree
R decision tree
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
Import Data using R
Import Data using R Import Data using R
Import Data using R
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
 
Data Visualization With R
Data Visualization With RData Visualization With R
Data Visualization With R
 
Step By Step Guide to Learn R
Step By Step Guide to Learn RStep By Step Guide to Learn R
Step By Step Guide to Learn R
 
Data Mining with R programming
Data Mining with R programmingData Mining with R programming
Data Mining with R programming
 
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With R
 
6. R data structures
6. R data structures6. R data structures
6. R data structures
 
Vector in R
Vector in RVector in R
Vector in R
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
R programming slides
R  programming slidesR  programming slides
R programming slides
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data frame
 
R Basics
R BasicsR Basics
R Basics
 
Introduction to R for data science
Introduction to R for data scienceIntroduction to R for data science
Introduction to R for data science
 
Exploratory data analysis with Python
Exploratory data analysis with PythonExploratory data analysis with Python
Exploratory data analysis with Python
 
R studio
R studio R studio
R studio
 

Similar a R Programming: Importing Data In R

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 RRsquared Academy
 
RDataMining slides-r-programming
RDataMining slides-r-programmingRDataMining slides-r-programming
RDataMining slides-r-programmingYanchang Zhao
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packagesAjay Ohri
 
R Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB AcademyR Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB Academyrajkamaltibacademy
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptanshikagoel52
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_iNico 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...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 ...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...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.pdfLecture1_R.pdf
Lecture1_R.pdfBusyBird2
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using PythonNishantKumar1179
 
Text Mining with R -- an Analysis of Twitter Data
Text Mining with R -- an Analysis of Twitter DataText Mining with R -- an Analysis of Twitter Data
Text Mining with R -- an Analysis of Twitter DataYanchang Zhao
 
Introduction to Apache Flink
Introduction to Apache FlinkIntroduction to Apache Flink
Introduction to Apache Flinkmxmxm
 

Similar a 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
 
RDataMining slides-r-programming
RDataMining slides-r-programmingRDataMining slides-r-programming
RDataMining slides-r-programming
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packages
 
R basics
R basicsR basics
R basics
 
R Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB AcademyR Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB Academy
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1 r
Lecture1 rLecture1 r
Lecture1 r
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
 
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop  - Xi...PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop  - Xi...
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
 
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...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...
 
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 ...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 ...
 
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...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...
 
Lecture1_R.pdf
Lecture1_R.pdfLecture1_R.pdf
Lecture1_R.pdf
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
Text Mining with R -- an Analysis of Twitter Data
Text Mining with R -- an Analysis of Twitter DataText Mining with R -- an Analysis of Twitter Data
Text Mining with R -- an Analysis of Twitter Data
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Introduction to Apache Flink
Introduction to Apache FlinkIntroduction to Apache Flink
Introduction to Apache Flink
 

Más de Rsquared Academy

Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in RRsquared 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 RRsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with PipesRsquared 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 RRsquared 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 RRsquared Academy
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in RRsquared 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 BeginnersRsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersRsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsRsquared Academy
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data TypesRsquared Academy
 
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsData Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsRsquared Academy
 
R Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsR Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsRsquared Academy
 

Más de Rsquared Academy (20)

Handling Date & Time in R
Handling Date & Time in RHandling Date & Time in R
Handling Date & Time in R
 
Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in R
 
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
 
Joining Data with dplyr
Joining Data with dplyrJoining Data with dplyr
Joining Data with dplyr
 
Explore Data using dplyr
Explore Data using dplyrExplore Data using dplyr
Explore Data using dplyr
 
Data Wrangling with dplyr
Data Wrangling with dplyrData Wrangling with dplyr
Data Wrangling with dplyr
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with Pipes
 
Introduction to tibbles
Introduction to tibblesIntroduction to tibbles
Introduction to tibbles
 
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
 
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
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in R
 
How to install & update R packages?
How to install & update R packages?How to install & update R packages?
How to install & update R packages?
 
How to get help in R?
How to get help in R?How to get help in R?
How to get help in R?
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data Types
 
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsData Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple Graphs
 
R Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsR Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To Plots
 

Último

Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 217djon017
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPTBoston Institute of Analytics
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Seán Kennedy
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理e4aez8ss
 
LLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGILLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGIThomas Poetter
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...GQ Research
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Colleen Farrelly
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Seán Kennedy
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfchwongval
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...limedy534
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfBoston Institute of Analytics
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Cathrine Wilhelmsen
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
Learn How Data Science Changes Our World
Learn How Data Science Changes Our WorldLearn How Data Science Changes Our World
Learn How Data Science Changes Our WorldEduminds Learning
 

Último (20)

Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
 
LLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGILLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGI
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdf
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
Learn How Data Science Changes Our World
Learn How Data Science Changes Our WorldLearn How Data Science Changes Our World
Learn How Data Science Changes Our World
 

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