In R Programming, handling of files such as reading and writing files can be done by using in-built functions present in R base package. In this article, let us discuss reading and writing of CSV files, creating a file, renaming a file, check the existence of the file, listing all files in the working directory, copying files and creating directories.
Creating a File
Using file.create() function, a new file can be created from console or truncates if already exists. The function returns a TRUE logical value if file is created otherwise, returns FALSE.
Syntax:
file.create(" ")
Parameters:
- ” “: The name of the file to be created.
Example:
# Create a file named Sample.txt
file.create("Sample.txt")
Output:
[1] TRUE
Writing to a File
The write.table() function allows you to write objects such as data frames or matrices to a file. This function is part of the utils package.
Syntax:
write.table(x, file)
Parameters:
- x: The object to be written to the file.
- file: The name of the file to write.
Example:
# Write the first 5 rows of mtcars dataset to Sample.txt
write.table(x = mtcars[1:5, ], file = "Sample.txt")
Output:
The content will be written to “Sample.txt” and can be opened in any text editor.
Renaming a File
The file.rename() function renames a file. It returns TRUE if successful, and FALSE otherwise.
Syntax:
file.rename(from, to)
Parameters:
- from: The current name or path of the file.
- to: The new name or path for the file.
Example:
# Rename Sample.txt to UpdatedSample.txt
file.rename("Sample.txt", "UpdatedSample.txt")
Output:
[1] TRUE
Checking File Existence
To check if a file exists, use the file.exists() function. It returns TRUE if the file exists, and FALSE otherwise.
Syntax:
file.exists(" ")
Parameters:
- ” “: The name of the file to check.
Example:
# Check if Sample.txt exists
file.exists("Sample.txt")
# Check if UpdatedSample.txt exists
file.exists("UpdatedSample.txt")
Output:
[1] FALSE
[1] TRUE
Reading a File
The read.table() function reads files and outputs them as data frames.
Syntax:
read.table(file)
Parameters:
- file: The name of the file to be read.
Example:
# Read UpdatedSample.txt
read_data <- read.table(file = "UpdatedSample.txt")
print(read_data)
Output:
mpg cyl disp hp drat
1 21.0 6 160 110 3.9
2 21.0 6 160 110 3.9
3 22.8 4 108 93 3.8
4 21.4 6 258 110 3.1
5 18.7 8 360 175 3.2
Listing All Files
The list.files() function lists all files in the specified path. If no path is provided, it lists files in the current working directory.
Syntax:
list.files(path)
Parameters:
- path: The directory path.
Example:
# List files in the current directory
list.files()
Output:
[1] "UpdatedSample.txt" "data.csv" "R_Script.R" "output.txt"
Copying a File
The file.copy() function creates a copy of a file.
Syntax:
file.copy(from, to)
Parameters:
- from: The file path to copy.
- to: The destination path.
Example:
# Copy UpdatedSample.txt to a new location
file.copy("UpdatedSample.txt", "Backup/UpdatedSample.txt")
# List files in Backup directory
list.files("Backup")
Output:
[1] TRUE
[1] "UpdatedSample.txt"
Creating a Directory
The dir.create() function creates a directory in the specified path. If no path is provided, it creates the directory in the current working directory.
Syntax:
dir.create(path)
Parameters:
- path: The directory path with the new directory name at the end.
Example:
# Create a directory named DataFiles
dir.create("DataFiles")
# List files in the current directory
list.files()
Output:
[1] "DataFiles" "UpdatedSample.txt" "output.txt"
Reading Files in R Programming
When working with R, the operations are often performed in a terminal or prompt, which does not store data persistently. To preserve data beyond the program’s execution, it can be saved to files. This approach is also useful for transferring large datasets without manual entry. Files can be stored in formats like .txt (tab-separated values), .csv (comma-separated values), or even hosted online or in cloud storage. R provides convenient methods to read and write such files.
File Reading in R
Reading Text Files
Text files are a popular format for storing data. R provides several methods for reading text files into your program.
1. read.delim(): Used for reading tab-separated (.txt) files with a period (.) as the decimal point.
Syntax:
read.delim(file, header = TRUE, sep = "\t", dec = ".", ...)
Parameters:
file: Path to the file.header: Logical; ifTRUE, assumes the first row contains column names.sep: Separator character (default is tab:"\t").dec: Decimal character ("."by default).
Example:
# Reading a text file
data <- read.delim("datafile.txt", header = FALSE)
print(data)
Output:
1 Sample text data for demonstration.
2. read.delim2(): Similar to read.delim(), but uses a comma (,) as the decimal point.
Syntax:
read.delim2(file, header = TRUE, sep = "\t", dec = ",", ...)
Example:
data <- read.delim2("datafile.txt", header = FALSE)
print(data)
Output:
1 Sample text data for demonstration.
3. file.choose(): Allows interactive file selection, useful for beginners.
Example:
data <- read.delim(file.choose(), header = FALSE)
print(data)
Output:
1 Sample text data for demonstration.
4. read_tsv()
Part of the readr package, this method reads tab-separated files.
Syntax:
read_tsv(file, col_names = TRUE)
Example:
library(readr)
# Reading a text file using read_tsv
data <- read_tsv("datafile.txt", col_names = FALSE)
print(data)
Output:
# A tibble: 1 x 1
Column1
1 Sample text data for demonstration.
Reading Specific Lines
1. read_lines(): Reads specific lines from a file. Available in the readr package.
Syntax:
read_lines(file, skip = 0, n_max = -1L)
Example:
library(readr)
# Reading one line
line <- read_lines("datafile.txt", n_max = 1)
print(line)
# Reading two lines
lines <- read_lines("datafile.txt", n_max = 2)
print(lines)
Output:
[1] "Sample text data for demonstration."
[1] "Sample text data for demonstration."
[2] "Additional line for testing."
Reading the Entire File
1. read_file(): Reads the entire content of a file.
Syntax:
read_file(file)
Example:
library(readr)
# Reading the entire file
content <- read_file("datafile.txt")
print(content)
Output:
[1] "Sample text data for demonstration.\nAdditional line for testing.\nEnd of file."
Reading Tabular Files
1. read.table(): Reads data stored in a tabular format.
Syntax:
read.table(file, header = FALSE, sep = "", dec = ".")
Example:
# Reading a tabular file
data <- read.table("tabledata.csv")
print(data)
Output:
1 Name Age Qualification Address
2 Alex 25 MSc New York
3 Jamie 30 BSc Chicago
4 Chris 28 PhD Boston
2. read.csv(): Used for comma-separated (.csv) files.
Syntax:
read.csv(file, header = TRUE, sep = ",", dec = ".", ...)
Example:
data <- read.csv("tabledata.csv")
print(data)
Output:
Name Age Qualification Address
1 Alex 25 MSc New York
2 Jamie 30 BSc Chicago
3 Chris 28 PhD Boston
Reading Files from the Web
It is possible to read files hosted online using read.delim(), read.csv(), or read.table().
Example:
# Reading data from the web
data <- read.delim("http://example.com/sampledata.txt")
print(head(data))
Output:
ID Value Category
1 101 20 A
2 102 15 B
3 103 30 A
4 104 25 C
5 105 10 B
Writing to Files in R Programming
R is a powerful programming language widely used for data analytics across various industries. Data analysis often involves reading and writing data from and to various file formats, such as Excel, CSV, and text files. This guide explores multiple ways of writing data to different types of files using R programming.
Writing Data to Files in R
1. Writing Data to CSV Files in R: CSV (Comma Separated Values) files are extensively used for managing large amounts of statistical data. Below is the syntax for writing data to a CSV file:
Syntax:
write.table(my_data, file = "my_dataset.txt", sep = " ")
Example:
my_data <- data.frame(
ID = c(1, 2, 3),
Name = c("John", "Doe", "Smith"),
Marks = c(88, 92, 75)
)
write.table(my_data, file = "example_dataset.txt", sep = "\t")
Output in example_dataset.txt:
"ID" "Name" "Marks"
1 "John" 88
2 "Doe" 92
3 "Smith" 75
Writing Data to Excel Files
To write data to Excel files, you need to use the xlsx package. This package is a Java-based solution for reading and writing Excel files. Install the package using the following command:
install.packages("xlsx")
Load the library and use the write.xlsx() function to write data to Excel files:
Syntax:
library("xlsx")
write.xlsx(my_data, file = "output_data.xlsx", sheetName = "Sheet1", append = FALSE)
Example:
library("xlsx")
my_data <- data.frame(
Product = c("Laptop", "Tablet", "Smartphone"),
Quantity = c(50, 80, 100),
Price = c(700, 300, 500)
)
write.xlsx(my_data, file = "products_data.xlsx", sheetName = "Inventory", append = FALSE)
Output in products_data.xlsx (Sheet Name: Inventory):
| Product | Quantity | Price |
|---|---|---|
| Laptop | 50 | 700 |
| Tablet | 80 | 300 |
| Smartphone | 100 | 500 |
Working with Binary Files in R Programming
In computer science, text files contain human-readable data such as letters, numbers, and other characters. In contrast, binary files are composed of 1s and 0s that only computers can process. The data stored in a binary file is unreadable by humans as the bytes represent characters, symbols, and other non-printable elements.
Sometimes, it becomes necessary to handle data in binary format in the R language. This might involve reading data generated by other programs or creating binary files that can be shared with different systems. Below are the four primary operations that can be performed with binary files in R:
- Creating and Writing to a Binary File
- Reading from a Binary File
- Appending to a Binary File
- Deleting a Binary File
1. Creating and Writing to a Binary File
You can create and write to a binary file using the writeBin() function. The file is opened in “wb” mode, where w stands for write and b for binary mode.
Syntax:
writeBin(object, con)
Parameters:
object: An R object to write to the file.con: A connection object, a file path, or a raw vector.
Example: Writing a Binary File
# Create a data frame
students <- data.frame(
"RollNo" = c(101, 102, 103, 104),
"Name" = c("Alice", "Bob", "Charlie", "David"),
"Age" = c(21, 22, 20, 23),
"Marks" = c(85, 90, 88, 92)
)
# Open a connection in binary write mode
conn <- file("student_data.dat", "wb")
# Write the column names to the binary file
writeBin(colnames(students), conn)
# Write the values of each column
writeBin(c(students$RollNo, students$Name, students$Age, students$Marks), conn)
# Close the connection
close(conn)
Output:
The file student_data.dat is created with the given data.
2. Reading from a Binary File
To read a binary file, use the readBin() function. Open the file in “rb” mode, where r indicates read and b indicates binary mode.
Syntax:
readBin(con, what, n)
Parameters:
con: A connection object, a file path, or a raw vector.what: The type of data to read (e.g.,integer,character,numeric, etc.).n: The maximum number of records to read.
Example: Reading a Binary File
# Open a connection in binary read mode
conn <- file("student_data.dat", "rb")
# Read the column names
column_names <- readBin(conn, character(), n = 4)
# Read the values
data_values <- readBin(conn, character(), n = 20)
# Extract values by indices
RollNo <- data_values[5:8]
Name <- data_values[9:12]
Age <- as.numeric(data_values[13:16])
Marks <- as.numeric(data_values[17:20])
# Combine values into a data frame
final_data <- data.frame(RollNo, Name, Age, Marks)
colnames(final_data) <- column_names
# Close the connection
close(conn)
# Print the data frame
print(final_data)
Output:
RollNo Name Age Marks
1 101 Alice 21 85
2 102 Bob 22 90
3 103 Charlie 20 88
4 104 David 23 92
3. Appending to a Binary File
Appending data to a binary file is done using the writeBin() function in “ab” mode, where a stands for append and b for binary mode.
Example: Appending Data to a Binary File
# Create additional data
new_data <- data.frame(
"Subjects" = c("Math", "Science", "History", "English"),
"Grades" = c("A", "B", "A", "A")
)
# Open a connection in binary append mode
conn <- file("student_data.dat", "ab")
# Append column names and values to the binary file
writeBin(colnames(new_data), conn)
writeBin(c(new_data$Subjects, new_data$Grades), conn)
# Close the connection
close(conn)
Output:
The file student_data.dat now contains the appended data.
4. Deleting a Binary File
Binary files can be deleted using the file.remove() function, and their links can be removed using unlink().
Example: Deleting a Binary File
xGlobal <- runif(5)
yGlobal <- runif(5)
f <- function() {
x <- xGlobal
y <- yGlobal
plot(y ~ x)
}
codetools::findGlobals(f)
Output:
[1] "File successfully deleted."
Leave a Reply