R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
File handling is an essential aspect of data analysis in R, as it allows you to import data for analysis and export results or modified data. In this tutorial, we'll discuss various file handling capabilities in R, from reading and writing text and CSV files to more advanced operations.
Reading Text Files:
Use the readLines()
function to read lines from a text file.
lines <- readLines("filename.txt") print(lines)
Writing to Text Files:
Use the writeLines()
function to write lines to a text file.
lines <- c("Hello", "World!") writeLines(lines, "output.txt")
Reading CSV Files:
Use the read.csv()
function.
data <- read.csv("data.csv") head(data)
Writing CSV Files:
Use the write.csv()
function.
write.csv(data, "output.csv", row.names = FALSE)
Checking if a File Exists:
file.exists("filename.txt") # Returns TRUE or FALSE
Deleting a File:
file.remove("filename.txt")
Renaming a File:
file.rename("oldname.txt", "newname.txt")
Create a Directory:
dir.create("new_directory")
List Files in a Directory:
list.files(path = ".", pattern = NULL) # Use pattern to filter by file type, e.g., "*.csv"
To work with Excel files, you can use the openxlsx
package.
install.packages("openxlsx") library(openxlsx) # Reading from Excel data <- read.xlsx("data.xlsx", sheet = 1) # Writing to Excel write.xlsx(data, "output.xlsx")
Use the getwd()
function to get the current working directory, and setwd()
to set a new working directory.
getwd() # Get the current working directory setwd("/path/to/directory") # Set a new working directory
R provides various packages like DBI
, RSQLite
, RMySQL
, and others to interact with databases. This allows you to directly read from or write to databases.
install.packages("DBI") install.packages("RSQLite") library(DBI) library(RSQLite) # Create a connection con <- dbConnect(RSQLite::SQLite(), dbname = "mydb.sqlite") # Read data from a table data <- dbReadTable(con, "mytable") # Write data to a table dbWriteTable(con, "newtable", data) # Close the connection dbDisconnect(con)
File handling in R provides the flexibility to interact with various types of files, making it easier to import/export and manipulate data. Whether you're dealing with text files, CSVs, Excel spreadsheets, or databases, R offers various functions and packages to assist you. This capability, combined with R's powerful data analysis and visualization tools, makes R a comprehensive data analysis platform.
Write to file in R:
# Write to a text file data <- c("apple", "banana", "cherry") writeLines(data, "fruits.txt")
Read and write CSV files in R:
read.csv()
and write.csv()
.# Read data from CSV file read_data <- read.csv("data.csv") # Write data to CSV file write.csv(read_data, "output_data.csv", row.names = FALSE)
Working with text files in R:
readLines()
and writeLines()
.# Read and write text files text <- readLines("input.txt") writeLines(text, "output.txt")
R file connection functions:
file()
, url()
, and gzfile()
, which allow flexible handling of different file types and sources.# File connection functions in R connection <- file("example.txt", "r") data <- readLines(connection) close(connection)
Binary file handling in R:
readBin()
and writeBin()
.# Binary file handling in R binary_data <- readBin("binary_file.bin", what = "raw", n = 100) writeBin(binary_data, "output_binary.bin")
Reading Excel files in R:
readxl
or openxlsx
.# Install and load the readxl package install.packages("readxl") library(readxl) # Read data from Excel file excel_data <- read_excel("data.xlsx")
R file manipulation functions:
# File manipulation functions in R old_name <- "old_file.txt" new_name <- "new_file.txt" # Rename file file.rename(old_name, new_name) # Check if file exists file.exists(new_name) # Delete file file.remove(new_name)