read.table, data.frame

2

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? Thank you in advance

    
asked by anonymous 22.08.2017 / 20:30

2 answers

1

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

1º) Use readLines to read the file and understand how it is structured. So you can check for example if you need to skip lines before reading the file (argument skip , which may be your case) and how to split 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 becomes clear that the first line is the header, and the others are the data. Still, the columns are separated by space. That way, to read, simply:

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

    
26.10.2018 / 17:47
0

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

  • Ensure that the file you are trying to read is complete and in the format you want. This solution may be ok for a small amount of data or when the problem is in the first or last line of the file, but it is a far from ideal solution;
  • As colleague Rui Barradas commented, the most ideal solution would be to use the parameter fill = TRUE . Thus, 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

        
    27.08.2017 / 20:39