SlideShare une entreprise Scribd logo
1  sur  39
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
Exporting Data From R
www.r-squared.in/rprogramming
Objectives
In this module, we will learn to:
● Output data to the console
● Output data to files
● Export data into text/CSV files
● Save R objects
r-squared
Slide 5
Output Data To Console
www.r-squared.in/rprogramming
In this section, we will learn to output data to the console using the following functions:
✓ print
✓ cat
✓ paste
✓ paste0
✓ sprintf
r-squared
Slide 6
print()
www.r-squared.in/rprogramming
Description
print() is a very basic function and as the name suggest it prints its arguments.
Syntax
print(x, ...)
Returns
Prints its arguments
Documentation
help(print)
r-squared
Slide 7
print()
www.r-squared.in/rprogramming
Examples
> # example 1
> x <- 3
> print(x)
[1] 3
> x
[1] 3
> # example 2
> name <- "Jovial"
> lname <- "Mann"
> print(name, lname)
Error in print.default(name, lname) : invalid 'digits' argument
> print(c(name, lname))
[1] "Jovial" "Mann"
r-squared
Slide 8
cat()
www.r-squared.in/rprogramming
Description
cat() concatenates its arguments and then outputs them.
Syntax
cat(... , file = "", sep = " ", fill = FALSE, labels = NULL,
append = FALSE)
Returns
Concatenates and outputs its arguments
Documentation
help(cat)
r-squared
Slide 9
cat()
www.r-squared.in/rprogramming
Examples
> # example 1
# output to the console
> name <- "Jovial"
> age <- 28
> cat("Your name is", name, " and you are", age, " years old.")
Your name is Jovial and you are 28 years old.
> # example 2
# output to a file
> name <- "Jovial"
> age <- 28
> cat("Your name is", name, " and you are", age, " years old.", file = "words.txt")
r-squared
Slide 10
cat()
www.r-squared.in/rprogramming
Examples
> # example 3
# difference between print and cat
> fname <- "Jovial"
> lname <- "Mann"
> print(c(fname, lname)) # throws an error
[1] "Jovial" "Mann"
> name <- c(fname, lname)
> name
[1] "Jovial" "Mann"
> name <- "Jovial Mann"
> print(name)
[1] "Jovial Mann"
> cat(fname, lname)
Jovial Mann
r-squared
Slide 11
paste()
www.r-squared.in/rprogramming
Description
paste() converts its arguments to character strings and concatenates them.
Syntax
paste (..., sep = " ", collapse = NULL)
Returns
Character vector
Documentation
help(paste)
r-squared
Slide 12
paste()
www.r-squared.in/rprogramming
Examples
> # example 1
> x <- 1:10
> paste(x)
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
> paste(x, collapse = "")
[1] "12345678910"
> # example 2
> x <- LETTERS
> x
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V"
[23] "W" "X" "Y" "Z"
> paste(x)
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V"
[23] "W" "X" "Y" "Z"
> paste(x, collapse = "")
[1] "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
r-squared
Slide 13
paste()
www.r-squared.in/rprogramming
Examples
> # example 3
> name <- "Jovial"
> age <- 28
> paste(name, age)
[1] "Jovial 28"
> paste(name, age, sep = ",")
[1] "Jovial,28"
> paste(name, age, sep = "")
[1] "Jovial28"
r-squared
Slide 14
paste()
www.r-squared.in/rprogramming
Examples
> # example 4
> alpha <- LETTERS[1:4]
> numeric <- 1:4
> alpha_numeric <- paste(alpha, numeric)
> alpha_numeric
[1] "A 1" "B 2" "C 3" "D 4"
> alpha_numeric <- paste(alpha, numeric, sep = "")
> alpha_numeric
[1] "A1" "B2" "C3" "D4"
r-squared
Slide 15
paste0()
www.r-squared.in/rprogramming
Description
paste0() does the same job as paste but with a blank separator.
Syntax
paste0(..., collapse = NULL)
Returns
Character vector of concatenated values.
Documentation
help(paste0)
r-squared
Slide 16
paste0()
www.r-squared.in/rprogramming
Examples
> # example 1
> alpha <- LETTERS[1:4]
> numeric <- 1:4
> paste0(alpha, numeric)
[1] "A1" "B2" "C3" "D4"
> paste(alpha, numeric, sep = "")
[1] "A1" "B2" "C3" "D4"
> # example 2
> name <- "Jovial"
> age <- 28
> paste0(name, age)
[1] "Jovial28"
> paste(name, age, sep = "")
[1] "Jovial28"
r-squared
Slide 17
sprintf()
www.r-squared.in/rprogramming
Description
sprintf() returns a character vector containing text and objects.
Syntax
sprintf(fmt, ...)
Returns
Character vector
Documentation
help(sprintf)
r-squared
Slide 18
sprintf()
www.r-squared.in/rprogramming
Examples
> # example 1
> name <- "Jovial"
> age <- 28
> sprintf("Your name is %s and you are %d years old", name, age)
[1] "Your name is Jovial and you are 28 years old"
# %s is replaced by the value in name and %d is replaced by value in age. s indicates string
type and d indicates integer type.
r-squared
Slide 19
Output Data To File
www.r-squared.in/rprogramming
In this section, we will learn to output data to a file using the following functions:
✓ writeLines
✓ write
✓ write.table
✓ write.csv
✓ sink
✓ dump
r-squared
Slide 20
writeLines()
www.r-squared.in/rprogramming
Description
writeLines() writes text lines to a connection or a file.
Syntax
writeLines(text, con = stdout(), sep = "n", useBytes = FALSE)
Returns
File with overwritten text.
Documentation
help(writeLines)
r-squared
Slide 21
writeLines()
www.r-squared.in/rprogramming
Examples
> # example 1
> details <- "My name is Jovial."
> writeLines(details, "write.txt")
> readLines("write.txt")
[1] "My name is Jovial."
> # example 2
> details_2 <- "I am 28 years old."
> writeLines(details_2, "write.txt")
> readLines("write.txt")
[1] "I am 28 years old."
# As you might have observed, writeLines overwrites the contents of a file. To append text to
a file, we will use the write function.
r-squared
Slide 22
write()
www.r-squared.in/rprogramming
Description
write() writes/appends data to a file.
Syntax
write(x, file = "data", ncolumns = if(is.character(x)) 1 else 5,
append = FALSE, sep = " ")
Returns
File with appended text.
Documentation
help(write)
r-squared
Slide 23
write()
www.r-squared.in/rprogramming
Examples
> # example 1
> name <- "My name is Jovial."
> write(name, file = "write.txt")
> readLines("write.txt")
[1] "My name is Jovial."
> age <- "I am 28 years old."
> write(age, file = "write.txt", append = TRUE)
> readLines("write.txt")
[1] "My name is Jovial." "I am 28 years old."
r-squared
Slide 24
write()
www.r-squared.in/rprogramming
Examples
> # example 2
> x <- 1:5
> x
[1] 1 2 3 4 5
> write(x, file = "write.txt", append = TRUE)
> readLines("write.txt")
[1] "My name is Jovial." "I am 28 years old." "1 2 3 4 5"
> write(x, file = "write.txt", append = TRUE)
> readLines("write.txt")
[1] "My name is Jovial." "I am 28 years old." "1 2 3 4 5" "1 2 3 4 5"
> write(x, file = "write.txt", append = TRUE, sep = "-")
> readLines("write.txt")
[1] "My name is Jovial." "I am 28 years old." "1 2 3 4 5" "1 2 3 4 5"
[5] "1-2-3-4-5"
r-squared
Slide 25
write.table()
www.r-squared.in/rprogramming
Description
write.table() will convert the data into data.frame or matrix before writing it to a file.
Syntax
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ", eol =
"n", na = "NA", dec = ".", row.names = TRUE, col.names = TRUE, qmethod =
c("escape", "double"), fileEncoding = "")
Returns
File with data as a data frame or matrix.
Documentation
help(write.table)
r-squared
Slide 26
write.table()
www.r-squared.in/rprogramming
Examples
> # example 1
> m <- matrix(1:9, nrow = 3)
> m
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> # write to a text file
> write.table(m, file = "table.txt")
> readLines("table.txt")
[1] ""V1" "V2" "V3"" ""1" 1 4 7" ""2" 2 5 8"
[4] ""3" 3 6 9"
r-squared
Slide 27
write.table()
www.r-squared.in/rprogramming
Examples
> # example 2
> # write to a csv file
> write.table(m, file = "table.csv")
> readLines("table.csv")
[1] ""V1" "V2" "V3"" ""1" 1 4 7" ""2" 2 5 8"
[4] ""3" 3 6 9"
> # example 3
> # append the transpose of the matrix
> write.table(t(m), file = "table.txt", append = TRUE)
> readLines("table.txt")
[1] ""V1" "V2" "V3"" ""1" 1 4 7" ""2" 2 5 8"
[4] ""3" 3 6 9" ""V1" "V2" "V3"" ""1" 1 2 3"
[7] ""2" 4 5 6" ""3" 7 8 9"
r-squared
Slide 28
write.table()
www.r-squared.in/rprogramming
> # example 4
> # use comma as a separator
> write.table(t(m), file = "table.txt", append = TRUE, sep = ",")
> readLines("table.txt")
[1] ""V1" "V2" "V3"" ""1" 1 4 7" ""2" 2 5 8"
[4] ""3" 3 6 9" ""V1" "V2" "V3"" ""1" 1 2 3"
[7] ""2" 4 5 6" ""3" 7 8 9" ""V1","V2","V3""
[10] ""1",1,2,3" ""2",4,5,6" ""3",7,8,9"
> # example 5
> # without row and column names
> write.table(t(m), file = "table.txt", append = TRUE, row.names = FALSE,
+ col.names = FALSE)
> readLines("table.txt")
[1] ""V1" "V2" "V3"" ""1" 1 4 7" ""2" 2 5 8"
[4] ""3" 3 6 9" ""V1" "V2" "V3"" ""1" 1 2 3"
[7] ""2" 4 5 6" ""3" 7 8 9" ""V1","V2","V3""
[10] ""1",1,2,3" ""2",4,5,6" ""3",7,8,9"
[13] "1 2 3" "4 5 6" "7 8 9"
r-squared
Slide 29
write.csv()
www.r-squared.in/rprogramming
Description
write.csv() will convert the data into data.frame or matrix before writing it to a CSV file.
Syntax
write.csv(data, file, row.names= FALSE)
Returns
CSV file
Documentation
help(write.csv)
r-squared
Slide 30
write.csv()
www.r-squared.in/rprogramming
Examples
> # example 1
> write.csv(mtcars, "mt.csv")
> readLines("mt.csv")
[1]""","mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear","carb""
[2] ""Mazda RX4",21,6,160,110,3.9,2.62,16.46,0,1,4,4"
[3] ""Mazda RX4 Wag",21,6,160,110,3.9,2.875,17.02,0,1,4,4"
[4] ""Datsun 710",22.8,4,108,93,3.85,2.32,18.61,1,1,4,1"
> # example 2
> # without row names
> write.csv(mtcars, "mt.csv", row.names = FALSE)
> readLines("mt.csv")
[1] ""mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear","carb""
[2] "21,6,160,110,3.9,2.62,16.46,0,1,4,4"
[3] "21,6,160,110,3.9,2.875,17.02,0,1,4,4"
r-squared
Slide 31
sink()
www.r-squared.in/rprogramming
Description
sink() will divert the R output to a file/connection instead of the console.
Syntax
sink(file = NULL, append = FALSE, type = c("output", "message"),
split = FALSE)
Returns
File with output
Documentation
help(sink)
r-squared
Slide 32
sink()
www.r-squared.in/rprogramming
Examples
> # example 1
> sink(file = "write.txt", append = TRUE, type = "output")
> x <- 1:5
> x * 2
> readLines("write.txt") # nothing is printed on console
> sink() # stop diverting output to file
> readLines("write.txt")
[1] "[1] 2 4 6 8 10"
[2] "[1] "[1] 2 4 6 8 10""
[3] "[1] 2 4 6 8 10"
[4] "[1] "[1] 2 4 6 8 10" "[1] "[1] 2 4 6 8 10"""
[5] "[3] "[1] 2 4 6 8 10" "
r-squared
Slide 33
sink()
www.r-squared.in/rprogramming
Examples
> # example 2
> sink("example.txt") # start writing to example.txt file
> x <- sample(1:10)
> y <- sample(1:10)
> cat("===============================n")
> cat(" T-test between x and y n")
> cat("===============================n")
> t.test(x, y)
> sink() # stop writing to the file
> readLines("example.txt")
[1] " [1] 2 5 4 6 9 3 10 1 7 8"
[2] " [1] 6 10 5 1 4 2 9 7 3 8"
[3] "==============================="
[4] " T-test between x and y "
[5] "==============================="
[6] ""
[7] "tWelch Two Sample t-test"
[8] ""
[9] "data: x and y"
[10] "t = 0, df = 18, p-value = 1"
[11] "alternative hypothesis: true difference in means is not equal to 0"
[12] "95 percent confidence interval:"
[13] " -2.844662 2.844662"
[14] "sample estimates:"
[15] "mean of x mean of y "
[16] " 5.5 5.5 "
[17] ""
r-squared
Slide 34
dump()
www.r-squared.in/rprogramming
Description
dump() takes a vector of names of R objects and produces text representation of objects
on a file.
Syntax
dump(list, file = "dumpdata.R", append = FALSE,
control = "all", envir = parent.frame(), evaluate = TRUE)
Returns
R file with text representation of R objects
Documentation
help(dump)
r-squared
Slide 35
dump()
www.r-squared.in/rprogramming
Examples
> # example 1
> x <- sample(1:10)
> y <- sample(1:10)
> xy <- list(x = x, y = y) # create a list
> dump("xy", file = "dump.Rdmped") # write xy to dump.R file
> unlink("dump.R") # close connection to dump.R file
> rm("x", "y", "xy") # remove objects x, y and xy from the workspace
> x # x is not available in the workspace
Error: object 'x' not found
> source("dump.Rdmped") # source dump.R file
> xy
$x
[1] 6 4 9 10 2 8 5 3 7 1
$y
[1] 8 7 6 9 3 1 5 2 4 10
r-squared
Slide 36
save()
www.r-squared.in/rprogramming
Description
save() writes an external representation of the R objects to specified file.
Syntax
save(R object, file_name)
Returns/Creates
R file with external representation of R objects
Documentation
help(save)
r-squared
Slide 37
save()
www.r-squared.in/rprogramming
Examples
> # example 1
> # save the following data in a R data file
> x <- 1:10
> save(x, file = "x.RData")
> # remove x from the workspace
> rm(x)
> # load x into workspace
> load("x.RData") # we will look at this function in a file
> # view the data
> x
[1] 1 2 3 4 5 6 7 8 9 10
r-squared
Slide 38
Next Steps...
www.r-squared.in/rprogramming
In the next unit, we will learn to transform/reshape data in R:
● Reorder Data
● Subset/Filter Data
● Combine Data
● Transform Data
r-squared
Slide 39
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

Contenu connexe

Tendances

Merge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using RMerge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using RYogesh Khandelwal
 
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, Factorskrishna singh
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to RAngshuman Saha
 
Data handling in r
Data handling in rData handling in r
Data handling in rAbhik Seal
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programmingAlberto Labarga
 
3 R Tutorial Data Structure
3 R Tutorial Data Structure3 R Tutorial Data Structure
3 R Tutorial Data StructureSakthi Dasans
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on rAbhik Seal
 
4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply FunctionSakthi Dasans
 
Using Scala Slick at FortyTwo
Using Scala Slick at FortyTwoUsing Scala Slick at FortyTwo
Using Scala Slick at FortyTwoEishay Smith
 
Stata Programming Cheat Sheet
Stata Programming Cheat SheetStata Programming Cheat Sheet
Stata Programming Cheat SheetLaura Hughes
 
Python Pandas
Python PandasPython Pandas
Python PandasSunil OS
 
Stata cheat sheet: data processing
Stata cheat sheet: data processingStata cheat sheet: data processing
Stata cheat sheet: data processingTim Essam
 

Tendances (20)

Merge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using RMerge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using R
 
R programming language
R programming languageR programming language
R programming language
 
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
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Sparklyr
SparklyrSparklyr
Sparklyr
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
 
Data handling in r
Data handling in rData handling in r
Data handling in r
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
3 R Tutorial Data Structure
3 R Tutorial Data Structure3 R Tutorial Data Structure
3 R Tutorial Data Structure
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on r
 
Language R
Language RLanguage R
Language R
 
R factors
R   factorsR   factors
R factors
 
4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function
 
R learning by examples
R learning by examplesR learning by examples
R learning by examples
 
Using Scala Slick at FortyTwo
Using Scala Slick at FortyTwoUsing Scala Slick at FortyTwo
Using Scala Slick at FortyTwo
 
Stata Programming Cheat Sheet
Stata Programming Cheat SheetStata Programming Cheat Sheet
Stata Programming Cheat Sheet
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Rmarkdown cheatsheet-2.0
Rmarkdown cheatsheet-2.0Rmarkdown cheatsheet-2.0
Rmarkdown cheatsheet-2.0
 
Devtools cheatsheet
Devtools cheatsheetDevtools cheatsheet
Devtools cheatsheet
 
Stata cheat sheet: data processing
Stata cheat sheet: data processingStata cheat sheet: data processing
Stata cheat sheet: data processing
 

Similaire à R Programming: Export/Output Data In R

RDataMining slides-r-programming
RDataMining slides-r-programmingRDataMining slides-r-programming
RDataMining slides-r-programmingYanchang Zhao
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programaciónSoftware Guru
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
Introduction to R
Introduction to RIntroduction to R
Introduction to RRajib Layek
 
R (Shiny Package) - Server Side Code for Decision Support System
R (Shiny Package) - Server Side Code for Decision Support SystemR (Shiny Package) - Server Side Code for Decision Support System
R (Shiny Package) - Server Side Code for Decision Support SystemMaithreya Chakravarthula
 
Produce nice outputs for graphical, tabular and textual reporting in R-Report...
Produce nice outputs for graphical, tabular and textual reporting in R-Report...Produce nice outputs for graphical, tabular and textual reporting in R-Report...
Produce nice outputs for graphical, tabular and textual reporting in R-Report...Dr. Volkan OBAN
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7decoupled
 

Similaire à R Programming: Export/Output Data In R (20)

R Basics
R BasicsR Basics
R Basics
 
RDataMining slides-r-programming
RDataMining slides-r-programmingRDataMining slides-r-programming
RDataMining slides-r-programming
 
R programming
R programmingR programming
R programming
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
R workshop
R workshopR workshop
R workshop
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
R basics
R basicsR basics
R basics
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
R (Shiny Package) - Server Side Code for Decision Support System
R (Shiny Package) - Server Side Code for Decision Support SystemR (Shiny Package) - Server Side Code for Decision Support System
R (Shiny Package) - Server Side Code for Decision Support System
 
A Shiny Example-- R
A Shiny Example-- RA Shiny Example-- R
A Shiny Example-- R
 
R language introduction
R language introductionR language introduction
R language introduction
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Programming in R
Programming in RProgramming in R
Programming in R
 
Produce nice outputs for graphical, tabular and textual reporting in R-Report...
Produce nice outputs for graphical, tabular and textual reporting in R-Report...Produce nice outputs for graphical, tabular and textual reporting in R-Report...
Produce nice outputs for graphical, tabular and textual reporting in R-Report...
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Python Day1
Python Day1Python Day1
Python Day1
 

Plus 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
 
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: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to MatricesRsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to VectorsRsquared 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
 

Plus 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
 
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: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
 
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
 

Dernier

Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...amitlee9823
 
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...gajnagarg
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...gajnagarg
 
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...gajnagarg
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteedamy56318795
 
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...amitlee9823
 
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...gajnagarg
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...karishmasinghjnh
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...amitlee9823
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...amitlee9823
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Pooja Nehwal
 

Dernier (20)

Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
 
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...
 
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
 
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
 

R Programming: Export/Output 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 Exporting Data From R www.r-squared.in/rprogramming Objectives In this module, we will learn to: ● Output data to the console ● Output data to files ● Export data into text/CSV files ● Save R objects
  • 5. r-squared Slide 5 Output Data To Console www.r-squared.in/rprogramming In this section, we will learn to output data to the console using the following functions: ✓ print ✓ cat ✓ paste ✓ paste0 ✓ sprintf
  • 6. r-squared Slide 6 print() www.r-squared.in/rprogramming Description print() is a very basic function and as the name suggest it prints its arguments. Syntax print(x, ...) Returns Prints its arguments Documentation help(print)
  • 7. r-squared Slide 7 print() www.r-squared.in/rprogramming Examples > # example 1 > x <- 3 > print(x) [1] 3 > x [1] 3 > # example 2 > name <- "Jovial" > lname <- "Mann" > print(name, lname) Error in print.default(name, lname) : invalid 'digits' argument > print(c(name, lname)) [1] "Jovial" "Mann"
  • 8. r-squared Slide 8 cat() www.r-squared.in/rprogramming Description cat() concatenates its arguments and then outputs them. Syntax cat(... , file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE) Returns Concatenates and outputs its arguments Documentation help(cat)
  • 9. r-squared Slide 9 cat() www.r-squared.in/rprogramming Examples > # example 1 # output to the console > name <- "Jovial" > age <- 28 > cat("Your name is", name, " and you are", age, " years old.") Your name is Jovial and you are 28 years old. > # example 2 # output to a file > name <- "Jovial" > age <- 28 > cat("Your name is", name, " and you are", age, " years old.", file = "words.txt")
  • 10. r-squared Slide 10 cat() www.r-squared.in/rprogramming Examples > # example 3 # difference between print and cat > fname <- "Jovial" > lname <- "Mann" > print(c(fname, lname)) # throws an error [1] "Jovial" "Mann" > name <- c(fname, lname) > name [1] "Jovial" "Mann" > name <- "Jovial Mann" > print(name) [1] "Jovial Mann" > cat(fname, lname) Jovial Mann
  • 11. r-squared Slide 11 paste() www.r-squared.in/rprogramming Description paste() converts its arguments to character strings and concatenates them. Syntax paste (..., sep = " ", collapse = NULL) Returns Character vector Documentation help(paste)
  • 12. r-squared Slide 12 paste() www.r-squared.in/rprogramming Examples > # example 1 > x <- 1:10 > paste(x) [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" > paste(x, collapse = "") [1] "12345678910" > # example 2 > x <- LETTERS > x [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" [23] "W" "X" "Y" "Z" > paste(x) [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" [23] "W" "X" "Y" "Z" > paste(x, collapse = "") [1] "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  • 13. r-squared Slide 13 paste() www.r-squared.in/rprogramming Examples > # example 3 > name <- "Jovial" > age <- 28 > paste(name, age) [1] "Jovial 28" > paste(name, age, sep = ",") [1] "Jovial,28" > paste(name, age, sep = "") [1] "Jovial28"
  • 14. r-squared Slide 14 paste() www.r-squared.in/rprogramming Examples > # example 4 > alpha <- LETTERS[1:4] > numeric <- 1:4 > alpha_numeric <- paste(alpha, numeric) > alpha_numeric [1] "A 1" "B 2" "C 3" "D 4" > alpha_numeric <- paste(alpha, numeric, sep = "") > alpha_numeric [1] "A1" "B2" "C3" "D4"
  • 15. r-squared Slide 15 paste0() www.r-squared.in/rprogramming Description paste0() does the same job as paste but with a blank separator. Syntax paste0(..., collapse = NULL) Returns Character vector of concatenated values. Documentation help(paste0)
  • 16. r-squared Slide 16 paste0() www.r-squared.in/rprogramming Examples > # example 1 > alpha <- LETTERS[1:4] > numeric <- 1:4 > paste0(alpha, numeric) [1] "A1" "B2" "C3" "D4" > paste(alpha, numeric, sep = "") [1] "A1" "B2" "C3" "D4" > # example 2 > name <- "Jovial" > age <- 28 > paste0(name, age) [1] "Jovial28" > paste(name, age, sep = "") [1] "Jovial28"
  • 17. r-squared Slide 17 sprintf() www.r-squared.in/rprogramming Description sprintf() returns a character vector containing text and objects. Syntax sprintf(fmt, ...) Returns Character vector Documentation help(sprintf)
  • 18. r-squared Slide 18 sprintf() www.r-squared.in/rprogramming Examples > # example 1 > name <- "Jovial" > age <- 28 > sprintf("Your name is %s and you are %d years old", name, age) [1] "Your name is Jovial and you are 28 years old" # %s is replaced by the value in name and %d is replaced by value in age. s indicates string type and d indicates integer type.
  • 19. r-squared Slide 19 Output Data To File www.r-squared.in/rprogramming In this section, we will learn to output data to a file using the following functions: ✓ writeLines ✓ write ✓ write.table ✓ write.csv ✓ sink ✓ dump
  • 20. r-squared Slide 20 writeLines() www.r-squared.in/rprogramming Description writeLines() writes text lines to a connection or a file. Syntax writeLines(text, con = stdout(), sep = "n", useBytes = FALSE) Returns File with overwritten text. Documentation help(writeLines)
  • 21. r-squared Slide 21 writeLines() www.r-squared.in/rprogramming Examples > # example 1 > details <- "My name is Jovial." > writeLines(details, "write.txt") > readLines("write.txt") [1] "My name is Jovial." > # example 2 > details_2 <- "I am 28 years old." > writeLines(details_2, "write.txt") > readLines("write.txt") [1] "I am 28 years old." # As you might have observed, writeLines overwrites the contents of a file. To append text to a file, we will use the write function.
  • 22. r-squared Slide 22 write() www.r-squared.in/rprogramming Description write() writes/appends data to a file. Syntax write(x, file = "data", ncolumns = if(is.character(x)) 1 else 5, append = FALSE, sep = " ") Returns File with appended text. Documentation help(write)
  • 23. r-squared Slide 23 write() www.r-squared.in/rprogramming Examples > # example 1 > name <- "My name is Jovial." > write(name, file = "write.txt") > readLines("write.txt") [1] "My name is Jovial." > age <- "I am 28 years old." > write(age, file = "write.txt", append = TRUE) > readLines("write.txt") [1] "My name is Jovial." "I am 28 years old."
  • 24. r-squared Slide 24 write() www.r-squared.in/rprogramming Examples > # example 2 > x <- 1:5 > x [1] 1 2 3 4 5 > write(x, file = "write.txt", append = TRUE) > readLines("write.txt") [1] "My name is Jovial." "I am 28 years old." "1 2 3 4 5" > write(x, file = "write.txt", append = TRUE) > readLines("write.txt") [1] "My name is Jovial." "I am 28 years old." "1 2 3 4 5" "1 2 3 4 5" > write(x, file = "write.txt", append = TRUE, sep = "-") > readLines("write.txt") [1] "My name is Jovial." "I am 28 years old." "1 2 3 4 5" "1 2 3 4 5" [5] "1-2-3-4-5"
  • 25. r-squared Slide 25 write.table() www.r-squared.in/rprogramming Description write.table() will convert the data into data.frame or matrix before writing it to a file. Syntax write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ", eol = "n", na = "NA", dec = ".", row.names = TRUE, col.names = TRUE, qmethod = c("escape", "double"), fileEncoding = "") Returns File with data as a data frame or matrix. Documentation help(write.table)
  • 26. r-squared Slide 26 write.table() www.r-squared.in/rprogramming Examples > # example 1 > m <- matrix(1:9, nrow = 3) > m [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 > # write to a text file > write.table(m, file = "table.txt") > readLines("table.txt") [1] ""V1" "V2" "V3"" ""1" 1 4 7" ""2" 2 5 8" [4] ""3" 3 6 9"
  • 27. r-squared Slide 27 write.table() www.r-squared.in/rprogramming Examples > # example 2 > # write to a csv file > write.table(m, file = "table.csv") > readLines("table.csv") [1] ""V1" "V2" "V3"" ""1" 1 4 7" ""2" 2 5 8" [4] ""3" 3 6 9" > # example 3 > # append the transpose of the matrix > write.table(t(m), file = "table.txt", append = TRUE) > readLines("table.txt") [1] ""V1" "V2" "V3"" ""1" 1 4 7" ""2" 2 5 8" [4] ""3" 3 6 9" ""V1" "V2" "V3"" ""1" 1 2 3" [7] ""2" 4 5 6" ""3" 7 8 9"
  • 28. r-squared Slide 28 write.table() www.r-squared.in/rprogramming > # example 4 > # use comma as a separator > write.table(t(m), file = "table.txt", append = TRUE, sep = ",") > readLines("table.txt") [1] ""V1" "V2" "V3"" ""1" 1 4 7" ""2" 2 5 8" [4] ""3" 3 6 9" ""V1" "V2" "V3"" ""1" 1 2 3" [7] ""2" 4 5 6" ""3" 7 8 9" ""V1","V2","V3"" [10] ""1",1,2,3" ""2",4,5,6" ""3",7,8,9" > # example 5 > # without row and column names > write.table(t(m), file = "table.txt", append = TRUE, row.names = FALSE, + col.names = FALSE) > readLines("table.txt") [1] ""V1" "V2" "V3"" ""1" 1 4 7" ""2" 2 5 8" [4] ""3" 3 6 9" ""V1" "V2" "V3"" ""1" 1 2 3" [7] ""2" 4 5 6" ""3" 7 8 9" ""V1","V2","V3"" [10] ""1",1,2,3" ""2",4,5,6" ""3",7,8,9" [13] "1 2 3" "4 5 6" "7 8 9"
  • 29. r-squared Slide 29 write.csv() www.r-squared.in/rprogramming Description write.csv() will convert the data into data.frame or matrix before writing it to a CSV file. Syntax write.csv(data, file, row.names= FALSE) Returns CSV file Documentation help(write.csv)
  • 30. r-squared Slide 30 write.csv() www.r-squared.in/rprogramming Examples > # example 1 > write.csv(mtcars, "mt.csv") > readLines("mt.csv") [1]""","mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear","carb"" [2] ""Mazda RX4",21,6,160,110,3.9,2.62,16.46,0,1,4,4" [3] ""Mazda RX4 Wag",21,6,160,110,3.9,2.875,17.02,0,1,4,4" [4] ""Datsun 710",22.8,4,108,93,3.85,2.32,18.61,1,1,4,1" > # example 2 > # without row names > write.csv(mtcars, "mt.csv", row.names = FALSE) > readLines("mt.csv") [1] ""mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear","carb"" [2] "21,6,160,110,3.9,2.62,16.46,0,1,4,4" [3] "21,6,160,110,3.9,2.875,17.02,0,1,4,4"
  • 31. r-squared Slide 31 sink() www.r-squared.in/rprogramming Description sink() will divert the R output to a file/connection instead of the console. Syntax sink(file = NULL, append = FALSE, type = c("output", "message"), split = FALSE) Returns File with output Documentation help(sink)
  • 32. r-squared Slide 32 sink() www.r-squared.in/rprogramming Examples > # example 1 > sink(file = "write.txt", append = TRUE, type = "output") > x <- 1:5 > x * 2 > readLines("write.txt") # nothing is printed on console > sink() # stop diverting output to file > readLines("write.txt") [1] "[1] 2 4 6 8 10" [2] "[1] "[1] 2 4 6 8 10"" [3] "[1] 2 4 6 8 10" [4] "[1] "[1] 2 4 6 8 10" "[1] "[1] 2 4 6 8 10""" [5] "[3] "[1] 2 4 6 8 10" "
  • 33. r-squared Slide 33 sink() www.r-squared.in/rprogramming Examples > # example 2 > sink("example.txt") # start writing to example.txt file > x <- sample(1:10) > y <- sample(1:10) > cat("===============================n") > cat(" T-test between x and y n") > cat("===============================n") > t.test(x, y) > sink() # stop writing to the file > readLines("example.txt") [1] " [1] 2 5 4 6 9 3 10 1 7 8" [2] " [1] 6 10 5 1 4 2 9 7 3 8" [3] "===============================" [4] " T-test between x and y " [5] "===============================" [6] "" [7] "tWelch Two Sample t-test" [8] "" [9] "data: x and y" [10] "t = 0, df = 18, p-value = 1" [11] "alternative hypothesis: true difference in means is not equal to 0" [12] "95 percent confidence interval:" [13] " -2.844662 2.844662" [14] "sample estimates:" [15] "mean of x mean of y " [16] " 5.5 5.5 " [17] ""
  • 34. r-squared Slide 34 dump() www.r-squared.in/rprogramming Description dump() takes a vector of names of R objects and produces text representation of objects on a file. Syntax dump(list, file = "dumpdata.R", append = FALSE, control = "all", envir = parent.frame(), evaluate = TRUE) Returns R file with text representation of R objects Documentation help(dump)
  • 35. r-squared Slide 35 dump() www.r-squared.in/rprogramming Examples > # example 1 > x <- sample(1:10) > y <- sample(1:10) > xy <- list(x = x, y = y) # create a list > dump("xy", file = "dump.Rdmped") # write xy to dump.R file > unlink("dump.R") # close connection to dump.R file > rm("x", "y", "xy") # remove objects x, y and xy from the workspace > x # x is not available in the workspace Error: object 'x' not found > source("dump.Rdmped") # source dump.R file > xy $x [1] 6 4 9 10 2 8 5 3 7 1 $y [1] 8 7 6 9 3 1 5 2 4 10
  • 36. r-squared Slide 36 save() www.r-squared.in/rprogramming Description save() writes an external representation of the R objects to specified file. Syntax save(R object, file_name) Returns/Creates R file with external representation of R objects Documentation help(save)
  • 37. r-squared Slide 37 save() www.r-squared.in/rprogramming Examples > # example 1 > # save the following data in a R data file > x <- 1:10 > save(x, file = "x.RData") > # remove x from the workspace > rm(x) > # load x into workspace > load("x.RData") # we will look at this function in a file > # view the data > x [1] 1 2 3 4 5 6 7 8 9 10
  • 38. r-squared Slide 38 Next Steps... www.r-squared.in/rprogramming In the next unit, we will learn to transform/reshape data in R: ● Reorder Data ● Subset/Filter Data ● Combine Data ● Transform Data
  • 39. r-squared Slide 39 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