# Example 1: Fields are in fixed columns separated by variable white space.
# Fields have internal white space. First two lines of file "cars":
# Price Country Reliability Mileage Type
# Acura Integra 4 11950 Japan Much better NA Small
# Give sep argument a vector of column numbers.
# First line has same no. of fields as all the rest, so
# extract row labels using scan, then set header explicitly:
cars.names <- scan("cars", what="", flush = T, widths=30,
skip = 1, strip = TRUE)
cars <- read.table("cars", header = TRUE, row.names = cars.names,
sep = c(30, 36, 46, 58, 66))
# Example 2: Fields are separated by ~ character; header defaults
# to TRUE, and first character field is automatically assigned to
# the row labels. First two lines of file:
# ~Price~Country~Reliability~Mileage~Type
# Acura Integra 4~11950~Japan~Much better~NA~Small
cars <- read.table("cars.tab", sep = "~")
# Example 3: Fields are separated by variable white space.
# There is no internal white space, so you need not specify sep.
# Use na.strings to specify string used for missing data.
# First three lines of file:
# Price Country Reliability Mileage Type
# Acura_Integra_4 11950 Japan Excelent N/A Small
# Dodge_Colt_4 6851 Japan N/A N/A Small
cars <- read.table("cars.na" , na.strings="N/A")