read.table, data.frame

Question:

I try to perform this excerpt below and it occurs:

rain.df<-read.table("C:\\Users\\Marcia\\Desktop\\Sript R\\daily.dat", header=TRUE)

Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
line 3572 did not have 29 elements.

Why ? grateful in advance

Answer:

A .dat file is general and can be structured in text/data form. What happens in your case is that the default read.table options read.table not read the file correctly. Here's what you can do:

1st) Use readLines to read the file and understand how it is structured. With this, you can check, for example, if it is necessary to skip lines before reading the file ( skip argument, which may be your case) and what is the way to separate it.

Example:

> readLines("https://docs.ufpr.br/~giolo/CE063/Dados/Hemophilia.txt", n=10)
 [1] "\"d1\" \"d2\" \"d3\" \"L\" \"R\" \"Low\" \"Medium\" \"High\"" "0 1 0 7 20 1 0 0"                                            
 [3] "0 1 0 9 12 0 0 1"                                             "0 1 0 9 20 1 0 0"                                            
 [5] "1 0 0 0 25 1 0 0"                                             "0 0 1 57 0 1 0 0"                                            
 [7] "0 1 0 23 26 1 0 0"                                            "0 1 0 8 21 1 0 0"                                            
 [9] "0 1 0 1 6 0 0 1"                                              "0 0 1 55 0 0 0 0"                                            

Then it is clear that the first line is the header, and the rest the data. Also, columns are separated by space. Thus, to perform the reading, just:

> read.table("https://docs.ufpr.br/~giolo/CE063/Dados/Hemophilia.txt", skip = 0, header = T, sep = " ")
    d1 d2 d3  L  R Low Medium High
1    0  1  0  7 20   1      0    0
2    0  1  0  9 12   0      0    1
3    0  1  0  9 20   1      0    0
4    1  0  0  0 25   1      0    0
5    0  0  1 57  0   1      0    0
6    0  1  0 23 26   1      0    0
7    0  1  0  8 21   1      0    0
8    0  1  0  1  6   0      0    1
9    0  0  1 55  0   0      0    0
10   0  0  1 55  0   0      0    0

If you provide the .dat file I edit the answer, but the procedure is the same.

Scroll to Top