Exporting Data in detail
When a program terminates, all data held in the program is lost. To ensure data persistence, we store the fetched information in files. This enables transferring data across systems and prevents re-entering large datasets. Files can be stored in formats such as .txt, .csv, or even in online/cloud storage. R provides straightforward methods to export data to these file types.
Exporting Data to a Text File
Text files are a common format for data storage. R provides methods like write.table() to export data frames or matrices to text files.
1. write.table(): The write.table() function writes a data frame or matrix to a text file.
Syntax:
write.table(x, file, append = FALSE, sep = " ", dec = ".", row.names = TRUE, col.names = TRUE)
Parameters:
- x: Data frame or matrix to be written.
- file: File name as a string.
- sep: Field separator (e.g.,
\tfor tab-separated values). - dec: Decimal separator (default is
.). - row.names: Logical or character vector for row names.
- col.names: Logical or character vector for column names.
Example:
# Creating a data frame
employee_data <- data.frame(
"Employee" = c("John", "Emma", "Liam"),
"Department" = c("HR", "IT", "Finance"),
"Age" = c(29, 34, 41)
)
# Exporting the data frame to a text file
write.table(employee_data,
file = "employee_data.txt",
sep = "\t",
row.names = TRUE,
col.names = NA)
Output:
"" "Employee" "Department" "Age"
"1" "John" "HR" 29
"2" "Emma" "IT" 34
"3" "Liam" "Finance" 41
write_tsv(): The write_tsv() method from the readr package exports tab-separated values.
Syntax:
write_tsv(file, path)
Example:
# Creating a data frame
employee_data <- data.frame(
"Employee" = c("John", "Emma", "Liam"),
"Department" = c("HR", "IT", "Finance"),
"Age" = c(29, 34, 41)
)
# Exporting the data frame to a text file
write.table(employee_data,
file = "employee_data.txt",
sep = "\t",
row.names = TRUE,
col.names = NA)
Output:
"" "Employee" "Department" "Age"
"1" "John" "HR" 29
"2" "Emma" "IT" 34
"3" "Liam" "Finance" 41
write_tsv(): The write_tsv() method from the readr package exports tab-separated values.
Syntax:
write_tsv(file, path)
Example:
# Importing the readr package
library(readr)
# Creating a data frame
student_data <- data.frame(
"Name" = c("Alice", "Bob", "Charlie"),
"Grade" = c("A", "B", "A+"),
"Age" = c(20, 22, 21)
)
# Exporting the data frame using write_tsv()
write_tsv(student_data, path = "student_data.txt")
Output:
Name Grade Age
Alice A 20
Bob B 22
Charlie A+ 21
Exporting Data to a CSV File
CSV files are widely used for storing tabular data. R provides multiple methods for exporting data to .csv files.
write.table(): The write.table() function can also export data to CSV files by specifying sep = ",".
Example:
# Creating a data frame
product_data <- data.frame(
"Product" = c("Laptop", "Phone", "Tablet"),
"Price" = c(1000, 500, 300),
"Stock" = c(50, 200, 150)
)
# Exporting the data frame to a CSV file
write.table(product_data,
file = "product_data.csv",
sep = ",",
row.names = FALSE)
Output:
Product,Price,Stock
Laptop,1000,50
Phone,500,200
Tablet,300,150
write.csv()
The write.csv() function simplifies exporting data to CSV files, using a comma as the default separator.
Example:
# Creating a data frame
city_data <- data.frame(
"City" = c("New York", "Los Angeles", "Chicago"),
"Population" = c(8419600, 3980400, 2716000),
"Area" = c(468.9, 503, 227.3)
)
# Exporting the data frame to a CSV file
write.csv(city_data, file = "city_data.csv")
Output:
"","City","Population","Area"
"1","New York",8419600,468.9
"2","Los Angeles",3980400,503
"3","Chicago",2716000,227.3
write.csv2():The write.csv2() function is similar to write.csv() but uses a semicolon (;) as the separator and a comma for the decimal point.
Example:
# Creating a data frame
sales_data <- data.frame(
"Month" = c("January", "February", "March"),
"Sales" = c(15000.50, 17000.75, 16000.30)
)
# Exporting the data frame to a CSV file
write.csv2(sales_data, file = "sales_data.csv")
Output:
";""Month"";""Sales"
"1";"January";"15000,50"
"2";"February";"17000,75"
"3";"March";"16000,30"
write_csv(): The write_csv() method from the readr package exports data to CSV files.
Example:
# Importing the readr package
library(readr)
# Creating a data frame
book_data <- data.frame(
"Title" = c("R for Data Science", "Python Crash Course", "The Art of R Programming"),
"Author" = c("Hadley Wickham", "Eric Matthes", "Norman Matloff"),
"Price" = c(35.99, 29.99, 45.00)
)
# Exporting the data frame using write_csv()
write_csv(book_data, path = "book_data.csv")
Output:
Title,Author,Price
R for Data Science,Hadley Wickham,35.99
Python Crash Course,Eric Matthes,29.99
The Art of R Programming,Norman Matloff,45.00
Leave a Reply