This document discusses various functions in R for exporting data, including print(), cat(), paste(), paste0(), sprintf(), writeLines(), write(), write.table(), write.csv(), and sink(). It provides descriptions, syntax, examples, and help documentation for each function. The functions can be used to output data to the console, files, or save R objects. write.table() and write.csv() convert data to a data frame or matrix before writing to a text file or CSV. sink() diverts R output to a file instead of the console.
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
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
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
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.
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)
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
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
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