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?
read.fwf
. Maybe only difference is that you havewidths=1
for 1-character fields? Hope it helps. – ravic_