我正在尝试读取R中的dat文件.数据可在此处获得.
有关数据集的规范可在此处获得.
我的问题的第一部分通过使用该sep选项并提供有关每列开始和提供的位置的信息来解决na.strings="*".但是,我不知道如何处理超过1行的单个观察.
在该数据集中,所有观察结果跨越2行.
Chi*_*til 10
你真的需要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'
Run Code Online (Sandbox Code Playgroud)
使用read.fwf读取 - 注意widths参数.的widths应该是2的整数次向量在多行上指定元素宽度的列表
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)
Run Code Online (Sandbox Code Playgroud)
使用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
## -----
##
Run Code Online (Sandbox Code Playgroud)
这是一个解决方法:
link <- "http://www.amstat.org/publications/jse/datasets/93cars.dat.txt"
Run Code Online (Sandbox Code Playgroud)
阅读原始行:
rawlines <- readLines(file(link))
Run Code Online (Sandbox Code Playgroud)
转换为一个文本字符串:
lines <- paste(rawlines[c(TRUE, FALSE)], rawlines[c(FALSE, TRUE)], collapse = "\n")
Run Code Online (Sandbox Code Playgroud)
该函数paste用于组合多个字符串.rawlines[c(TRUE, FALSE)]代表奇数行,rawlines[c(FALSE, TRUE)]代表偶数行.(有关如何使用布尔值选择偶数和奇数元素的详细信息,请参阅此答案.)两条小线组合成一条长线.然后,所有长行与参数组合成一个单独的字符串collapse = "\n".这些行由换行符(\n)分隔.
阅读新文字read.table:
dat <- read.table(text = lines, na.string = "*")
Run Code Online (Sandbox Code Playgroud)
结果:
> 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
Run Code Online (Sandbox Code Playgroud)