On 9/17/2010 4:39 PM, Phil Spector wrote:
Soyeon -
I think scan() (combined with matrix and data.frame) is the easiest way.
Suppose your text file is called "data.txt". Then

data.frame(matrix(scan('data.txt'),byrow=TRUE,ncol=14))

should give you what you want.
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spec...@stat.berkeley.edu

I agree Phil's solution is probably the easiest, but I thought I'd offer another solution anyway. read.fwf can handle multi-line input files; the downside is that you have to specify the widths of the columns directly (which may change with the file). Upside is that you could specify different data types for different columns if they were not all numeric and you can handle files that don't delimit with white space (neither of which is true for the example you gave).

read.fwf("data.txt", widths=list(c(10,7,7,3,8,8,7,8,5,7,5),c(10,7,5)))


On Fri, 17 Sep 2010, Soyeon Kim wrote:

Dear All,

I am in a trouble with reading data.

It is in txt file looking like this.

0.00632 18.00 2.310 0 0.5380 6.5750 65.20 4.0900 1 296.0 15.30
396.90 4.98 24.00
0.02731 0.00 7.070 0 0.4690 6.4210 78.90 4.9671 2 242.0 17.80
396.90 9.14 21.60
0.02729 0.00 7.070 0 0.4690 7.1850 61.10 4.9671 2 242.0 17.80
392.83 4.03 34.70
0.03237 0.00 2.180 0 0.4580 6.9980 45.80 6.0622 3 222.0 18.70
394.63 2.94 33.40
0.06905 0.00 2.180 0 0.4580 7.1470 54.20 6.0622 3 222.0 18.70
396.90 5.33 36.20
0.02985 0.00 2.180 0 0.4580 6.4300 58.70 6.0622 3 222.0 18.70
394.12 5.21 28.70
0.08829 12.50 7.870 0 0.5240 6.0120 66.60 5.5605 5 311.0 15.20
395.60 12.43 22.90
0.14455 12.50 7.870 0 0.5240 6.1720 96.10 5.9505 5 311.0 15.20
396.90 19.15 27.10

There are 14 variables. Therefore, the first of row should be at the
end of the second row.
So after importing the data, this should look like
v1 v2 v3 v4 v5 v6 v7 v8
v9 v10 v11 v12 v13 v14
0.00632 18.00 2.310 0 0.5380 6.5750 65.20 4.0900 1 296.0
15.30 396.90 4.98 24.00
and so on.

How can import this kind of data into R?

Thank you,

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to