Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in read.fwf when header=TRUE

I have simulated data that looks like this:

LastName   Date      email                                                    CreditCardNum   AgeZip  Amount
Paul       21/02/14  [email protected]                     4241033422900360 6738851$14.39
Bullock      2/7/[email protected]                     5178789953524240 3336538$498.31
Mcmahon     11/5/[email protected]                              5389589582467450 7734302$92.44
Walters    25/09/13  [email protected]                       5157094536097720 7794007$206.60
Franco     17/06/13  [email protected]                                 345477952996264 2415873$89.12

and this is how I'm attempting to import it into R, with headers:

w <- c(11,10,57,16,3,5,8)
df <- read.fwf("data.txt",widths=w,stringsAsFactors=F)
names(df) <- df[1,]; df <- df[-1,]

The reason I'm not using header=T is that it gives me the error:

Error in read.table(file = FILE, header = header, sep = sep, row.names = row.names,  :  more columns than column names

which just isn't true. I know the widths (w) are correct. So where is this error coming from? My solution works fine, I'd just like to understand what's happening.

like image 278
shadowtalker Avatar asked Sep 14 '26 16:09

shadowtalker


1 Answers

If you specify header=TRUE, then, as per ?read.fwf, you must ensure that the column names are separated by sep. The default is for names to be separated by \t (the tab character) and this must not be true for your data.

The following works just fine:

w <- c(11, 10, 57, 16, 3, 5, 8)

read.fwf(widths=w, header=TRUE, sep='|', 
file=textConnection('LastName   |Date      |email                                                    |CreditCardNum   |Age|Zip  |Amount
Paul       21/02/14  [email protected]                     4241033422900360 6738851$14.39
Bullock      2/7/[email protected]                     5178789953524240 3336538$498.31
Mcmahon     11/5/[email protected]                              5389589582467450 7734302$92.44
Walters    25/09/13  [email protected]                       5157094536097720 7794007$206.60
Franco     17/06/13  [email protected]                                 345477952996264 2415873$89.12'))
like image 127
jbaums Avatar answered Sep 16 '26 07:09

jbaums



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!