user9622749
user9622749

Reputation: 11

How do I read this txt file in R?

0.1825845 1.82775 1.547577 3.292672
4.811791 4.660616 6.591353 7.89525 6.634558
6.671906 7.368399 5.729084 4.901463 4.784151
6.180467 6.072145 4.690723 4.634633 5.251753
3.098002 2.511983 4.120591 3.641962 4.032412
2.911343 2.592363 1.646208 0.8059016 0.3112239
-2.35102 -0.7046296 1.492431 1.642447 4.454217
4.946531 4.926586 2.306022 2.011536 5.226938
3.10814 2.111353 2.928797 2.432052 2.231116
1.534472 0.9636707 2.546463 6.388513 9.335976
5.930774 4.148496 5.377457 6.940673 10.30807
11.59153 10.64519 9.945118 9.17827 9.49149
10.49464 10.15781 8.82073 7.961096 9.589847
8.222084 7.088067 8.041036 7.15563 7.343862
8.118055 8.47373 9.182639 9.932846 9.715861
9.299668 7.81619 7.523487 6.698099 6.468871
8.330133 9.781642 9.918872 7.934951 7.258369
9.862305 10.51931 11.92558 10.6621 7.123752
6.52588 7.957712 8.391421 8.967569 10.70995
9.874107 9.90484 14.87436 14.9034 15.7997
18.71859

I'm trying to read this file first because I want to convert it to ts data, but I get an error.

> read.table("C:/R/ex2.txt")
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  : 
  line 20 did not have 5 elements

How can I read this data file and convert it to ts data?

Upvotes: 0

Views: 54

Answers (1)

Amar
Amar

Reputation: 1373

This should work for you:

dat <- read.csv("C:/R/ex2.txt", sep = " ", header = FALSE)

NA will be added to any cell that has no data.

           V1         V2        V3         V4         V5
1   0.1825845  1.8277500  1.547577  3.2926720         NA
2   4.8117910  4.6606160  6.591353  7.8952500  6.6345580
3   6.6719060  7.3683990  5.729084  4.9014630  4.7841510
4   6.1804670  6.0721450  4.690723  4.6346330  5.2517530
5   3.0980020  2.5119830  4.120591  3.6419620  4.0324120
6   2.9113430  2.5923630  1.646208  0.8059016  0.3112239
7  -2.3510200 -0.7046296  1.492431  1.6424470  4.4542170
8   4.9465310  4.9265860  2.306022  2.0115360  5.2269380
9   3.1081400  2.1113530  2.928797  2.4320520  2.2311160
10  1.5344720  0.9636707  2.546463  6.3885130  9.3359760
11  5.9307740  4.1484960  5.377457  6.9406730 10.3080700
12 11.5915300 10.6451900  9.945118  9.1782700  9.4914900
13 10.4946400 10.1578100  8.820730  7.9610960  9.5898470
14  8.2220840  7.0880670  8.041036  7.1556300  7.3438620
15  8.1180550  8.4737300  9.182639  9.9328460  9.7158610
16  9.2996680  7.8161900  7.523487  6.6980990  6.4688710
17  8.3301330  9.7816420  9.918872  7.9349510  7.2583690
18  9.8623050 10.5193100 11.925580 10.6621000  7.1237520
19  6.5258800  7.9577120  8.391421  8.9675690 10.7099500
20  9.8741070  9.9048400 14.874360 14.9034000 15.7997000
21 18.7185900         NA        NA         NA         NA

Upvotes: 1

Related Questions