0

I have a .DAT file that contains several thousand rows of data. Each row has a fixed number of variables and each row is a case, but not every case has values for each variable. So if a case doesn't have a value for a variable, that space will be blank. So the entire data looks like a sparse matrix. A sample data looks like below:

10101010  100  10000FM

001 100 100  1000000 F

I want to read this data in r as a data frame. I've tried read.table but failed. My code is

m <- read.table("C:/Users/Desktop/testdata.dat", header = FALSE)

R gives me error message like

"Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 1 did not have 6 elements"

How do I fix this?

ashwin agrawal
  • 1,603
  • 8
  • 16
  • It sounds like you have a fixed-width file. Here's a duplicate [thread on SO on using `read.fwf`](https://stackoverflow.com/questions/14383710/read-fixed-width-text-file). Maybe only difference is that you have `widths=1` for 1-character fields? Hope it helps. – ravic_ Nov 22 '19 at 21:53

1 Answers1

0

Generally the dat file has some lines of extra information before actual data.

Skip them with the skip argument as follows:

df<-read.table("C:/Users/Desktop/testdata.dat",header=FALSE, skip=3)

Else you can also try the below using the readlines function, this will read the specified number of lines from your file (pass n parameter as below):

readLines("C:/Users/Desktop/testdata.dat",n=5)

ashwin agrawal
  • 1,603
  • 8
  • 16