dat="mpg cyl disp hp drat wt qsec vs am gear carb
21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
2"
我需要它,如下所示,请参阅第6行?
> read.table(text=dat,fill=T,header=TRUE)
mpg cyl disp hp drat wt qsec vs am gear carb
1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
5 2.0 NA NA NA NA NA NA NA NA NA NA
> read.table(text=dat,fill=T,header=TRUE)
mpg cyl disp hp drat wt qsec vs am gear carb
1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
5 NA NA NA NA NA NA NA NA NA NA 2
答案 0 :(得分:1)
我自己解决了。
read.fwf(file=textConnection(dat),fill=TRUE,skip=1,widths=c(12,4,6,4,5,6,6,3,3,5,5)) -> r
unlist(strsplit(y,split="\\s+")) -> colnames(y)
unlist(strsplit(y,split="\\s+")) -> colnames(r)