File Handling in R Programming
Introduction to File Handling in R
File handling is a crucial skill in R for data analysis. R provides powerful functions to import, export, and view data from various file formats like CSV, TXT, and Excel. This tutorial will guide you through the essential file handling operations in R.
1. Import CSV File in R
CSV (Comma-Separated Values) files are commonly used for storing tabular data.
You can import CSV files into R using the read.csv() function.
How to convert the downloaded .txt file to .csv
Follow these simple steps:
- Copy the entire comma-separated text from the webpage.
- Open Excel or Google Sheets.
- Paste the copied text into the first cell (A1).
-
In Excel:
- Go to Data → Text to Columns.
- Choose Delimited → click Next.
- Check Comma as the delimiter → click Finish.
- Save the file as CSV (Comma delimited):
- Excel: File → Save As → choose CSV (Comma delimited) (*.csv).
- Google Sheets: File → Download → Comma-separated values (.csv, current sheet).
Example:
# Import a CSV file
data <- read.csv("path/to/your/file.csv")
# View the first few rows of the data
head(data)
Replace "path/to/your/file.csv" with the actual path to your CSV file.
If your CSV file uses a different delimiter (e.g., semicolon), use the sep argument:
data <- read.csv("path/to/your/file.csv", sep = ";")
2. Export Data to Excel in R
To export data to Excel, you can use the writexl or openxlsx package.
Learn how to create random data in R programming, which you can export it in excel.
Now, install and load the package:
# Install the writexl package
install.packages("writexl")
# Load the package
library(writexl)
# Export data to Excel
write_xlsx(data, "path/to/your/output.xlsx")
Alternatively, you can use the openxlsx package:
# Install the openxlsx package
install.packages("openxlsx")
# Load the package
library(openxlsx)
# Export data to Excel
write.xlsx(data, "path/to/your/output.xlsx")
3. Read TXT File in R
TXT files can be imported using the read.table() or read.delim() function,
depending on the file's structure.
Example:
# Import a TXT file
data <- read.table("path/to/your/file.txt", header = TRUE, sep = "\t")
# View the first few rows of the data
head(data)
If your TXT file is space-delimited, omit the sep argument or set it to "".
4. Import and Export Files in R
R supports various file formats, including CSV, TXT, Excel, and more. Below are examples of importing and exporting different file types.
Importing Excel Files:
# Install the readxl package
install.packages("readxl")
# Load the package
library(readxl)
# Import an Excel file
data <- read_excel("path/to/your/file.xlsx")
# View the first few rows of the data
head(data)
Exporting to CSV:
# Export data to CSV
write.csv(data, "path/to/your/output.csv", row.names = FALSE)
5. View Data Files in R
Once you've imported your data, you can use several functions to view and inspect it.
Common Functions:
head(data): View the first few rows.tail(data): View the last few rows.str(data): View the structure of the data.summary(data): View summary statistics.View(data): Open a spreadsheet-like view of the data.
Example:
# View the first 6 rows
head(data)
# View the structure of the data
str(data)
# View summary statistics
summary(data)
# Open a spreadsheet-like view
View(data)
Practical Exercise
Follow these steps to practice file handling in R:
- Download a sample CSV file (e.g., sample.csv).
- Import the CSV file into R using
read.csv(). - View the first 10 rows of the data using
head(). - Export the data to an Excel file using the
writexlpackage. - Import a TXT file (e.g., sample.txt) using
read.table(). - View the structure of the imported TXT file using
str().
Solution:
# Step 1: Import CSV file
csv_data <- read.csv("sample.csv")
# Step 2: View the first 10 rows
head(csv_data, 10)
# Step 3: Export to Excel
install.packages("writexl")
library(writexl)
write_xlsx(csv_data, "sample_output.xlsx")
# Step 4: Import TXT file
txt_data <- read.table("sample.txt", header = TRUE, sep = "\t")
# Step 5: View the structure
str(txt_data)
File Handling Tips
- Always check the working directory using
getwd()and set it usingsetwd("path/to/your/directory"). - Use relative paths if your script and data files are in the same directory.
- For large datasets, consider using the
data.tablepackage for faster processing. - Ensure your data files are properly formatted to avoid import errors.

