实际上,你需要使用read.fwf
来完成此操作。
设置一些示例数据
txt <- 'Acura Integra Small 12.9 15.9 18.8 25 31 0 1 4 1.8 140 6300
2890 1 13.2 5 177 102 68 37 26.5 11 2705 0
Acura Legend Midsize 29.2 33.9 38.7 18 25 2 1 6 3.2 200 5500
2335 1 18.0 5 195 115 71 38 30.0 15 3560 0
Audi 90 Compact 25.9 29.1 32.3 20 26 1 1 6 2.8 172 5500
2280 1 16.9 5 180 102 67 37 28.0 14 3375 0'
使用read.fwf读取文本文件时,请注意widths
参数。 widths
应该是由两个整数向量组成的列表,指定多行上的元素宽度
DF <- read.fwf(textConnection(txt),
widths = list(
c(14, 15, 8, 5, 5, 5, 3, 3, 2, 2, 2, 4, 4, 4),
c(5, 2, 5, 2, 4, 4, 3, 3, 5, 3, 5, 1)
),
header = FALSE)
使用 pander
包美观地打印表格,因为它有很多列。
require(pander)
pandoc.table(DF)
##
## ---------------------------------------------------
## V1 V2 V3 V4 V5 V6 V7 V8 V9
## ----- ------- ------- ---- ---- ---- ---- ---- ----
## Acura Integra Small 12.9 15.9 18.8 25 31 0
##
## Acura Legend Midsize 29.2 33.9 38.7 18 25 2
##
## Audi 90 Compact 25.9 29.1 32.3 20 26 1
## ---------------------------------------------------
##
## Table: Table continues below
##
##
## -----------------------------------------------
## V10 V11 V12 V13 V14 V15 V16 V17
## ----- ----- ----- ----- ----- ----- ----- -----
## 1 4 1.8 140 6300 2890 1 13.2
##
## 1 6 3.2 200 5500 2335 1 18.0
##
## 1 6 2.8 172 5500 2280 1 16.9
## -----------------------------------------------
##
## Table: Table continues below
##
##
## -----------------------------------------------
## V18 V19 V20 V21 V22 V23 V24 V25
## ----- ----- ----- ----- ----- ----- ----- -----
## 5 177 102 68 37 26.5 11 2705
##
## 5 195 115 71 38 30.0 15 3560
##
## 5 180 102 67 37 28.0 14 3375
## -----------------------------------------------
##
## Table: Table continues below
##
##
## -----
## V26
## -----
## 0
##
## 0
##
## 0
## -----
##
link <- "http://www.amstat.org/publications/jse/datasets/93cars.dat.txt"
阅读原始行:
rawlines <- readLines(file(link))
转换为一个文本字符串:
lines <- paste(rawlines[c(TRUE, FALSE)], rawlines[c(FALSE, TRUE)], collapse = "\n")
函数paste
用于将多个字符串组合在一起。rawlines[c(TRUE, FALSE)]
表示奇数行,rawlines[c(FALSE, TRUE)]
表示偶数行。(有关如何使用布尔值选择偶数和奇数元素的详细信息,请参见此答案。)两行小行被合并成一条长行。然后,所有长行都被合并成一个单独的字符串,参数为collapse = "\n"
。行由换行符(\n
)分隔。
使用read.table
读取新文本:
dat <- read.table(text = lines, na.string = "*")
结果:
> head(dat)
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17
1 Acura Integra Small 12.9 15.9 18.8 25 31 0 1 4 1.8 140 6300 2890 1 13.2
2 Acura Legend Midsize 29.2 33.9 38.7 18 25 2 1 6 3.2 200 5500 2335 1 18.0
3 Audi 90 Compact 25.9 29.1 32.3 20 26 1 1 6 2.8 172 5500 2280 1 16.9
4 Audi 100 Midsize 30.8 37.7 44.6 19 26 2 1 6 2.8 172 5500 2535 1 21.1
5 BMW 535i Midsize 23.7 30.0 36.2 22 30 1 0 4 3.5 208 5700 2545 1 21.1
6 Buick Century Midsize 14.2 15.7 17.3 22 31 1 1 4 2.2 110 5200 2565 0 16.4
V18 V19 V20 V21 V22 V23 V24 V25 V26
1 5 177 102 68 37 26.5 11 2705 0
2 5 195 115 71 38 30.0 15 3560 0
3 5 180 102 67 37 28.0 14 3375 0
4 6 193 106 70 37 31.0 17 3405 0
5 4 186 109 69 39 27.0 13 3640 0
6 6 189 105 69 41 28.0 16 2880 1
fill=TRUE
。dat <- read.table('93cars.dat.txt',fill=TRUE)
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14
1 Acura Integra Small 12.9 15.9 18.8 25 31 0 1 4 1.8 140 6300
2 2890 1 13.2 5.0 177.0 102.0 68 37 26.5 11 2705 0.0 NA NA
3 Acura Legend Midsize 29.2 33.9 38.7 18 25 2 1 6 3.2 200 5500
4 2335 1 18.0 5.0 195.0 115.0 71 38 30.0 15 3560 0.0 NA NA
5 Audi 90 Compact 25.9 29.1 32.3 20 26 1 1 6 2.8 172 5500
如果您想将每行数据分别放入不同的 data.frame 中;也就是说,将 line1、line3 等放在一个 data.frame 中,将 line2、line4 等放在另一个 data.frame 中,则可以按照以下方法进行:
dat.even <- dat[c(FALSE,TRUE),]
dat.odd <- dat[c(TRUE,FALSE),]