read.table, data.frame

Asked

Viewed 1,053 times

3

I try to perform this section 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

  • 1

    Maybe I’ll find fill = TRUE.

2 answers

2

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

1º) 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 (argument skip, which may be your case) and what is the manner of separation from 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 row is the header, and the rest the data. Still, the columns are separated by space. That way, to do the reading, it is enough:

> 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 file .dat I edit the answer, but the procedure is the same.

0

This usually occurs when the data you are reading has a line with fewer elements than the others. For example, if you have several rows, all with 29 elements (like columns of a table), and one of these rows is not complete (for example, one of them has only 2 entries), you will get an error of this type. To correct, you can proceed in two ways:

  1. Ensure that the file you are trying to read is complete and in the desired format. This solution may be ok for a small volume of data or when the problem is on the first or last line of the file, however it is a solution far from ideal;
  2. As Rui Barradas commented, the most ideal solution would be to use the parameter fill = TRUE. Therefore, the reading should be done as follows:

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

Source: R_manual Read.table

Browser other questions tagged

You are not signed in. Login or sign up in order to post.