R Read Text Files

Plain text files store data as lines of characters. They range from simple one-value-per-line lists to complex log files and fixed-width tables. R provides several functions to read text files depending on their structure.

Types of Text Files You Will Encounter

File Type          Extension     Structure
──────────────────────────────────────────────────────────
Plain text         .txt          Unstructured, lines of text
Delimited          .csv .tsv     Values separated by character
Fixed-width        .txt .dat     Columns at fixed positions
Log files          .log          Timestamped event records
Configuration      .conf .ini    Key=value pairs

readLines() — Read as a Character Vector

# Each line becomes one element in a character vector
lines <- readLines("notes.txt")

print(lines)
# [1] "First line of the file"
# [2] "Second line of the file"
# [3] "Third line with data"

length(lines)   # number of lines

Read Only Specific Lines

# First 5 lines only
first_five <- readLines("bigfile.txt", n=5)

# Lines 10 to 20
all_lines <- readLines("bigfile.txt")
section   <- all_lines[10:20]

read.table() — Flexible Delimited Files

# Space-separated file
df <- read.table("data.txt", header=TRUE)

# Pipe-separated file
df <- read.table("data.txt", sep="|", header=TRUE)

# No header row
df <- read.table("data.txt", header=FALSE,
                  col.names=c("name","score","city"))

Reading a Fixed-Width File

# Fixed-width: each column occupies specific character positions
# Col1: chars 1-10, Col2: chars 11-15, Col3: chars 16-25
df <- read.fwf("fixed.txt",
                widths     = c(10, 5, 10),
                col.names  = c("Name","Score","City"),
                strip.white = TRUE)

Reading Log Files

# Access log example:
# 192.168.1.1 - - [15/Aug/2024:09:01:23] "GET /index.html" 200 512

log_lines <- readLines("access.log")

# Extract IP addresses using regex
ips <- regmatches(log_lines,
                  regexpr("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}",
                          log_lines))
print(ips)

Reading Key=Value Config Files

# config.txt contents:
# server=localhost
# port=8080
# timeout=30

config_lines <- readLines("config.txt")
config_list  <- strsplit(config_lines, "=")

config <- setNames(
  sapply(config_list, `[`, 2),
  sapply(config_list, `[`, 1)
)

config["port"]      # "8080"
config["server"]    # "localhost"

Writing Text Files

# Write a character vector as lines
lines_to_write <- c("Line 1: Analysis Start",
                     "Line 2: Total records: 500",
                     "Line 3: Analysis Complete")

writeLines(lines_to_write, "output_log.txt")

# Append to existing file
write("New log entry added", "output_log.txt", append=TRUE)

Handling Encoding Issues

# Reading files with non-UTF-8 encoding (Hindi, regional scripts)
lines <- readLines("hindi_data.txt", encoding="UTF-8")

# Or specify in connection
con <- file("data.txt", encoding="UTF-8")
df  <- read.table(con, header=TRUE)
close(con)

Text file reading powers log analysis, configuration management, and processing data from legacy systems that do not output CSV. readLines() is your starting point for any unstructured text, and read.table() handles structured delimited text beyond what read.csv() covers.

Leave a Comment

Your email address will not be published. Required fields are marked *